Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define int long long
using namespace std;
using P = pair<int, int>;
using ll = long long;
using prique = priority_queue<int>;
const int MOD = 10007;
const int INF = LLONG_MAX / 3;
const int MAX = 710000;

/*-----Library-----*/

struct UnionFind {
  vector<int> par;    // 親ノード
  vector<int> rank;   // ランク
  vector<int> sizeuf; // 要素数
  UnionFind(int n = 1) { init(n); }

  void init(int n = 1) {
    par.resize(n);
    rank.resize(n);
    sizeuf.resize(n);
    for (int i = 0; i < n; ++i) {
      par[i] = i;
      rank[i] = 0;
      sizeuf[i] = 1;
    }
  }

  int root(int x) {
    if (par[x] == x) {
      return x;
    } else {
      int r = root(par[x]);
      return par[x] = r;
    }
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (rank[x] < rank[y])
      swap(x, y);
    if (rank[x] == rank[y])
      ++rank[x];
    par[y] = x;
    sizeuf[x] += sizeuf[y];
    return true;
  }

  int size(int x) { return sizeuf[root(x)]; }
};

long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

long long lcm(long long i, long long j) { return i * (j / gcd(i, j)); }

long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
/*-----Library-----*/
vector<int> es[110000];
map<pair<int, int>, int> c;
bool reached[110000] = {};
void dfs(int s, int co) {
  reached[s] = true;
  int cnt = 1;
  for (int i = 0; i < es[s].size(); i++) {
    if (reached[es[s][i]] == true)
      continue;
    if (cnt == co)
      cnt++;
    c[P(s, es[s][i])] = cnt;
    c[P(es[s][i], s)] = cnt;
    dfs(es[s][i], cnt);
    cnt++;
  }
}
signed main() {
  int n;
  cin >> n;
  int cnt[110000] = {};
  P t[11000];
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    cnt[a - 1]++;
    cnt[b - 1]++;
    es[a - 1].push_back(b - 1);
    es[b - 1].push_back(a - 1);
    t[i] = P(a - 1, b - 1);
  }
  int k = 0;
  for (int i = 0; i < n; i++) {
    k = max(k, cnt[i]);
  }
  cout << k << endl;
  dfs(0, 0);
  for (int i = 0; i < n - 1; i++) {
    cout << c[P(t[i].first, t[i].second)] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define int long long
using namespace std;
using P = pair<int, int>;
using ll = long long;
using prique = priority_queue<int>;
const int MOD = 10007;
const int INF = LLONG_MAX / 3;
const int MAX = 710000;

/*-----Library-----*/

struct UnionFind {
  vector<int> par;    // 親ノード
  vector<int> rank;   // ランク
  vector<int> sizeuf; // 要素数
  UnionFind(int n = 1) { init(n); }

  void init(int n = 1) {
    par.resize(n);
    rank.resize(n);
    sizeuf.resize(n);
    for (int i = 0; i < n; ++i) {
      par[i] = i;
      rank[i] = 0;
      sizeuf[i] = 1;
    }
  }

  int root(int x) {
    if (par[x] == x) {
      return x;
    } else {
      int r = root(par[x]);
      return par[x] = r;
    }
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (rank[x] < rank[y])
      swap(x, y);
    if (rank[x] == rank[y])
      ++rank[x];
    par[y] = x;
    sizeuf[x] += sizeuf[y];
    return true;
  }

  int size(int x) { return sizeuf[root(x)]; }
};

long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

long long lcm(long long i, long long j) { return i * (j / gcd(i, j)); }

long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
/*-----Library-----*/
vector<int> es[110000];
map<pair<int, int>, int> c;
bool reached[110000] = {};
void dfs(int s, int co) {
  reached[s] = true;
  int cnt = 1;
  for (int i = 0; i < es[s].size(); i++) {
    if (reached[es[s][i]] == true)
      continue;
    if (cnt == co)
      cnt++;
    c[P(s, es[s][i])] = cnt;
    c[P(es[s][i], s)] = cnt;
    dfs(es[s][i], cnt);
    cnt++;
  }
}
signed main() {
  int n;
  cin >> n;
  int cnt[110000] = {};
  P t[110000];
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    cnt[a - 1]++;
    cnt[b - 1]++;
    es[a - 1].push_back(b - 1);
    es[b - 1].push_back(a - 1);
    t[i] = P(a - 1, b - 1);
  }
  int k = 0;
  for (int i = 0; i < n; i++) {
    k = max(k, cnt[i]);
  }
  cout << k << endl;
  dfs(0, 0);
  for (int i = 0; i < n - 1; i++) {
    cout << c[P(t[i].first, t[i].second)] << endl;
  }
  return 0;
}