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

#include <bits/stdc++.h>

using namespace std;

struct UnionFind {
  vector<int> data;

  UnionFind(int sz) { data.assign(sz, -1); }

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return (false);
    if (data[x] > data[y])
      swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }

  int find(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k) { return (-data[find(k)]); }
};

struct LowLink {
  UnionFind uf;
  vector<vector<int>> g;
  vector<int> used, ord, low, parent;

  LowLink(size_t v) : g(v), used(v, 0), ord(v), low(v), uf(v), parent(v, -1) {}

  virtual void add_edge(int x, int y) {
    g[x].push_back(y);
    g[y].push_back(x);
  }

  void dfs(int idx, int &k, int par = -1) {
    used[idx] = true;
    ord[idx] = k++;
    low[idx] = ord[idx];

    for (auto &to : g[idx]) {
      if (!used[to]) {
        dfs(to, k, idx);
        low[idx] = min(low[idx], low[to]);
        parent[to] = idx;
        if (ord[idx] >= low[to])
          uf.unite(idx, to);
      } else if (to != par) {
        low[idx] = min(low[idx], ord[to]);
      }
    }
  }

  void dfs() {
    int k = 0;
    dfs(0, k);
  }
};

struct ArticulationPoints : LowLink {
  ArticulationPoints(size_t v) : LowLink(v) {}

  vector<int> build() {
    LowLink::dfs();
    vector<int> vs;
    int sum = 0;
    for (int i = g.size() - 1; i >= 1; i--) {
      if (parent[i] == 0)
        ++sum;
      else if (ord[parent[i]] <= low[i])
        vs.push_back(parent[i]);
    }
    if (sum > 1)
      vs.push_back(0);
    sort(begin(vs), end(vs));
    vs.erase(unique(begin(vs), end(vs)), end(vs));
    return (vs);
  }
};

struct BiConnectedComponents : LowLink {
  vector<pair<int, int>> edges;
  vector<int> comp;

  BiConnectedComponents(size_t v) : LowLink(v), comp(v) {}

  void add_edge(int x, int y) {
    LowLink::add_edge(x, y);
    edges.push_back(minmax(x, y));
  }

  int operator[](int k) { return (comp[k]); }

  void build(vector<vector<int>> &t) {
    LowLink::dfs();
    int ptr = 0;
    vector<int> cc(g.size());
    for (int i = 0; i < g.size(); i++) {
      if (i == uf.find(i))
        cc[i] = ptr++;
    }

    t.resize(ptr);
    for (int i = 0; i < g.size(); i++) {
      comp[i] = cc[uf.find(i)];
    }
    for (auto &e : edges) {
      int x = comp[e.first], y = comp[e.second];
      if (x == y)
        continue;
      t[x].push_back(y);
      t[y].push_back(x);
    }
  }
};

void solve() {
  int V, E;
  cin >> V >> E;
  ArticulationPoints graph(V);
  vector<vector<int>> latte(V), malta(V);
  for (int i = 0; i < E; i++) {
    int s, t;
    cin >> s >> t;
    graph.add_edge(s, t);
    latte[s].emplace_back(t);
    latte[t].emplace_back(s);
  }
  for (int i : graph.build())
    cout << i << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct UnionFind {
  vector<int> data;

  UnionFind(int sz) { data.assign(sz, -1); }

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return (false);
    if (data[x] > data[y])
      swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }

  int find(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k) { return (-data[find(k)]); }
};

struct BiConnectedComponents {
  UnionFind uf;
  vector<vector<int>> g;
  vector<pair<int, int>> edges;
  vector<int> used, ord, low, comp;

  BiConnectedComponents(size_t v)
      : uf(v), g(v), used(v, 0), comp(v), ord(v), low(v) {}

  void add_edge(int x, int y) {
    g[x].push_back(y);
    g[y].push_back(x);
    edges.push_back(minmax(x, y));
  }

  void dfs(int idx, int &k, int par = -1) {
    used[idx] = true;
    ord[idx] = k++;
    low[idx] = ord[idx];

    for (auto &to : g[idx]) {
      if (!used[to]) {
        dfs(to, k, idx);
        low[idx] = min(low[idx], low[to]);
        if (ord[idx] >= low[to])
          uf.unite(idx, to);
      } else if (to != par) {
        low[idx] = min(low[idx], ord[to]);
      }
    }
  }

  int operator[](int k) { return (comp[k]); }

  size_t size() { return (g.size()); }

  void build(vector<vector<int>> &t) {
    int kk = 0;
    dfs(0, kk);

    int ptr = 0;
    vector<int> cc(g.size());
    for (int i = 0; i < g.size(); i++) {
      if (i == uf.find(i))
        cc[i] = ptr++;
    }

    t.resize(ptr);
    for (int i = 0; i < g.size(); i++) {
      comp[i] = cc[uf.find(i)];
    }
    for (auto &e : edges) {
      int x = comp[e.first], y = comp[e.second];
      if (x == y)
        continue;
      t[x].push_back(y);
      t[y].push_back(x);
    }
  }
};

void solve() {
  int V, E;
  cin >> V >> E;
  BiConnectedComponents graph(V);
  vector<vector<int>> latte(V), malta(V);
  for (int i = 0; i < E; i++) {
    int s, t;
    cin >> s >> t;
    graph.add_edge(s, t);
    latte[s].emplace_back(t);
    latte[t].emplace_back(s);
  }
  graph.build(malta);
  vector<int> st;
  for (int i = 0; i < E; i++) {
    set<int> count;
    for (auto &p : latte[i]) {
      count.emplace(graph[p]);
    }
    if (count.size() > 1)
      st.push_back(i);
  }
  sort(begin(st), end(st));
  for (int i : st)
    cout << i << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
}