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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &i : v)
    is >> i;
  return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  const string delimiter = "\\n";
  REP(i, v.size()) {
    os << v[i];
    if (i != v.size() - 1)
      os << delimiter;
  }
  return os;
}
/*--------------------template--------------------*/

typedef int Weight;
struct Edge {
  int from, to;
  Weight cost;
  bool operator<(const Edge &e) const { return cost < e.cost; }
  bool operator>(const Edge &e) const { return cost > e.cost; }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;
void add_edge(Graph &g, int from, int to, Weight cost) {
  g[from].push_back(Edge{from, to, cost});
}

vector<vi> g, tree;
vi w;
vector<bool> can_back;
vi mx;

bool get_can_back(int v) {
  bool res = false;
  if (tree[v].size() + 1 < g[v].size())
    res = true;
  for (auto u : tree[v]) {
    if (get_can_back(u))
      res = u;
  }
  return can_back[v] = res;
}

int get_max(int v) {
  if (mx[v] >= 0)
    return mx[v];
  int res = 0;
  for (auto u : tree[v]) {
    chmax(res, get_max(u));
  }
  return mx[v] = res + w[v];
}

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int n, m;
  cin >> n >> m;
  w.resize(n);
  cin >> w;
  g.resize(n);
  tree.resize(n);
  can_back.resize(n);
  mx.resize(n, -1);
  REP(i, m) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  queue<int> que;
  vector<bool> vis(n);
  vis[0] = true;
  que.push(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (auto u : g[v]) {
      if (vis[u])
        continue;
      vis[u] = true;
      tree[v].push_back(u);
      que.push(u);
    }
  }
  for (auto v : tree[0]) {
    get_can_back(v);
  }
  int ans = w[0];
  int fin = 0;
  FOR(i, 1, n) {
    if (can_back[i])
      ans += w[i];
    else {
      chmax(fin, get_max(i));
    }
  }
  cout << ans + fin << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &i : v)
    is >> i;
  return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  const string delimiter = "\\n";
  REP(i, v.size()) {
    os << v[i];
    if (i != v.size() - 1)
      os << delimiter;
  }
  return os;
}
/*--------------------template--------------------*/

typedef int Weight;
struct Edge {
  int from, to;
  Weight cost;
  bool operator<(const Edge &e) const { return cost < e.cost; }
  bool operator>(const Edge &e) const { return cost > e.cost; }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;
void add_edge(Graph &g, int from, int to, Weight cost) {
  g[from].push_back(Edge{from, to, cost});
}

vector<vi> g, tree;
vi w;
vector<bool> can_back;
vi mx;

bool get_can_back(int v) {
  bool res = false;
  if (tree[v].size() + 1 < g[v].size())
    res = true;
  for (auto u : tree[v]) {
    if (get_can_back(u))
      res = u;
  }

  return can_back[v] = res;
}

int get_max(int v) {
  int res = 0;
  for (auto u : tree[v]) {
    chmax(res, get_max(u));
  }
  return mx[v] = res + w[v];
}

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int n, m;
  cin >> n >> m;
  w.resize(n);
  cin >> w;
  g.resize(n);
  tree.resize(n);
  can_back.resize(n);
  mx.resize(n);
  REP(i, m) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  queue<int> que;
  vector<bool> vis(n);
  vis[0] = true;
  que.push(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (auto u : g[v]) {
      if (vis[u])
        continue;
      vis[u] = true;
      tree[v].push_back(u);
      que.push(u);
    }
  }
  for (auto v : tree[0]) {
    get_can_back(v);
  }
  int ans = w[0];
  int fin = 0;
  FOR(i, 1, n) {
    if (can_back[i])
      ans += w[i];
    else {
      chmax(fin, get_max(i));
    }
  }
  cout << ans + fin << endl;
  return 0;
}