Problem p03654 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define llong long long
#define ldouble long double
#define uint unsigned int
#define ullong unsigned long long
#define udouble unsigned double
#define uldouble unsigned long double
#define modinc(x)                                                              \
  {                                                                            \
    if (x >= P)                                                                \
      x -= P;                                                                  \
  }
#define pii pair<int, int>
#define piii pair<pair<int, int>, int>
#define piiii pair<pair<int, int>, pair<int, int>>
#define pli pair<llong, int>
#define pll pair<llong, llong>
#define Memset(a, x)                                                           \
  { memset(a, x, sizeof(a)); }
using namespace std;

const int N = 1e5;
struct Edge {
  int nxt, v;
  llong w;
} e[(N << 1) + 2];
int sz[N + 2];
int fe[N + 2];
int mxs[N + 2];
int fa[N + 2];
int cnt[N + 2];
int n, en, g1, g2;

void addedge(int u, int v, llong w) {
  en++;
  e[en].v = v;
  e[en].w = w;
  e[en].nxt = fe[u];
  fe[u] = en;
}

void dfs(int u) {
  sz[u] = 1;
  for (int i = fe[u]; i; i = e[i].nxt) {
    if (e[i].v == fa[u])
      continue;
    fa[e[i].v] = u;
    dfs(e[i].v);
    cnt[i] = min(sz[e[i].v], n - sz[e[i].v]);
    sz[u] += sz[e[i].v];
    mxs[u] = max(mxs[u], sz[e[i].v]);
  }
  mxs[u] = max(mxs[u], n - sz[u] - 1);
  if ((sz[u] << 1) == n) {
    g1 = u;
    g2 = fa[u];
  } else if ((mxs[u] << 1) < n) {
    g1 = u;
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int x, y;
    llong z;
    scanf("%d%d%lld", &x, &y, &z);
    addedge(x, y, z);
    addedge(y, x, z);
  }
  dfs(1);
  llong ans = 0ll;
  for (int i = 1; i <= en; i++)
    ans += 2ll * cnt[i] * e[i].w;
  if (g2 == 0) {
    llong tmp = ans;
    for (int i = fe[g1]; i; i = e[i].nxt) {
      tmp = min(tmp, e[i].w);
    }
    printf("%lld\n", ans - tmp);
  } else {
    llong tmp = 0ll;
    for (int i = fe[g1]; i; i = e[i].nxt) {
      if (e[i].v == g2) {
        tmp = e[i].w;
        break;
      }
    }
    printf("%lld\n", ans - tmp);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define llong long long
#define ldouble long double
#define uint unsigned int
#define ullong unsigned long long
#define udouble unsigned double
#define uldouble unsigned long double
#define modinc(x)                                                              \
  {                                                                            \
    if (x >= P)                                                                \
      x -= P;                                                                  \
  }
#define pii pair<int, int>
#define piii pair<pair<int, int>, int>
#define piiii pair<pair<int, int>, pair<int, int>>
#define pli pair<llong, int>
#define pll pair<llong, llong>
#define Memset(a, x)                                                           \
  { memset(a, x, sizeof(a)); }
using namespace std;

const int N = 1e5;
struct Edge {
  int nxt, v;
  llong w;
} e[(N << 1) + 2];
int sz[N + 2];
int fe[N + 2];
int mxs[N + 2];
int fa[N + 2];
int cnt[(N << 1) + 2];
int n, en, g1, g2;

void addedge(int u, int v, llong w) {
  en++;
  e[en].v = v;
  e[en].w = w;
  e[en].nxt = fe[u];
  fe[u] = en;
}

void dfs(int u) {
  sz[u] = 1;
  for (int i = fe[u]; i; i = e[i].nxt) {
    if (e[i].v == fa[u])
      continue;
    fa[e[i].v] = u;
    dfs(e[i].v);
    cnt[i] = min(sz[e[i].v], n - sz[e[i].v]);
    sz[u] += sz[e[i].v];
    mxs[u] = max(mxs[u], sz[e[i].v]);
  }
  mxs[u] = max(mxs[u], n - sz[u] - 1);
  if ((sz[u] << 1) == n) {
    g1 = u;
    g2 = fa[u];
  } else if ((mxs[u] << 1) < n) {
    g1 = u;
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int x, y;
    llong z;
    scanf("%d%d%lld", &x, &y, &z);
    addedge(x, y, z);
    addedge(y, x, z);
  }
  dfs(1);
  llong ans = 0ll;
  for (int i = 1; i <= en; i++)
    ans += 2ll * cnt[i] * e[i].w;
  if (g2 == 0) {
    llong tmp = ans;
    for (int i = fe[g1]; i; i = e[i].nxt) {
      tmp = min(tmp, e[i].w);
    }
    printf("%lld\n", ans - tmp);
  } else {
    llong tmp = 0ll;
    for (int i = fe[g1]; i; i = e[i].nxt) {
      if (e[i].v == g2) {
        tmp = e[i].w;
        break;
      }
    }
    printf("%lld\n", ans - tmp);
  }
  return 0;
}