Problem p01815 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
struct BiconectedGraph {
  int n;
  vector<vector<int>> G, C, T;
  vector<int> ord, low, belong;
  vector<P> B;
  BiconectedGraph() {}
  BiconectedGraph(int sz) : n(sz), G(sz), C(sz), T(sz) {}

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

  void input(int m, int offset = 0) {
    int a, b;
    for (int i = 0; i < m; i++) {
      cin >> a >> b;
      add_edge(a + offset, b + offset);
    }
  }

  bool is_bridge(int u, int v) {
    if (ord[u] > ord[v])
      swap(u, v);
    return ord[u] < low[v];
  }

  void dfs(int v, int p, int &k) {
    ord[v] = low[v] = k;
    ++k;
    for (int u : G[v]) {
      if (u == p)
        continue;
      if (ord[u] >= 0) {
        low[v] = min(low[v], ord[u]);
      } else {
        dfs(u, v, k);
        low[v] = min(low[v], low[u]);
      }
      if (is_bridge(u, v))
        B.push_back(P(u, v));
    }
  }

  void fill_component(int c, int v) {
    C[c].push_back(v);
    belong[v] = c;
    for (int u : G[v]) {
      if (belong[u] >= 0 || is_bridge(u, v))
        continue;
      fill_component(c, u);
    }
  }

  void add_component(int v, int &k) {
    if (belong[v] >= 0)
      return;
    fill_component(k++, v);
  }

  int build() {
    int k = 0;
    ord.resize(n);
    low.resize(n);
    belong.resize(n);
    fill(ord.begin(), ord.end(), -1);
    fill(belong.begin(), belong.end(), -1);
    for (int v = 0; v < n; v++) {
      if (ord[v] >= 0)
        continue;
      dfs(v, -1, k);
    }
    k = 0;
    for (int i = 0; i < (int)B.size(); i++) {
      add_component(B[i].first, k);
      add_component(B[i].second, k);
    }
    for (int v = 0; v < n; v++)
      add_component(v, k);
    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);
    }
    return k;
  }
};
// END CUT HERE

int ans = 0;

void dfs(int x, vector<int> &used, vector<vector<int>> &v, map<int, int> &m,
         int A) {
  used[x] = 1;
  int res = 0;
  int f = 0;
  int now = m[x];
  r(i, v[x].size()) {
    if (used[v[x][i]])
      continue;
    dfs(v[x][i], used, v, m, A + now);
    f++;
  }
  if (f == 0) {
    ans = max(A + now, ans);
  }
  return;
}

int n, m, w[100009], x[100009], y[1000009];

signed main() {
  cin >> n >> m;
  r(i, n) cin >> w[i];
  BiconectedGraph bcc(n);
  set<P> tt;
  r(i, m) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
    if (x[i] == y[i])
      return 0;
    bcc.add_edge(x[i], y[i]);
    if (tt.count(P(x[i], y[i])) || tt.count(P(y[i], x[i])))
      exit(1);
    tt.insert(P(x[i], y[i]));
  }
  bcc.build();
  int cnt = 0;
  map<int, int> M, count, wei;
  set<int> st;
  r(i, n) {
    if (!st.count(bcc.belong[i])) {
      st.insert(bcc.belong[i]);
      count[cnt]++;
      wei[cnt] += w[i];
      M[bcc.belong[i]] = cnt++;
    } else {
      wei[M[bcc.belong[i]]] += w[i];
      count[M[bcc.belong[i]]]++;
    }
  }

  BiconectedGraph B(cnt + 1);
  int root = M[bcc.belong[0]];
  vector<P> hen;
  r(i, m) {
    if (bcc.belong[x[i]] == bcc.belong[y[i]])
      continue;
    B.add_edge(M[bcc.belong[x[i]]], M[bcc.belong[y[i]]]);
    hen.push_back(P(M[bcc.belong[x[i]]], M[bcc.belong[y[i]]]));
  }
  r(i, cnt) {
    if (count[i] > 1)
      if (i != root) {
        B.add_edge(cnt, i);
        hen.push_back(P(cnt, i));
      }
  }
  B.add_edge(root, cnt);
  hen.push_back(P(root, cnt));
  B.build();
  st.clear();
  map<int, int> wei2;
  M.clear();
  int cnt2 = 0;
  r(i, cnt + 1) {
    if (!st.count(B.belong[i])) {
      st.insert(B.belong[i]);
      wei2[cnt2] += wei[i];
      M[B.belong[i]] = cnt2++;
    } else {
      wei2[M[B.belong[i]]] += wei[i];
    }
  }
  int ii = 0;
  vector<vector<int>> v(cnt2);
  r(i, hen.size()) {
    int x = hen[i].first;
    int y = hen[i].second;
    if (B.belong[x] == B.belong[y])
      continue;
    x = M[B.belong[x]];
    y = M[B.belong[y]];
    v[x].push_back(y);
    v[y].push_back(x);
    ii++;
  }
  root = M[B.belong[root]];
  vector<int> used(cnt2, 0);
  dfs(root, used, v, wei2, 0);
  cout << root + ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
struct BiconectedGraph {
  int n;
  vector<vector<int>> G, C, T;
  vector<int> ord, low, belong;
  vector<P> B;
  BiconectedGraph() {}
  BiconectedGraph(int sz) : n(sz), G(sz), C(sz), T(sz) {}

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

  void input(int m, int offset = 0) {
    int a, b;
    for (int i = 0; i < m; i++) {
      cin >> a >> b;
      add_edge(a + offset, b + offset);
    }
  }

  bool is_bridge(int u, int v) {
    if (ord[u] > ord[v])
      swap(u, v);
    return ord[u] < low[v];
  }

  void dfs(int v, int p, int &k) {
    ord[v] = low[v] = k;
    ++k;
    for (int u : G[v]) {
      if (u == p)
        continue;
      if (ord[u] >= 0) {
        low[v] = min(low[v], ord[u]);
      } else {
        dfs(u, v, k);
        low[v] = min(low[v], low[u]);
      }
      if (is_bridge(u, v))
        B.push_back(P(u, v));
    }
  }

  void fill_component(int c, int v) {
    C[c].push_back(v);
    belong[v] = c;
    for (int u : G[v]) {
      if (belong[u] >= 0 || is_bridge(u, v))
        continue;
      fill_component(c, u);
    }
  }

  void add_component(int v, int &k) {
    if (belong[v] >= 0)
      return;
    fill_component(k++, v);
  }

  int build() {
    int k = 0;
    ord.resize(n);
    low.resize(n);
    belong.resize(n);
    fill(ord.begin(), ord.end(), -1);
    fill(belong.begin(), belong.end(), -1);
    for (int v = 0; v < n; v++) {
      if (ord[v] >= 0)
        continue;
      dfs(v, -1, k);
    }
    k = 0;
    for (int i = 0; i < (int)B.size(); i++) {
      add_component(B[i].first, k);
      add_component(B[i].second, k);
    }
    for (int v = 0; v < n; v++)
      add_component(v, k);
    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);
    }
    return k;
  }
};
// END CUT HERE

int dfs(int x, vector<int> &used, vector<vector<int>> &v, map<int, int> &m) {
  used[x] = 1;
  int res = 0;
  int now = m[x];
  r(i, v[x].size()) {
    if (used[v[x][i]])
      continue;
    res = max(res, dfs(v[x][i], used, v, m));
  }
  return res + now;
}

int n, m, w[100009], x[100009], y[1000009];

signed main() {
  cin >> n >> m;
  r(i, n) cin >> w[i];
  BiconectedGraph bcc(n);
  r(i, m) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
    bcc.add_edge(x[i], y[i]);
  }
  bcc.build();
  int cnt = 0;
  map<int, int> M, count, wei;
  set<int> st;
  r(i, n) {
    if (!st.count(bcc.belong[i])) {
      st.insert(bcc.belong[i]);
      count[cnt]++;
      wei[cnt] += w[i];
      M[bcc.belong[i]] = cnt++;
    } else {
      wei[M[bcc.belong[i]]] += w[i];
      count[M[bcc.belong[i]]]++;
    }
  }

  BiconectedGraph B(cnt + 1);
  int root = M[bcc.belong[0]];
  vector<P> hen;
  r(i, m) {
    if (bcc.belong[x[i]] == bcc.belong[y[i]])
      continue;
    B.add_edge(M[bcc.belong[x[i]]], M[bcc.belong[y[i]]]);
    hen.push_back(P(M[bcc.belong[x[i]]], M[bcc.belong[y[i]]]));
  }
  r(i, cnt) {
    if (count[i] > 1) {
      B.add_edge(cnt, i);
      hen.push_back(P(cnt, i));
    }
  }
  B.build();
  st.clear();
  map<int, int> wei2;
  M.clear();
  int cnt2 = 0;
  r(i, cnt + 1) {
    if (!st.count(B.belong[i])) {
      st.insert(B.belong[i]);
      wei2[cnt2] += wei[i];
      M[B.belong[i]] = cnt2++;
    } else {
      wei2[M[B.belong[i]]] += wei[i];
    }
  }
  vector<vector<int>> v(cnt2);
  r(i, hen.size()) {
    int x = hen[i].first;
    int y = hen[i].second;
    if (B.belong[x] == B.belong[y])
      continue;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  vector<int> used(cnt2, 0);
  cout << dfs(root, used, v, wei2) << endl;
}