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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T> using Graph = vector<vector<T>>;

#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) REP(i, 0, a)
#define EACH(i, a) for (auto i : a)
#define ITR(x, a) for (__typeof(a.begin()) x = a.begin(); x != a.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define HAS(a, x) (a.find(x) != a.end())
#define endl '\\n'
#define to first
#define cost second

int n;
bool used[100005];
vector<pair<int, int>> tr[100005];

pair<int, int> dfs1(int s, int d) {
  used[s] = true;
  pair<int, int> res = {s, d};
  for (auto p : tr[s]) {
    if (used[p.to])
      continue;
    auto tp = dfs1(p.to, d + p.cost);
    if (tp.cost > res.cost)
      res = tp;
  }
  return res;
}

int dfs2(int s) {
  used[s] = true;
  int res = 0;
  for (auto p : tr[s]) {
    if (used[p.to])
      continue;
    res = max(res, dfs2(p.to) + p.cost);
  }
  return res;
}

int diameter() {
  auto p = dfs1(0, 0);
  rep(i, n) used[i] = false;
  return dfs2(p.to);
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;
  rep(i, n - 1) {
    int s, t, w;
    cin >> s >> t >> w;
    tr[s].push_back({t, w});
    tr[t].push_back({s, w});
  }

  cout << diameter() << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = n - 1; i >= (0); i--)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define all(v) (v).begin(), (v).end()
#define eall(v) unique(all(v), v.end())
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll INFF = 1e18;

int n;
int s[100010], t[100010], w[100010];
ll dist[100010];
vector<pair<int, ll>> G[100010];

int dfs1(int u, int par) {
  ll ma = 0;
  for (auto tm : G[u]) {
    int v = tm.fi;
    ll cost = tm.se;
    if (v == par)
      continue;
    chmax(ma, dfs1(v, u) + cost);
  }
  return dist[u] = ma;
}

ll dfs2(int u, int w, int par) {
  vector<pair<int, ll>> child; // (??????, ?????????????????????)
  child.pb(mp(0, -1));
  for (auto tm : G[u]) {
    int v = tm.fi;
    ll cost = tm.se;
    if (v == par)
      child.pb(mp(w + cost, v));
    else
      child.pb(mp(dist[v] + cost, v));
  }
  sort(all(child));
  reverse(all(child));
  ll ret = child[0].fi + child[1].fi;
  for (auto tm : G[u]) {
    int v = tm.fi;
    ll cost = tm.se;
    if (v == par)
      continue;
    if (v == child[0].se)
      chmax(ret, dfs2(v, child[1].fi, u));
    else
      chmax(ret, dfs2(v, child[0].fi, u));
  }
  return ret;
}

int main(void) {
  cin >> n;
  rep(i, n - 1) cin >> s[i] >> t[i] >> w[i];
  rep(i, n - 1) G[s[i]].pb(mp(t[i], w[i]));
  rep(i, n - 1) G[t[i]].pb(mp(s[i], w[i]));
  dfs1(n / 2, -1);
  auto ret = dfs2(n / 2, 0, -1);
  printf("%lld\\n", ret);
  return 0;
}