首页 > 解决方案 > 使用堆栈的内存错误代码 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

标签: c++graphstackruntime-error

解决方案


推荐阅读