Problem p02366 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, k, n) for (int(i) = (k); (i) < (n); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define pb push_back
#define all(v) begin(v), end(v)
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
template <class T> using vv = vector<vector<T>>;
typedef deque<int> di;
typedef deque<deque<int>> ddi;

vvi edge;
vi prenum;
vi lowest;
vi parent;
int timer;

void dfs(int u, int prev) {
  prenum[u] = timer;
  lowest[u] = timer;
  timer += 1;

  for (auto v : edge[u]) {
    if (prenum[v] < 0) {
      parent[v] = u;
      dfs(v, u);
      lowest[u] = min(lowest[u], lowest[v]);
    } else if (v != prev) {
      lowest[u] = min(lowest[u], prenum[v]);
    }
  }
}

int main() {
  int v, e;
  cin >> v >> e;
  edge.resize(v);
  prenum.resize(v, -1);
  lowest.resize(v);
  parent.resize(v);
  rep(i, e) {
    int s, t;
    cin >> s >> t;
    edge[s].pb(t);
    edge[t].pb(s);
  }
  timer = 0;
  dfs(0, -1);

  int zero_parent = 0;
  set<int> ans;
  FOR(i, 1, v) {
    if (parent[i] == 0) {
      zero_parent += 1;
    } else {
      if (prenum[parent[i]] <= lowest[i]) {
        ans.insert(parent[i]);
      }
    }
  }
  if (zero_parent >= 2) {
    ans.insert(0);
  }
  for (auto i : ans) {
    printf("%d\\n", i);
  }

  return 0;
}

Error: 0
v: 1
v: 2
v: 3
i: 0, parent[i]: 0
prenum[i]: 0, lowest[i]: 0
i: 1, parent[i]: 0
prenum[i]: 1, lowest[i]: 0
i: 2, parent[i]: 1
prenum[i]: 2, lowest[i]: 0
i: 3, parent[i]: 2
prenum[i]: 3, lowest[i]: 3


Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, k, n) for (int(i) = (k); (i) < (n); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define pb push_back
#define all(v) begin(v), end(v)
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
template <class T> using vv = vector<vector<T>>;
typedef deque<int> di;
typedef deque<deque<int>> ddi;

vvi edge;
vi prenum;
vi lowest;
vi parent;
int timer;

void dfs(int u, int prev) {
  prenum[u] = timer;
  lowest[u] = timer;
  timer += 1;

  for (auto v : edge[u]) {
    if (prenum[v] < 0) {
      debug(v);
      parent[v] = u;
      dfs(v, u);
      lowest[u] = min(lowest[u], lowest[v]);
    } else if (v != prev) {
      lowest[u] = min(lowest[u], prenum[v]);
    }
  }
}

int main() {
  int v, e;
  cin >> v >> e;
  edge.resize(v);
  prenum.resize(v, -1);
  lowest.resize(v);
  parent.resize(v);
  rep(i, e) {
    int s, t;
    cin >> s >> t;
    edge[s].pb(t);
    edge[t].pb(s);
  }
  timer = 0;
  dfs(0, -1);
  rep(i, v) {
    debug2(i, parent[i]);
    debug2(prenum[i], lowest[i]);
  }

  int zero_parent = 0;
  set<int> ans;
  FOR(i, 1, v) {
    if (parent[i] == 0) {
      zero_parent += 1;
    } else {
      if (prenum[parent[i]] <= lowest[i]) {
        ans.insert(parent[i]);
      }
    }
  }
  if (zero_parent >= 2) {
    ans.insert(0);
  }
  for (auto i : ans) {
    printf("%d\\n", i);
  }

  return 0;
}