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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

/// --- Graph Template {{{ ///

template <class T> struct Edge {
  int from, to;
  T cost;
  Edge(int to, T cost) : from(-1), to(to), cost(cost) {}
  Edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
};
template <class T> using WeightedGraph = vector<vector<Edge<T>>>;
using UnWeightedGraph = vector<vector<int>>;

/// }}}--- ///

// SCC(UnWeightedGraph)
// must build(renew)
/// --- Strongly Connected Component {{{ ///

struct SCC {
  int n;
  vector<vector<int>> g, rev;
  vector<int> topo;
  vector<int> used;
  vector<int> comp;
  SCC(int n) : n(n), g(n), rev(n) {}
  SCC(UnWeightedGraph ig) : n(ig.size()), g(n), rev(n) {
    for (int from = 0; from < n; from++)
      for (int to : ig[from])
        addEdge(from, to);
  }
  void addEdge(int a, int b) {
    g[a].emplace_back(b);
    rev[b].emplace_back(a);
  }
  int operator[](int i) { return comp[i]; }
  void build(vector<vector<int>> &renew) {
    used.resize(n, 0);
    comp.resize(n, -1);
    for (int i = 0; i < n; i++)
      if (!used[i])
        dfs1(i);
    reverse(begin(topo), end(topo));
    int k = 0;
    for (int i : topo)
      if (comp[i] == -1)
        dfs2(i, k++);

    renew.resize(k);
    set<pair<int, int>> connect;
    for (int i = 0; i < n; i++) {
      for (int j : g[i]) {
        int x = comp[i], y = comp[j];
        if (x == y)
          continue;
        if (connect.count(make_pair(x, y)))
          continue;
        connect.emplace(x, y);
        renew[x].emplace_back(y);
        // f[x].emplace_back(y);
        // f[y].emplace_back(x);
      }
    }
  }

private:
  void dfs1(int i) {
    if (used[i])
      return;
    used[i] = 1;
    for (int j : g[i])
      dfs1(j);
    topo.emplace_back(i);
  }
  void dfs2(int i, int num) {
    if (comp[i] != -1)
      return;
    comp[i] = num;
    for (int j : rev[i])
      dfs2(j, num);
  }
};

/// ---}}} ///

int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  int n, m;
  cin >> n >> m;
  SCC ecas(n);
  for (int i = 0; i < m; i++) {
    int s, t;
    cin >> s >> t;
    ecas.addEdge(s, t);
  }
  UnWeightedGraph g;
  ecas.build(g);
  int k = g.size();
  if (k == 1) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> ins(k);
  for (int i = 0; i < k; i++) {
    for (int j : g[i])
      ins[j] = 1;
  }
  // vector<int> gr(k, -1);
  // function<void(int, int, int)> dfs = [&](int i, int p, int t) {
  //   gr[i] = t;
  //   for(int j : f[i]) if(j != p) {
  //     dfs(j, i, t);
  //   }
  // };
  // int t = 0;
  // for(int i = 0; i < n; i++) if(gr[i] == -1) dfs(i, -1, t++);
  // にゅうじ0の頂点がわかる
  // vector<int> I(n), O(n);
  int I = 0, O = 0;

  for (int i = 0; i < k; i++)
    if (ins[i] == 0) {
      I++;
    }
  for (int i = 0; i < k; i++)
    if (g[i].size() == 0) {
      O++;
    }
  ll ans = max(I, O);
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

/// --- Graph Template {{{ ///

template <class T> struct Edge {
  int from, to;
  T cost;
  Edge(int to, T cost) : from(-1), to(to), cost(cost) {}
  Edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
};
template <class T> using WeightedGraph = vector<vector<Edge<T>>>;
using UnWeightedGraph = vector<vector<int>>;

/// }}}--- ///

// SCC(UnWeightedGraph)
// must build(renew)
/// --- Strongly Connected Component {{{ ///

struct SCC {
  int n;
  vector<vector<int>> g, rev;
  vector<int> topo;
  vector<int> used;
  vector<int> comp;
  SCC(int n) : n(n), g(n), rev(n) {}
  SCC(UnWeightedGraph ig) : n(ig.size()), g(n), rev(n) {
    for (int from = 0; from < n; from++)
      for (int to : ig[from])
        addEdge(from, to);
  }
  void addEdge(int a, int b) {
    g[a].emplace_back(b);
    rev[b].emplace_back(a);
  }
  int operator[](int i) { return comp[i]; }
  void build(vector<vector<int>> &renew, UnWeightedGraph &f) {
    used.resize(n, 0);
    comp.resize(n, -1);
    for (int i = 0; i < n; i++)
      if (!used[i])
        dfs1(i);
    reverse(begin(topo), end(topo));
    int k = 0;
    for (int i : topo)
      if (comp[i] == -1)
        dfs2(i, k++);

    renew.resize(k);
    f.resize(k);
    set<pair<int, int>> connect;
    for (int i = 0; i < n; i++) {
      for (int j : g[i]) {
        int x = comp[i], y = comp[j];
        if (x == y)
          continue;
        if (connect.count(make_pair(x, y)))
          continue;
        connect.emplace(x, y);
        renew[x].emplace_back(y);
        f[x].emplace_back(y);
        f[y].emplace_back(x);
      }
    }
  }

private:
  void dfs1(int i) {
    if (used[i])
      return;
    used[i] = 1;
    for (int j : g[i])
      dfs1(j);
    topo.emplace_back(i);
  }
  void dfs2(int i, int num) {
    if (comp[i] != -1)
      return;
    comp[i] = num;
    for (int j : rev[i])
      dfs2(j, num);
  }
};

/// ---}}} ///

int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  int n, m;
  cin >> n >> m;
  SCC ecas(n);
  for (int i = 0; i < m; i++) {
    int s, t;
    cin >> s >> t;
    ecas.addEdge(s, t);
  }
  UnWeightedGraph g, f;
  ecas.build(g, f);
  int k = g.size();
  vector<int> ins(k);
  for (int i = 0; i < k; i++) {
    for (int j : g[i])
      ins[j] = 1;
  }
  vector<int> gr(k, -1);
  function<void(int, int, int)> dfs = [&](int i, int p, int t) {
    gr[i] = t;
    for (int j : f[i])
      if (j != p) {
        dfs(j, i, t);
      }
  };
  int t = 0;
  for (int i = 0; i < n; i++)
    if (gr[i] == -1)
      dfs(i, -1, t++);
  // にゅうじ0の頂点がわかる
  // vector<int> I(n), O(n);
  int I = 0, O = 0;

  for (int i = 0; i < k; i++)
    if (ins[i] == 0) {
      I++;
    }
  for (int i = 0; i < k; i++)
    if (g[i].size() == 0) {
      O++;
    }
  ll ans = max(I, O);
  if (ans == 1)
    ans = 0;
  cout << ans << endl;
  return 0;
}