Problem p01080 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, int> tri;
typedef vector<int> vi;

vector<vector<pii>> d;
vi ans;

void dfs1(const vector<vi> &t) {
  int v = 0, p = -1, out = 0;
  stack<tri> s;
  s.push(tri(pii(v, p), out));

  while (not s.empty()) {
    tri x = s.top();
    s.pop();
    v = x.first.first, p = x.first.second, out = x.second;

    if (out) {
      int cnt = 0;
      for (int u : t[v]) {
        if (p == u)
          continue;
        pii tmp(1, u);
        if (d[u].size())
          tmp.first = d[u][0].first + 1;
        d[v].push_back(tmp);
        cnt++;
      }
      sort(d[v].begin(), d[v].end());
      reverse(d[v].begin(), d[v].end());
    } else {
      s.push(tri(pii(v, p), 1));
      for (int u : t[v]) {
        if (p == u)
          continue;
        s.push(tri(pii(u, v), 0));
      }
    }
  }
}

void dfs2(const vector<vi> &t) {
  int v = 0, p = -1, len = 0;
  stack<tri> s;
  s.push(tri(pii(v, p), len));

  while (not s.empty()) {
    tri x = s.top();
    s.pop();
    v = x.first.first, p = x.first.second, len = x.second;

    int res = len;
    if (d[v].size())
      res = max(res, d[v][0].first);
    ans[v] = res;

    for (int u : t[v]) {
      if (p == u)
        continue;
      int tmp = len;
      if (d[v].size() >= 2) {
        if (d[v][0].second == u) {
          tmp = max(tmp, d[v][1].first);
        } else {
          tmp = max(tmp, d[v][0].first);
        }
      }

      s.push(tri(pii(u, v), tmp + 1));
    }
  }
}

int main() {
  int n;
  cin >> n;

  vector<vi> t(n);
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    t[u].push_back(v);
    t[v].push_back(u);
  }

  d = vector<vector<pii>>(n);
  dfs1(t);

  ans = vi(n, 0);
  dfs2(t);

  for (int i = 0; i < n; i++) {
    cout << 2 * n - 2 - ans[i] << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define mp make_pair
#define MAX_N 100005

using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;

vector<int> g[MAX_N];
P cnt[MAX_N][5];
bool used[MAX_N];
int ans[MAX_N];

void dfs(int cur) {
  used[cur] = true;

  vector<P> res;
  rep(i, g[cur].size()) {
    int to = g[cur][i];
    if (used[to])
      continue;

    dfs(to);
    res.push_back(mp(cnt[to][0].first + 1, to));
  }

  sort(res.begin(), res.end(), greater<P>());
  rep(i, min((int)res.size(), 2)) { cnt[cur][i] = res[i]; }
}

void solve(int cur, int prev, int len) {
  int res = cnt[cur][0].first;
  int newlen = len;

  ans[cur] = max(res, newlen);

  rep(i, g[cur].size()) {
    int to = g[cur][i];
    if (to == prev)
      continue;
    int x = 0;

    if (to == cnt[cur][0].second) {
      x = cnt[cur][1].first;
    } else {
      x = cnt[cur][0].first;
    }

    solve(to, cur, max(x, newlen) + 1);
  }
}

int main() {
  int N;
  cin >> N;

  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;

    g[a].push_back(b);
    g[b].push_back(a);
  }

  rep(i, N) {
    cnt[i][0].first = 0;
    cnt[i][1].first = 0;

    cnt[i][0].second = -1;
    cnt[i][1].second = -1;
  }

  int root = 0;
  // int root = rand() % N;
  memset(used, 0, sizeof(used));
  dfs(root);

  memset(ans, 0, sizeof(ans));
  solve(0, -1, 0);

  rep(i, N) { cout << 2 * (N - 1) - ans[i] << endl; }

  return 0;
}