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

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

struct edge {
  int to;
  edge(const int &_to) : to(_to) {}
  edge(const edge &e) : to(e.to) {}
};

constexpr int MAX_N = 100000;
vector<int> pre_order(
    MAX_N,
    -1); // pre_order[i]:=??±???????????¢?´¢??§???????????????i????????£?????¨??????timer
vector<int> post_order(
    MAX_N,
    -1); // post_order[i]:=??±???????????¢?´¢??§???????????????i????????£?????¨??????timer
vector<int> parent(
    MAX_N,
    -1); // parent[i]:=??±???????????¢?´¢??§??¨????§????????????¨???????????????i??????
vector<bool> visited(
    MAX_N,
    false); //??±???????????¢?´¢???????????§????¨?????????????????????°???????´?????????????

vector<vector<edge>> G(MAX_N);

void add_edge(int s, int t) {
  G[s].emplace_back(edge(t));
  G[t].emplace_back(edge(s));
}

void dfs(const vector<vector<edge>> &G, int u = 0, int prev_u = -1,
         int timer = 1) {
  pre_order[u] = timer;
  post_order[u] = timer;
  ++timer;

  // u????¨????????????¨????????¢
  visited[u] = true;

  for (int i = 0; i < G[u].size(); ++i) {

    int v = G[u][i].to;

    // u?????£??\\??????????????????v == G[u][i].to ???????¨?
    if (!visited[v]) {

      // v?????????u??§?¢??????????
      parent[v] = u;

      // v??\\????????±???????????¢?´¢????????????
      dfs(G, v, u, timer);

      // u???post_order?????´??°??????
      post_order[u] = min(post_order[u], post_order[v]);
    }

    // u?????£??\\??????????????????v?????¢?¨?????????¢?´¢??§??´????????§????????£??????????????¨?????????
    else if (v != prev_u) {
      post_order[u] = min(post_order[u], pre_order[v]);
    }
  }
}

vector<int> articulation_points(const vector<vector<edge>> &G, int G_sz,
                                int root = 0) {
  dfs(G, root);
  vector<int> ans;
  vector<bool> used(G_sz, false);
  for (int i = 0; i < G_sz; ++i) {

    //??±???????????¢?´¢??§?§????????????¨??????????????????????????????2?????\\????????????????????\\????¢????
    if (i == root) {
      int root_child = 0;
      for (int j = 0; j < G_sz; ++j) {
        if (parent[j] == root)
          ++root_child;
        if (root_child == 2) {
          ans.emplace_back(root);
          break;
        }
      }
    }

    else if (parent[i] != root && pre_order[parent[i]] <= post_order[i] &&
             !used[parent[i]]) {

      ans.emplace_back(parent[i]);
      used[parent[i]] = true;
    }
  }
  sort(ans.begin(), ans.end());
  return ans;
}

int main() {
  int N, E;
  cin >> N >> E;
  int s, t;

  for (int i = 0; i < E; ++i) {
    cin >> s >> t;
    add_edge(s, t);
  }
  vector<int> ans = articulation_points(G, N);
  for (int i = 0; i < ans.size(); ++i) {
    cout << ans[i] << endl;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

struct edge {
  int to;
  edge(const int &_to) : to(_to) {}
  edge(const edge &e) : to(e.to) {}
};

constexpr int MAX_N = 100000;
vector<int> pre_order(
    MAX_N,
    -1); // pre_order[i]:=??±???????????¢?´¢??§???????????????i????????£?????¨??????timer
vector<int> post_order(
    MAX_N,
    -1); // post_order[i]:=??±???????????¢?´¢??§???????????????i????????£?????¨??????timer
vector<int> parent(
    MAX_N,
    -1); // parent[i]:=??±???????????¢?´¢??§??¨????§????????????¨???????????????i??????
vector<bool> visited(
    MAX_N,
    false); //??±???????????¢?´¢???????????§????¨?????????????????????°???????´?????????????

vector<vector<edge>> G;

void add_edge(int s, int t) {
  G[s].emplace_back(edge(t));
  G[t].emplace_back(edge(s));
}

void dfs(const vector<vector<edge>> &G, int u = 0, int prev_u = -1,
         int timer = 1) {
  pre_order[u] = timer;
  post_order[u] = timer;
  ++timer;

  // u????¨????????????¨????????¢
  visited[u] = true;

  for (int i = 0; i < G[u].size(); ++i) {

    int v = G[u][i].to;

    // u?????£??\\??????????????????v == G[u][i].to ???????¨?
    if (!visited[v]) {

      // v?????????u??§?¢??????????
      parent[v] = u;

      // v??\\????????±???????????¢?´¢????????????
      dfs(G, v, u, timer);

      // u???post_order?????´??°??????
      post_order[u] = min(post_order[u], post_order[v]);
    }

    // u?????£??\\??????????????????v?????¢?¨?????????¢?´¢??§??´????????§????????£??????????????¨?????????
    else if (v != prev_u) {
      post_order[u] = min(post_order[u], pre_order[v]);
    }
  }
}

vector<int> articulation_points(const vector<vector<edge>> &G, int G_sz,
                                int root = 0) {
  dfs(G, root);
  vector<int> ans;
  for (int i = 0; i < G_sz; ++i) {

    //??±???????????¢?´¢??§?§????????????¨??????????????????????????????2?????\\????????????????????\\????¢????
    if (i == root) {
      int root_child = 0;
      for (int j = 0; j < G_sz; ++j) {
        if (parent[j] == root)
          ++root_child;
        if (root_child == 2) {
          ans.emplace_back(root);
          break;
        }
      }
    }

    else if (pre_order[parent[i]] <= post_order[i]) {
      ans.emplace_back(parent[i]);
    }
  }
  return ans;
}

int main() {
  int N, E;
  cin >> N >> E;
  int s, t;

  for (int i = 0; i < E; ++i) {
    cin >> s >> t;
    add_edge(s, t);
  }
  vector<int> ans = articulation_points(G, N);
  for (int i = 0; i < ans.size(); ++i) {
    cout << ans[i] << endl;
  }
}