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

#include <bits/stdc++.h>
using namespace std;
#define MAX_V 114514
typedef pair<int, int> P;
vector<vector<int>> G, C, T;
// vector<int> G[MAX_V],C[MAX_V],T[MAX_V];
vector<int> ord, low, belong;
vector<P> B;
int V, K;
bool is_bridge(int u, int v) {
  if (ord[u] > ord[v])
    swap(u, v);
  return ord[u] < low[v];
}
typedef pair<int, P> PP;
void dfs(int uu, int pp, int d = 0) {
  // cout<<u<<" "<<p<<" "<<d<<endl;
  stack<PP> s;
  s.push(PP(0, P(uu, pp)));
  int u, p, i, v;
  u = uu;
  p = pp;
BEGIN:
  ord[u] = low[u] = K;
  K++;
  for (i = 0; i < (int)G[u].size(); i++) {
    v = G[u][i];
    if (v == p)
      continue;
    if (ord[v] >= 0) {
      low[u] = min(low[u], ord[v]);
    } else {
      s.push(PP(i, P(u, p)));
      p = u;
      u = v;
      goto BEGIN;
    RET:
      PP pp = s.top();
      s.pop();
      i = pp.first;
      u = pp.second.first;
      p = pp.second.first;
      v = G[u][i];
      low[u] = min(low[u], low[v]);
    }
    if (is_bridge(u, v))
      B.push_back(P(u, v));
  }

  if (!s.empty()) {
    goto RET;
  }
}
void fill_component(int c, int u) {
  C[c].push_back(u);
  belong[u] = c;
  for (int v : G[u]) {
    if (belong[v] >= 0 || is_bridge(u, v))
      continue;
    fill_component(c, v);
  }
}
void add_component(int u) {
  if (belong[u] >= 0)
    return;
  C.emplace_back();
  fill_component(C.size() - 1, u);
}

void biconnectedgraph(int n) {
  K = 0;
  ord.clear();
  ord.resize(n, -1);
  low.clear();
  low.resize(n);
  belong.clear();
  belong.resize(n, -1);
  // puts("OK2");
  for (int v = 0; v < n; v++) {
    if (ord[v] >= 0)
      continue;
    // cout<<v<<endl;
    dfs(v, -1);
  }
  // puts("OK3");
  C.clear();
  for (int i = 0; i < (int)B.size(); i++) {
    add_component(B[i].first);
    add_component(B[i].second);
  }
  add_component(0);
  V = C.size();
  T.clear();
  T.resize(V);
  for (int i = 0; i < (int)B.size(); i++) {
    int u = belong[B[i].first], v = belong[B[i].second];
    T[u].push_back(v);
    T[v].push_back(u);
  }
}

void add_edge(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}

static int wei[MAX_V], memo[MAX_V];
static bool used[MAX_V], dele[MAX_V];
bool flag = 0;
int root, xyz;
void dfs2(int v, int p) {
  used[v] = 1;
  if (v != root && (int)C[v].size() > 1) {
    if (v == xyz)
      xyz = root;
    flag = 1;
    dele[v] = 1;
    for (int k : T[v])
      T[root].push_back(k);
    wei[root] += wei[v];
    return;
  }
  for (int i = 0; i < (int)T[v].size(); i++) {
    int u = T[v][i];
    if (u == p)
      continue;
    if (used[u])
      continue;
    dfs2(u, v);
    if (flag) {
      if (~p) {
        if (v == xyz)
          xyz = root;
        dele[v] = 1;
        for (int k : T[v])
          T[root].push_back(k);
        wei[root] += wei[v];
        return;
      } else {
        flag = 0;
      }
    }
  }
}

int dfs3(int v, int p) {
  int res = wei[v];
  for (int u : T[v]) {
    if (dele[u] || u == p)
      continue;
    res = max(res, wei[v] + dfs3(u, v));
  }
  // if(p==xyz) cout<<v<<" "<<res<<endl;
  return res;
}
priority_queue<int> tmp;
int ans = 0;
bool flag2 = 0;
int dfs4(int v, int p) {
  // cout<<v<<" "<<p<<endl;
  if (~memo[v])
    return memo[v];
  if (v == root) {
    flag2 = 1;
    ans += wei[v];
    for (int u : T[v])
      if (u != p)
        tmp.push(dfs3(u, v));
    return 0;
  }
  int res = wei[v];
  for (int u : T[v]) {
    if (dele[u] || u == p)
      continue;
    res = max(res, wei[v] + dfs4(u, v));
    if (flag2) {
      ans += wei[v];
      for (int k : T[v])
        if (u != k && k != p)
          tmp.push(dfs3(k, v));
      break;
    }
  }
  return memo[v] = res;
}
signed main() {
  int n, m;
  cin >> n >> m;
  G.clear();
  G.resize(n);
  int w[n];
  for (int i = 0; i < n; i++)
    cin >> w[i];
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    add_edge(u, v);
  }
  // puts("OK1");
  biconnectedgraph(n);
  // puts("OK");
  root = -1;
  for (int i = 0; i < V; i++) {
    for (int j = 0; j < (int)C[i].size(); j++) {
      wei[i] += w[C[i][j]];
      // w[C[i][j]]=0;
      if (j > 0)
        root = i;
      if (C[i][j] == 0)
        xyz = i;
    }
    // if(C[i].size()>1) cout<<i<<" "<<C[i].size()<<":"<<wei[i]<<endl;
    // cout<<i<<":"<<wei[i]<<endl;
  }
  // for(int i=0;i<n;i++) assert(!w[i]);
  // cout<<C[xyz].size()<<":";for(int i:C[xyz]) cout<<" "<<i;cout<<endl;
  // cout<<wei[xyz]<<endl;
  if (~root) {
    dfs2(root, -1);
    for (int i = 0; i < V; i++)
      assert(used[i]);
  }
  // cout<<root<<"-"<<xyz<<endl;
  if (root < 0 || root == xyz)
    cout << dfs3(xyz, -1) << endl;
  else {
    memset(memo, -1, sizeof(memo));
    dfs4(xyz, -1);
    if (!tmp.empty())
      ans += tmp.top();
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MAX_V 114514
typedef pair<int, int> P;
vector<vector<int>> G, C, T;
// vector<int> G[MAX_V],C[MAX_V],T[MAX_V];
vector<int> ord, low, belong;
vector<P> B;
int V, K;
bool is_bridge(int u, int v) {
  if (ord[u] > ord[v])
    swap(u, v);
  return ord[u] < low[v];
}
void dfs(int u, int p, int d = 0) {
  // cout<<u<<" "<<p<<" "<<d<<endl;
  ord[u] = low[u] = K;
  K++;
  for (int v : G[u]) {
    if (v == p)
      continue;
    if (ord[v] >= 0) {
      low[u] = min(low[u], ord[v]);
    } else {
      dfs(v, u, d + 1);
      low[u] = min(low[u], low[v]);
    }
    if (is_bridge(u, v))
      B.push_back(P(u, v));
  }
}
void fill_component(int c, int u) {
  C[c].push_back(u);
  belong[u] = c;
  for (int v : G[u]) {
    if (belong[v] >= 0 || is_bridge(u, v))
      continue;
    fill_component(c, v);
  }
}
void add_component(int u) {
  if (belong[u] >= 0)
    return;
  C.emplace_back();
  fill_component(C.size() - 1, u);
}

void biconnectedgraph(int n) {
  K = 0;
  ord.clear();
  ord.resize(n, -1);
  low.clear();
  low.resize(n);
  belong.clear();
  belong.resize(n, -1);
  // puts("OK2");
  for (int v = 0; v < n; v++) {
    if (ord[v] >= 0)
      continue;
    // cout<<v<<endl;
    dfs(v, -1);
  }
  // puts("OK3");
  C.clear();
  for (int i = 0; i < (int)B.size(); i++) {
    add_component(B[i].first);
    add_component(B[i].second);
  }
  add_component(0);
  V = C.size();
  T.clear();
  T.resize(V);
  for (int i = 0; i < (int)B.size(); i++) {
    int u = belong[B[i].first], v = belong[B[i].second];
    T[u].push_back(v);
    T[v].push_back(u);
  }
}

void add_edge(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}

int wei[MAX_V], memo[MAX_V];
bool used[MAX_V], dele[MAX_V];
bool flag = 0;
int root;
int xyz;
void dfs2(int v, int p) {
  used[v] = 1;
  if (v != root && (int)C[v].size() > 1) {
    if (v == xyz)
      xyz = root;
    flag = 1;
    dele[v] = 1;
    for (int k : T[v])
      T[root].push_back(k);
    wei[root] += wei[v];
    return;
  }
  for (int i = 0; i < (int)T[v].size(); i++) {
    int u = T[v][i];
    if (u == p)
      continue;
    if (used[u])
      continue;
    dfs2(u, v);
    if (flag) {
      if (~p) {
        if (v == xyz)
          xyz = root;
        dele[v] = 1;
        for (int k : T[v])
          T[root].push_back(k);
        wei[root] += wei[v];
        return;
      } else {
        flag = 0;
      }
    }
  }
}

int dfs3(int v, int p) {
  int res = wei[v];
  for (int u : T[v]) {
    if (dele[u] || u == p)
      continue;
    res = max(res, wei[v] + dfs3(u, v));
  }
  // if(p==xyz) cout<<v<<" "<<res<<endl;
  return res;
}
priority_queue<int> tmp;
int ans = 0;
bool flag2 = 0;
int dfs4(int v, int p) {
  // cout<<v<<" "<<p<<endl;
  if (~memo[v])
    return memo[v];
  if (v == root) {
    flag2 = 1;
    ans += wei[v];
    for (int u : T[v])
      if (u != p)
        tmp.push(dfs3(u, v));
    return 0;
  }
  int res = wei[v];
  for (int u : T[v]) {
    if (dele[u] || u == p)
      continue;
    res = max(res, wei[v] + dfs4(u, v));
    if (flag2) {
      ans += wei[v];
      for (int k : T[v])
        if (u != k && k != p)
          tmp.push(dfs3(k, v));
      break;
    }
  }
  return memo[v] = res;
}
signed main() {
  int n, m;
  cin >> n >> m;
  G.clear();
  G.resize(n);
  int w[n];
  for (int i = 0; i < n; i++)
    cin >> w[i];
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    add_edge(u, v);
  }
  // puts("OK1");
  biconnectedgraph(n);
  // puts("OK");
  root = -1;
  for (int i = 0; i < V; i++) {
    for (int j = 0; j < (int)C[i].size(); j++) {
      wei[i] += w[C[i][j]];
      // w[C[i][j]]=0;
      if (j > 0)
        root = i;
      if (C[i][j] == 0)
        xyz = i;
    }
    // if(C[i].size()>1) cout<<i<<" "<<C[i].size()<<":"<<wei[i]<<endl;
    // cout<<i<<":"<<wei[i]<<endl;
  }
  // for(int i=0;i<n;i++) assert(!w[i]);
  // cout<<C[xyz].size()<<":";for(int i:C[xyz]) cout<<" "<<i;cout<<endl;
  // cout<<wei[xyz]<<endl;
  if (~root)
    dfs2(root, -1);
  for (int i = 0; i < V; i++)
    assert(used[i]);
  // cout<<root<<"-"<<xyz<<endl;
  if (root == xyz)
    cout << dfs3(xyz, -1) << endl;
  else {
    memset(memo, -1, sizeof(memo));
    dfs4(xyz, -1);
    if (!tmp.empty())
      ans += tmp.top();
    cout << ans << endl;
  }
  return 0;
}