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

#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> res;
vector<int> temp;
stack<int> st;
#define pb push_back
#define vi vector<int>

const int inf = 2e5 + 5;

vector<int> adj[inf];
vector<int> rev[inf];

void dfs(vi &vis, int s) {
  vis[s] = 1;
  bool ok = true;
  for (auto x : adj[s]) {
    if (vis[x] == 0) {
      ok = false;
      dfs(vis, x);
    }
  }
  st.push(s);
}

void dfs1(vi &vis, int s) {
  vis[s] = 1;
  temp.pb(s);
  for (auto x : rev[s]) {
    if (vis[x] == 0) {
      dfs1(vis, x);
    }
  }
}
void kosaraju(int nd) {
  res.clear();
  temp.clear();
  vi vis(nd);
  for (int i = 0; i < nd; i++) {
    if (vis[i] == 0) {
      dfs(vis, i);
    }
  }
  vis.clear();
  vis.resize(nd);
  fill(vis.begin(), vis.end(), 0);

  for (int i = 0; i < nd; i++) {
    for (auto x : adj[i]) {
      rev[x].pb(i);
    }
  }
  while (!st.empty()) {
    int i = st.top();
    st.pop();
    if (!vis[i]) {
      dfs1(vis, i);
      res.pb(temp);
      temp.clear();
    }
  }
  // return res;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  kosaraju(n);
  int ss = res.size();
  vi boxes;
  for (int i = 0; i < ss; i++) {
    boxes.pb(res[i].size());
  }
  /*for(auto x:boxes){
      cout<<x<<" ";
  }*/
  // cout<<endl;
  sort(boxes.begin(), boxes.end());
  long ans = 0;
  int comp = 0;
  for (int i = 0; i < boxes.size(); i++) {
    if (i == 0) {
      comp += boxes[i];
      ans += boxes[i];
    } else {
      if (boxes[i] > comp) {
        ans += boxes[i] - comp;
        comp += boxes[i] - comp;
        // ans+=boxes[i]-comp;
      }
    }
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> res;
// vi temp;
vector<int> temp;
stack<int> st;
#define pb push_back
#define vi vector<int>

const int inf = 350000;

void dfs(vi adj[], vi &vis, int s) {
  vis[s] = 1;
  bool ok = true;
  for (auto x : adj[s]) {
    if (vis[x] == 0) {
      ok = false;
      dfs(adj, vis, x);
    }
  }
  st.push(s);
}

void dfs1(vi rev[], vi &vis, int s) {
  vis[s] = 1;
  temp.pb(s);
  for (auto x : rev[s]) {
    if (vis[x] == 0) {
      dfs1(rev, vis, x);
    }
  }
}
void kosaraju(int nd, vector<int> adj[]) {
  res.clear();
  temp.clear();
  vi vis(nd);
  vector<int> rev[6000];
  for (int i = 0; i < nd; i++) {
    if (vis[i] == 0) {
      dfs(adj, vis, i);
    }
  }
  vis.clear();
  vis.resize(nd);
  fill(vis.begin(), vis.end(), 0);

  for (int i = 0; i < nd; i++) {
    for (auto x : adj[i]) {
      rev[x].pb(i);
    }
  }
  while (!st.empty()) {
    int i = st.top();
    st.pop();
    if (!vis[i]) {
      dfs1(rev, vis, i);
      res.pb(temp);
      temp.clear();
    }
  }
  // return res;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> adj[n];
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  kosaraju(n, adj);
  int ss = res.size();
  vi boxes;
  for (int i = 0; i < ss; i++) {
    boxes.pb(res[i].size());
  }
  /*for(auto x:boxes){
      cout<<x<<" ";
  }*/
  cout << endl;
  sort(boxes.begin(), boxes.end());
  long ans = 0;
  int comp = 0;
  for (int i = 0; i < boxes.size(); i++) {
    if (i == 0) {
      comp += boxes[i];
      ans += boxes[i];
    } else {
      if (boxes[i] > comp) {
        ans += boxes[i] - comp;
        comp += boxes[i] - comp;
        // ans+=boxes[i]-comp;
      }
    }
  }
  cout << ans;
  return 0;
}