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

#include <bits/stdc++.h>

using namespace std;

using Weight = long long;

struct Edge {
  int src, dst;
  Edge(int src, int dst) : src(src), dst(dst) {}
};

using Edges = vector<Edge>;
using Graph = vector<Edges>;

int memo[100001];

pair<vector<int>, Edges> bridge(const Graph &g) {
  const int n = g.size();
  int idx = 0, s = 0, t = 0, k = 0;
  vector<int> ord(n, -1), onS(n), stk(n), roots(n), cmp(n);
  Edges brdg;
  function<void(int, int)> dfs = [&](int v, int u) {
    ord[v] = idx++;
    stk[s++] = v;
    onS[v] = true;
    roots[t++] = v;
    for (auto &e : g[v]) {
      int w = e.dst;
      if (ord[w] == -1)
        dfs(w, v);
      else if (u != w && onS[w])
        while (ord[roots[t - 1]] > ord[w])
          --t;
    }
    if (v == roots[t - 1]) {
      brdg.emplace_back(u, v);
      while (true) {
        int w = stk[--s];
        onS[w] = false;
        cmp[w] = k;
        if (v == w)
          break;
      }
      --t;
      ++k;
    }
  };
  for (int u = 0; u < n; ++u) {
    if (ord[u] == -1) {
      dfs(u, n);
      brdg.pop_back();
    }
  }
  return make_pair(cmp, brdg);
}

bool dfs0(const Graph &g, int p, int v, vector<bool> &elect,
          vector<int> &elements, vector<int> &cycle) {
  for (auto e : g[v]) {
    if (e.dst == p)
      continue;
    if (elements[cycle[e.dst]] > 1 || elect[e.dst])
      return elect[v] = 1;
    if (dfs0(g, v, e.dst, elect, elements, cycle))
      return elect[v] = 1;
  }
  return 0;
}

int dfs1(const Graph &g, int p, int v, vector<bool> &elect,
         vector<int> &elements, vector<int> &cycle, vector<Weight> &node) {
  if (memo[v])
    return memo[v];
  int res = 0;
  for (auto e : g[v]) {
    if (elect[e.dst]) {
      return memo[v] = node[v];
    }
    if (e.dst != p && elements[cycle[e.dst]] == 1) {
      res = dfs1(g, v, e.dst, elect, elements, cycle, node);
      if (res) {
        return memo[v] = res + node[v];
      }
    }
  }
  return 0;
}

int main() {
  int n, m;
  cin >> n >> m;
  Graph g(n);
  vector<Weight> node(n);
  for (int i = 0; i < n; ++i) {
    cin >> node[i];
  }
  for (int i = 0; i < m; ++i) {
    int s, t;
    cin >> s >> t;
    --s;
    --t;
    g[s].emplace_back(s, t);
    g[t].emplace_back(t, s);
  }
  vector<int> cycle = bridge(g).first;
  vector<int> elements(n, 0);
  for (int i = 0; i < n; ++i) {
    elements[cycle[i]]++;
  }
  vector<bool> elect(n, false);
  elect[0] = true;
  for (int i = 1; i < n; ++i) {
    if (elements[cycle[i]] > 1) {
      dfs0(g, -1, i, elect, elements, cycle);
    }
  }
  int buf = 0;
  for (int i = 1; i < n; ++i) {
    if (g[i].size() == 1) {
      buf = max(buf, dfs1(g, -1, g[i][0].src, elect, elements, cycle, node));
    }
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (elect[i]) {
      ans += node[i];
    }
  }
  ans += buf;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using Weight = long long;

struct Edge {
  int src, dst;
  Edge(int src, int dst) : src(src), dst(dst) {}
};

using Edges = vector<Edge>;
using Graph = vector<Edges>;

int memo[100001];

pair<vector<int>, Edges> bridge(const Graph &g) {
  const int n = g.size();
  int idx = 0, s = 0, t = 0, k = 0;
  vector<int> ord(n, -1), onS(n), stk(n), roots(n), cmp(n);
  Edges brdg;
  function<void(int, int)> dfs = [&](int v, int u) {
    ord[v] = idx++;
    stk[s++] = v;
    onS[v] = true;
    roots[t++] = v;
    for (auto &e : g[v]) {
      int w = e.dst;
      if (ord[w] == -1)
        dfs(w, v);
      else if (u != w && onS[w])
        while (ord[roots[t - 1]] > ord[w])
          --t;
    }
    if (v == roots[t - 1]) {
      brdg.emplace_back(u, v);
      while (true) {
        int w = stk[--s];
        onS[w] = false;
        cmp[w] = k;
        if (v == w)
          break;
      }
      --t;
      ++k;
    }
  };
  for (int u = 0; u < n; ++u) {
    if (ord[u] == -1) {
      dfs(u, n);
      brdg.pop_back();
    }
  }
  return make_pair(cmp, brdg);
}

int dfs1(const Graph &g, int p, int v, vector<bool> &elect,
         vector<int> &elements, vector<int> &cycle, vector<Weight> &node) {
  if (memo[v])
    return memo[v];
  int res = 0;
  elect[v] = false;
  for (auto e : g[v]) {
    if (elements[cycle[e.dst]] > 1 || e.dst == 0)
      return memo[v] = node[v];
    if (e.dst != p && elements[cycle[e.dst]] == 1) {
      res = dfs1(g, v, e.dst, elect, elements, cycle, node);
    }
  }
  if (res)
    return memo[v] = res + node[v];
  return 0;
}

int main() {
  int n, m;
  cin >> n >> m;
  Graph g(n);
  vector<Weight> node(n);
  for (int i = 0; i < n; ++i) {
    cin >> node[i];
  }
  for (int i = 0; i < m; ++i) {
    int s, t;
    cin >> s >> t;
    --s;
    --t;
    g[s].emplace_back(s, t);
    g[t].emplace_back(t, s);
  }
  vector<int> cycle = bridge(g).first;
  vector<int> elements(n, 0);
  for (int i = 0; i < n; ++i) {
    elements[cycle[i]]++;
  }
  vector<bool> elect(n, true);
  int buf = 0;
  for (int i = 1; i < n; ++i) {
    if (g[i].size() == 1) {
      buf = max(buf, dfs1(g, -1, g[i][0].src, elect, elements, cycle, node));
    }
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (elect[i]) {
      ans += node[i];
    }
  }
  ans += buf;
  cout << ans << endl;
  return 0;
}