Problem p03654 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define rep(i, j, k) for (register int i = j; i <= k; i++)
#define dow(i, j, k) for (register int i = j; i >= k; i--)
#define fi first
#define se second
// #define P pair<ull,ull>
// #define PP pair<int,pair<int,int> >
// #define mp(a,b) make_pair(a,b)
#define ll long long
#define ull unsigned long long
using namespace std;

inline int read() {
  int s = 0, t = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      t = -1;
    c = getchar();
  }
  while (isdigit(c))
    s = s * 10 + c - 48, c = getchar();
  return s * t;
}

const int N = 1e5 + 7, p = 1e9 + 7;
struct edge {
  int to, v, nxt;
} e[N];
int head[N], cnt = 0;
inline void add(int v, int y, int x) {
  e[++cnt].to = y, e[cnt].v = v, e[cnt].nxt = head[x], head[x] = cnt;
  e[++cnt].to = x, e[cnt].v = v, e[cnt].nxt = head[y], head[y] = cnt;
}
ll ans = 0;
int n, q[N], sz[N], g[N], f[N], l, minl = N;
#define ez(i, j) for (register int i = head[j]; i; i = e[i].nxt)
#define to e[i].to
inline void dfs(int x, int fa = 0) {
  sz[x] = 1;
  ez(i, x) if (to != fa) dfs(to, x);
  ez(i, x) if (to != fa) {
    ans += 2ll * min(n - sz[to], sz[to]) * e[i].v;
    sz[x] += sz[to];
    g[x] = max(sz[to], g[x]);
  }
  g[x] = max(g[x], n - sz[x]);
  if (g[x] < minl)
    minl = g[x], f[l = 1] = x;
  else if (g[x] == minl)
    f[++l] = x;
}

int main() {
  n = read();
  rep(i, 1, n - 1) add(read(), read(), read());
  dfs(1);
  if (l == 2) {
    ez(i, f[1]) if (to == f[2]) ans -= e[i].v;
  } else {
    int minl = 1e8;
    ez(i, f[1]) minl = min(minl, e[i].v);
    ans -= minl;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define rep(i, j, k) for (register int i = j; i <= k; i++)
#define dow(i, j, k) for (register int i = j; i >= k; i--)
#define fi first
#define se second
// #define P pair<ull,ull>
// #define PP pair<int,pair<int,int> >
// #define mp(a,b) make_pair(a,b)
#define ll long long
#define ull unsigned long long
using namespace std;

inline int read() {
  int s = 0, t = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      t = -1;
    c = getchar();
  }
  while (isdigit(c))
    s = s * 10 + c - 48, c = getchar();
  return s * t;
}

const int N = 1e5 + 7, p = 1e9 + 7;
struct edge {
  int to, v, nxt;
} e[N << 1];
int head[N], cnt = 0;
inline void add(int v, int y, int x) {
  e[++cnt].to = y, e[cnt].v = v, e[cnt].nxt = head[x], head[x] = cnt;
  e[++cnt].to = x, e[cnt].v = v, e[cnt].nxt = head[y], head[y] = cnt;
}
ll ans = 0;
int n, q[N], sz[N], g[N], f[N], l, minl = N;
#define ez(i, j) for (register int i = head[j]; i; i = e[i].nxt)
#define to e[i].to
inline void dfs(int x, int fa = 0) {
  sz[x] = 1;
  ez(i, x) if (to != fa) dfs(to, x);
  ez(i, x) if (to != fa) {
    ans += 2ll * min(n - sz[to], sz[to]) * e[i].v;
    sz[x] += sz[to];
    g[x] = max(sz[to], g[x]);
  }
  g[x] = max(g[x], n - sz[x]);
  if (g[x] < minl)
    minl = g[x], f[l = 1] = x;
  else if (g[x] == minl)
    f[++l] = x;
}

int main() {
  n = read();
  rep(i, 1, n - 1) add(read(), read(), read());
  dfs(1);
  if (l == 2) {
    ez(i, f[1]) if (to == f[2]) ans -= e[i].v;
  } else {
    int minl = 1e8;
    ez(i, f[1]) minl = min(minl, e[i].v);
    ans -= minl;
  }
  cout << ans << endl;
  return 0;
}