0

I was trying to solve the problem of biconnected components in an unoriented graph. The algorithm itself is not really important here, I am just trying to figure out why do i get the following runtime error 0xC0000005. (I am not even sure if the algorithm is perfectly correct). Whenever I pop from the stack, I check if it is not empty, and the while can not be infinite. Here is the code:

#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;
}

here is the content of zao.in:

8 9
1 2
1 4
2 3
2 5
5 4
5 7
6 7
7 8
8 6
Johnny
  • 447
  • 2
  • 8
  • 3
    `while(s.top() != vecin && (!(s.empty()))` when `s` is empty, then `s.top != vecin` is already undefined behavior. Checking afterwards doesnt help – 463035818_is_not_an_ai Oct 24 '21 at 16:09
  • 1
    `#include `, `#include `, `#include ` in the same code says you don't know what `#include ` does or how to use it. [Here is some reading to help you with that](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – user4581301 Oct 24 '21 at 16:36

0 Answers0