c++ - 使用堆栈的内存错误代码 0xC0000005
问题描述
我试图解决无向图中双连接组件的问题。算法本身在这里并不重要,我只是想弄清楚为什么会出现以下运行时错误0xC0000005
。(我什至不确定算法是否完全正确)。每当我从堆栈中弹出时,我检查它是否不为空,并且while不能是无限的。这是代码:
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
using namespace std;
ifstream in("zao.in");
const int nmax = 1005;
int nivel[nmax];
vector<int> v[nmax];
bool viz[nmax];
int nivelMin[nmax];
stack<int> s;
void dfs(int nod){
viz[nod] = true;
nivelMin[nod] = nivel[nod];
s.push(nod);
for(auto vecin: v[nod]){
if(viz[vecin] == false)
{
nivel[vecin] = nivel[nod] + 1;
dfs(vecin);
nivelMin[nod] = min(nivelMin[nod], nivelMin[vecin]);
}
else
if(nivel[nod] - nivel[vecin] >= 2)
{
nivelMin[nod] = min(nivelMin[nod], nivel[vecin]);
}
if(nivelMin[vecin] >= nivel[nod]){
//cout << nod << " --- " << vecin << '\n';
while(s.top() != vecin && (!(s.empty())) && (s.size() > 0))
{
cout << "loop ";
//cout << s.top() << ' ';
s.pop();
cout << s.size() << " ";
}
// cout << nod << ' ';
cout << s.size() << " ";
if(!(s.empty()))
s.pop();
//cout << vecin << ' ';
cout << '\n';
//cout << vecin << " ";
}
}
}
int main()
{
nivel[1] = 1;
int n, m;
in >> n >> m;
for(int i = 0; i < m; ++i)
{
int x, y;
in >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1);
return 0;
}
这是 zao.in 的内容:
8 9
1 2
1 4
2 3
2 5
5 4
5 7
6 7
7 8
8 6
解决方案
推荐阅读
- android - 如何禁用 EditText 上的虚拟键盘?
- python - 如何正确使用 split 和 Beautifulsoup?
- r - 怎么修?'建议有一个行列式大于 1/ ((2*PI)^k) 的协方差矩阵。在 R 中使用 RcppHMM 包时
- sql-server - SSMS 中的随机缩放文本
- python - Discord.py 设置状态(客户端)
- bash - 在 getopts 中为解析的参数启用自动完成
- ios - 如何检查视图控制器是否已在 Swift 中关闭
- typescript - 如何交换对象键值并获得交换函数的有效返回类型?
- r - R:将显示标签更改为其“实际”值
- sql - SQL选择包含间隔中所有值的行