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

#include <bits/stdc++.h>
#define MAX_V 100005
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
int V;
vector<int> G[MAX_V];
vector<int> rG[MAX_V];
vector<int> vs;
bool used[MAX_V];
int cmp[MAX_V];

void add_edge(int from, int to) {
  G[from].push_back(to);
  rG[to].push_back(from);
}

void dfs(int v) {
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++)
    if (!used[G[v][i]])
      dfs(G[v][i]);
  vs.push_back(v);
}

void rdfs(int v, int k) {
  used[v] = true;
  cmp[v] = k;
  for (int i = 0; i < rG[v].size(); i++)
    if (!used[rG[v][i]])
      rdfs(rG[v][i], k);
}

int scc() {
  memset(used, 0, sizeof(used));
  vs.clear();
  for (int v = 0; v < V; v++)
    if (!used[v])
      dfs(v);
  memset(used, 0, sizeof(used));
  int k = 0;
  for (int i = vs.size() - 1; i >= 0; i--)
    if (!used[vs[i]])
      rdfs(vs[i], k++);
  return k;
}

vector<int> G2[MAX_V];
vector<P> G3[MAX_V];
int usede[MAX_V];

void dfs2(int x) {
  for (int i = 0; i < G3[x].size(); i++) {
    if (usede[G3[x][i].second])
      continue;
    usede[G3[x][i].second] = true;
    add_edge(x, G3[x][i].first);
    dfs2(G3[x][i].first);
  }
}

int n, m, w[MAX_V], u, v;
int sum[MAX_V], f[MAX_V];
int used2[MAX_V], ans;

int dfs3(int x) {
  if (used[x])
    return 0;
  used[x] = true;
  int r = 0;
  for (int i = 0; i < G2[x].size(); i++)
    r |= dfs3(G2[x][i]);
  if (f[cmp[x]])
    r = 1;
  if (!used2[cmp[x]] && r) {
    ans += sum[cmp[x]];
    used2[cmp[x]] = 1;
  }
  return r;
}

int maxs = 0;
void dfs4(int x, int s) {
  int flag = 0;
  for (int i = 0; i < G2[x].size(); i++) {
    if (used[G2[x][i]])
      continue;
    used[G2[x][i]] = true;
    flag = 1;
    if (cmp[x] != cmp[G2[x][i]] && !used2[cmp[x]])
      dfs4(G2[x][i], s + sum[cmp[x]]);
    else
      dfs4(G2[x][i], s);
  }
  if (!flag && !used2[cmp[x]])
    maxs = max(maxs, s + sum[cmp[x]]);
}

void solve() {
  dfs2(0);
  int K = scc();
  for (int i = 0; i < V; i++) {
    if (sum[cmp[i]])
      f[cmp[i]] = 1;
    sum[cmp[i]] += w[i];
  }
  memset(used, 0, sizeof(used));
  dfs3(0);
  memset(used, 0, sizeof(used));
  used[0] = true;
  dfs4(0, 0);
  ans += maxs;
  cout << ans << endl;
}

int main() {
  cin >> n >> m;
  V = n;
  for (int i = 0; i < n; i++)
    cin >> w[i];
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    u--, v--;
    G2[u].push_back(v);
    G2[v].push_back(u);
    G3[u].push_back(P(v, i));
    G3[v].push_back(P(u, i));
  }
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define MAX_V 100005
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
int V;
vector<int> G[MAX_V];
vector<int> rG[MAX_V];
vector<int> vs;
bool used[MAX_V];
int cmp[MAX_V];

void add_edge(int from, int to) {
  G[from].push_back(to);
  rG[to].push_back(from);
}

void dfs(int v) {
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++)
    if (!used[G[v][i]])
      dfs(G[v][i]);
  vs.push_back(v);
}

void rdfs(int v, int k) {
  used[v] = true;
  cmp[v] = k;
  for (int i = 0; i < rG[v].size(); i++)
    if (!used[rG[v][i]])
      rdfs(rG[v][i], k);
}

int scc() {
  memset(used, 0, sizeof(used));
  vs.clear();
  for (int v = 0; v < V; v++)
    if (!used[v])
      dfs(v);
  memset(used, 0, sizeof(used));
  int k = 0;
  for (int i = vs.size() - 1; i >= 0; i--)
    if (!used[vs[i]])
      rdfs(vs[i], k++);
  return k;
}

vector<int> G2[MAX_V];
set<P> usede;

void dfs2(int x) {
  for (int i = 0; i < G2[x].size(); i++) {
    if (usede.find(P(x, G2[x][i])) != usede.end())
      continue;
    usede.insert(P(x, G2[x][i]));
    usede.insert(P(G2[x][i], x));
    add_edge(x, G2[x][i]);
    dfs2(G2[x][i]);
  }
}

int n, m, w[MAX_V], u, v;
int sum[MAX_V], f[MAX_V];
int used2[MAX_V], ans;

int dfs3(int x) {
  if (used[x])
    return 0;
  used[x] = true;
  int r = 0;
  for (int i = 0; i < G2[x].size(); i++)
    r |= dfs3(G2[x][i]);
  if (f[cmp[x]])
    r = 1;
  if (!used2[cmp[x]] && r) {
    ans += sum[cmp[x]];
    used2[cmp[x]] = 1;
  }
  return r;
}

int maxs = 0;
void dfs4(int x, int s) {
  int flag = 0;
  for (int i = 0; i < G2[x].size(); i++) {
    if (used[G2[x][i]])
      continue;
    used[G2[x][i]] = true;
    flag = 1;
    if (cmp[x] != cmp[G2[x][i]] && !used2[cmp[x]])
      dfs4(G2[x][i], s + sum[cmp[x]]);
    else
      dfs4(G2[x][i], s);
  }
  if (!flag && !used2[cmp[x]])
    maxs = max(maxs, s + sum[cmp[x]]);
}

void solve() {
  dfs2(0);
  int K = scc();
  for (int i = 0; i < V; i++) {
    if (sum[cmp[i]])
      f[cmp[i]] = 1;
    sum[cmp[i]] += w[i];
  }
  memset(used, 0, sizeof(used));
  dfs3(0);
  memset(used, 0, sizeof(used));
  used[0] = true;
  dfs4(0, 0);
  ans += maxs;
  cout << ans << endl;
}

int main() {
  cin >> n >> m;
  V = n;
  for (int i = 0; i < n; i++)
    cin >> w[i];
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    u--, v--;
    G2[u].push_back(v);
    G2[v].push_back(u);
  }
  solve();
  return 0;
}