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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
const int M = 1000000007;

vector<vector<int>> edge;
vector<vector<int>> tecomp;
vector<set<int>> nedge;
vector<int> uid, uuid;
vector<int> nw;

void visit(int v, int u, stack<int> &roots, stack<int> &S, vector<bool> &inS,
           vector<int> &num, int &tm) {
  num[v] = ++tm;
  S.push(v);
  inS[v] = true;
  roots.push(v);
  for (int w : edge[v]) {
    if (num[w] == 0)
      visit(w, v, roots, S, inS, num, tm);
    else if (u != w && inS[w])
      while (num[roots.top()] > num[w])
        roots.pop();
  }
  if (v == roots.top()) {
    tecomp.push_back(vector<int>());
    while (1) {
      int w = S.top();
      S.pop();
      inS[w] = false;
      tecomp.back().push_back(w);
      if (v == w)
        break;
    }
    roots.pop();
  }
}

bool dfs(int now, int par, int zero) {
  bool ret = tecomp[now].size() > 1;
  for (int i : nedge[now]) {
    if (i == par)
      continue;
    ret |= dfs(i, now, zero);
  }
  if (ret) {
    uuid[now] = zero;
  }
  return ret;
}
int dfs(int now, int par) {
  int ret = 0;
  for (int i : nedge[now]) {
    if (i == par)
      continue;
    ret = max(ret, dfs(i, now));
  }
  return ret + nw[now];
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> w(n);
  for (int i = 0; i < n; ++i) {
    cin >> w[i];
  }
  edge.resize(n);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  vector<int> num(n, 0);
  vector<bool> inS(n);
  stack<int> roots, S;
  int tm = 0;
  for (int u = 0; u < n; ++u)
    if (num[u] == 0)
      visit(u, n, roots, S, inS, num, tm);

  int nn = tecomp.size();
  uid.resize(n);
  for (int i = 0; i < nn; ++i) {
    for (int j : tecomp[i])
      uid[j] = i;
  }
  int zero = uid[0];
  nedge.resize(nn);
  for (int i = 0; i < n; ++i) {
    for (int j : edge[i]) {
      if (uid[i] != uid[j])
        nedge[uid[i]].insert(uid[j]);
    }
  }
  uuid.resize(n);
  for (int i = 0; i < n; ++i) {
    uuid[i] = i;
  }
  dfs(zero, -1, zero);
  nw.resize(nn, 0);
  for (int i = 0; i < n; ++i) {
    nw[uuid[uid[i]]] += w[i];
  }
  vector<int> useduid(nn, 0);
  vector<int> used(nn, 0);
  int ma = 0;
  for (int i = 0; i < n; ++i) {
    if (!useduid[uid[i]] && uuid[uid[i]] == zero) {
      useduid[uid[i]] = 1;
      for (int j : nedge[uid[i]]) {
        if (uuid[j] != zero && !used[j]) {
          used[j] = 1;
          ma = max(ma, dfs(j, uid[i]));
        }
      }
    }
  }
  cout << (nw[zero] + ma) << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
const int M = 1000000007;

vector<vector<int>> edge;
vector<set<int>> nedge;
vector<int> uid, uuid;
vector<int> vis;
vector<int> viscnt;
vector<int> cnts;
vector<int> nw;
bool dfs(int now, int par, int s) {
  vis[now] = s;
  bool ret = false;
  for (int i : edge[now]) {
    if (i == par)
      continue;
    if (s == 0) {
      ++viscnt[i];
    }
    if (i == s) {
      ret = true;
    } else if (vis[i] < s && uid[i] > s) {
      ret |= dfs(i, now, s);
    }
  }
  if (ret) {
    uid[now] = s;
  }
  return ret;
}
bool dfs(int now, int par) {
  bool ret = cnts[now] > 1;
  for (int i : nedge[now]) {
    if (i == par)
      continue;
    ret |= dfs(i, now);
  }
  if (ret) {
    uuid[now] = 0;
  }
  return ret;
}
int dfs2(int now, int par) {
  int ret = 0;
  for (int i : nedge[now]) {
    if (i == par)
      continue;
    ret = max(ret, dfs2(i, now));
  }
  return ret + nw[now];
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> w(n);
  for (int i = 0; i < n; ++i) {
    cin >> w[i];
  }
  edge.resize(n);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  uid.resize(n);
  vis.resize(n, -1);
  for (int i = 0; i < n; ++i) {
    uid[i] = i;
  }
  viscnt.resize(n, 0);
  cnts.resize(n, 0);
  for (int i = 0; i < n; ++i) {
    if (uid[i] == i) {
      if (i == 0 || viscnt[i] > 1)
        dfs(i, -1, i);
    }
    ++cnts[uid[i]];
  }
  nedge.resize(n);
  for (int i = 0; i < n; ++i) {
    for (int j : edge[i]) {
      if (uid[i] != uid[j]) {
        nedge[uid[i]].insert(uid[j]);
      }
    }
  }
  uuid.resize(n);
  for (int i = 0; i < n; ++i) {
    uuid[i] = i;
  }
  dfs(0, -1);
  nw.resize(n, 0);
  for (int i = 0; i < n; ++i) {
    nw[uuid[uid[i]]] += w[i];
  }
  vector<int> used(n, 0);
  int ma = 0;
  for (int i = 0; i < n; ++i) {
    if (i == uid[i] && uuid[i] == 0) {
      for (int j : nedge[i]) {
        if (uuid[j] > 0 && !used[j]) {
          used[j] = 1;
          ma = max(ma, dfs2(j, i));
        }
      }
    }
  }
  cout << (nw[0] + ma) << "\\n";
  return 0;
}