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

#include <bits/stdc++.h>
#define ll long long
#define REP(i, n) for (ll i = 0, max_i = (n); i < max_i; i++)
#define REPI(i, a, b) for (ll i = (a), max_i = (b); i < max_i; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define fi first
#define se second
#define pb push_back
#define debug(x) cerr << #x << ": " << (x) << endl
#define int long long
using namespace std;
using II = pair<int, int>;
using VII = vector<II>;
using VVII = vector<VII>;
using VI = vector<int>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
template <class T = int> inline T in() {
  T x;
  cin >> x;
  return x;
}
template <class T = int> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T = int> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> ostream &operator<<(ostream &s, const vector<T> &d) {
  int n = d.size();
  REP(i, n) s << d[i] << " ";
  return s;
}
template <class T>
ostream &operator<<(ostream &s, const vector<vector<T>> &dd) {
  for (vector<T> d : dd)
    s << d << endl;
  return s;
}
// struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); } } fast;
const int MOD = 1e9 + 7;

signed main() {
  int N = in();
  VVI G(N);
  REP(i, N - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  VVII dist(N); // {len, v}
  VI far(N);
  function<void(int, int)> dfs1 = [&](int v, int prev) {
    for (int u : G[v])
      if (u != prev) {
        dfs1(u, v);
        dist[v].push_back({far[u] + 1, u});
        chmax(far[v], far[u] + 1);
      }
  };
  dfs1(0, -1);

  sort(RALL(dist[0]));
  function<void(int, int)> dfs2 = [&](int v, int prev) {
    for (int u : G[v])
      if (u != prev) {
        int tmp;
        if (dist[v][0].se == u) {
          tmp = dist[v].size() == 1 ? 0 : dist[v][1].fi;
        } else {
          tmp = dist[v][0].fi;
        }
        dist[u].pb({tmp + 1, v});
        sort(RALL(dist[u]));
        dfs2(u, v);
      }
  };
  dfs2(0, -1);

  REP(i, N) { cout << 2 * (N - 1) - dist[i][0].fi << endl; }
}


Error: 0
tmp: 0


Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define REP(i, n) for (ll i = 0, max_i = (n); i < max_i; i++)
#define REPI(i, a, b) for (ll i = (a), max_i = (b); i < max_i; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define fi first
#define se second
#define pb push_back
#define debug(x) cerr << #x << ": " << (x) << endl
#define int long long
using namespace std;
using II = pair<int, int>;
using VII = vector<II>;
using VVII = vector<VII>;
using VI = vector<int>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
template <class T = int> inline T in() {
  T x;
  cin >> x;
  return x;
}
template <class T = int> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T = int> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> ostream &operator<<(ostream &s, const vector<T> &d) {
  int n = d.size();
  REP(i, n) s << d[i] << " ";
  return s;
}
template <class T>
ostream &operator<<(ostream &s, const vector<vector<T>> &dd) {
  for (vector<T> d : dd)
    s << d << endl;
  return s;
}
// struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); } } fast;
const int MOD = 1e9 + 7;

signed main() {
  int N = in();
  VVI G(N);
  REP(i, N - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  VVII dist(N); // {len, v}
  VI far(N);
  function<void(int, int)> dfs1 = [&](int v, int prev) {
    for (int u : G[v])
      if (u != prev) {
        dfs1(u, v);
        dist[v].push_back({far[u] + 1, u});
        chmax(far[v], far[u] + 1);
      }
  };
  dfs1(0, -1);

  sort(RALL(dist[0]));
  function<void(int, int)> dfs2 = [&](int v, int prev) {
    for (int u : G[v])
      if (u != prev) {
        int tmp;
        if (dist[v][0].se == u) {
          tmp = dist[v].size() == 1 ? 0 : dist[v][1].fi;
        } else {
          tmp = dist[v][0].fi;
        }
        debug(tmp);
        dist[u].pb({tmp + 1, v});
        sort(RALL(dist[u]));
        dfs2(u, v);
      }
  };
  dfs2(0, -1);

  REP(i, N) { cout << 2 * (N - 1) - dist[i][0].fi << endl; }
}