Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll, ll>;

#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846

#define REP(i, m, n) for (ll(i) = (m), (i_len) = (n); (i) < (i_len); ++(i))
#define FORR(i, v) for (auto(i) : v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x)                                                                \
  ASC((x));                                                                    \
  REV((x))
#define pb push_back
#define eb emplace_back

int main() {
  ll N;
  cin >> N;
  vec A(N - 1), B(N - 1);
  vector<vector<ll>> G(N + 1);
  REP(i, 0, N - 1) {
    cin >> A[i] >> B[i];
    G[A[i]].pb(B[i]);
    G[B[i]].pb(A[i]);
  }
  G[0].pb(1);
  G[1].pb(0);

  queue<ll> Q;
  vector<bool> F(N, false);
  vec P(N);
  map<pll, ll> E;
  ll K = 0;
  Q.push(1);
  F[0] = true;
  F[1] = true;
  P[1] = 0;
  while (!Q.empty()) {
    ll v = Q.front();
    Q.pop();
    ll c = 1, pc = E[{P[v], v}];
    FORR(nv, G[v]) {
      if (!F[nv]) {
        if (c == pc)
          ++c;
        E[{v, nv}] = c;
        E[{nv, v}] = c;
        K = MAX(K, c);
        Q.push(nv);
        F[nv] = true;
        P[nv] = v;
        ++c;
      }
    }
  }

  PR(K);
  REP(i, 0, N - 1) cout << E[{A[i], B[i]}] << endl;

  return 0;
}

/*



*/

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll, ll>;

#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846

#define REP(i, m, n) for (ll(i) = (m), (i_len) = (n); (i) < (i_len); ++(i))
#define FORR(i, v) for (auto(i) : v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x)                                                                \
  ASC((x));                                                                    \
  REV((x))
#define pb push_back
#define eb emplace_back

int main() {
  ll N;
  cin >> N;
  vec A(N - 1), B(N - 1);
  vector<vector<ll>> G(N + 1);
  REP(i, 0, N - 1) {
    cin >> A[i] >> B[i];
    G[A[i]].pb(B[i]);
    G[B[i]].pb(A[i]);
  }
  G[0].pb(1);
  G[1].pb(0);

  queue<ll> Q;
  vector<bool> F(N + 1, false);
  vec P(N + 1);
  map<pll, ll> E;
  ll K = 0;
  Q.push(1);
  F[0] = true;
  F[1] = true;
  P[1] = 0;
  while (!Q.empty()) {
    ll v = Q.front();
    Q.pop();
    ll c = 1, pc = E[{P[v], v}];
    FORR(nv, G[v]) {
      if (!F[nv]) {
        if (c == pc)
          ++c;
        E[{v, nv}] = c;
        E[{nv, v}] = c;
        K = MAX(K, c);
        Q.push(nv);
        F[nv] = true;
        P[nv] = v;
        ++c;
      }
    }
  }

  PR(K);
  REP(i, 0, N - 1) cout << E[{A[i], B[i]}] << endl;

  return 0;
}

/*



*/