Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
// #define INF 810114514
// #define PI 3.141592653589
typedef pair<int, int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \
  do {                                                                         \
    cout << "No" << endl;                                                      \
    return 0;                                                                  \
  } while (false)
#define MM << " " <<
#define Endl endl
std::vector<int> compress(std::vector<int> D) {
  std::map<int, int> G;
  std::vector<int> E, r;
  int s = D.size();
  for (int i = 0; i < s; i++) {
    E.push_back(D[i]);
  }
  sort(E.begin(), E.end());
  int t = 0;
  for (int i = 0; i < s; i++) {
    if (i != 0 && E[i] != E[i - 1]) {
      t++;
    }
    G[E[i]] = t;
  }
  for (int i = 0; i < s; i++) {
    r.push_back(G[D[i]]);
  }
  return r;
}
signed main(void) {
  const int MEM = 114;
  int N;
  int a[MEM], b[MEM];
  cin >> N;
  REP(i, N - 1) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  vector<int> G[MEM], child[MEM];
  int color[MEM], parent[MEM];
  int kai[MEM] = {};
  bool visited[MEM] = {};
  REP(i, N - 1) {
    G[a[i]].push_back(b[i]);
    G[b[i]].push_back(a[i]);
    kai[a[i]]++;
    kai[b[i]]++;
  }

  int root = 0;
  REP(i, N) {
    if (kai[i] == 1) {
      root = i;
      break;
    }
  }
  // cout << root << endl;
  queue<int> P;
  P.push(root);
  while (!P.empty()) {
    int q = P.front();
    P.pop();
    visited[q] = 1;
    REP(i, G[q].size()) {
      if (!visited[G[q][i]]) {
        P.push(G[q][i]);
        child[q].push_back(G[q][i]);
        parent[G[q][i]] = q;
      }
    }
  }
  parent[root] = -1;

  /*REP(i,N){
      cout << i << ": ";
      REP(j,child[i].size()){
          cout << child[i][j] << " ";
      }cout << endl;
  }*/

  P.push(root);
  color[root] = 0;
  while (!P.empty()) {
    int q = P.front();
    P.pop();
    int now = 0;
    if (color[q] == now)
      now++;
    REP(i, child[q].size()) {
      P.push(child[q][i]);
      color[child[q][i]] = now;
      now++;
      if (color[q] == now)
        now++;
    }
  }
  vector<int> Ans;
  REP(i, N - 1) {
    if (a[i] == parent[b[i]]) {
      Ans.push_back(color[b[i]] + 1);
    } else if (b[i] == parent[a[i]]) {
      Ans.push_back(color[a[i]] + 1);
    }
  }
  Ans = compress(Ans);
  int K = 0;
  REP(i, Ans.size()) { K = max(K, Ans[i] + 1); }
  cout << K << endl;
  REP(i, Ans.size()) { cout << Ans[i] + 1 << endl; }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
// #define INF 810114514
// #define PI 3.141592653589
typedef pair<int, int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \
  do {                                                                         \
    cout << "No" << endl;                                                      \
    return 0;                                                                  \
  } while (false)
#define MM << " " <<
#define Endl endl
std::vector<int> compress(std::vector<int> D) {
  std::map<int, int> G;
  std::vector<int> E, r;
  int s = D.size();
  for (int i = 0; i < s; i++) {
    E.push_back(D[i]);
  }
  sort(E.begin(), E.end());
  int t = 0;
  for (int i = 0; i < s; i++) {
    if (i != 0 && E[i] != E[i - 1]) {
      t++;
    }
    G[E[i]] = t;
  }
  for (int i = 0; i < s; i++) {
    r.push_back(G[D[i]]);
  }
  return r;
}
signed main(void) {
  const int MEM = 114514;
  int N;
  int a[MEM], b[MEM];
  cin >> N;
  REP(i, N - 1) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  vector<int> G[MEM], child[MEM];
  int color[MEM], parent[MEM];
  int kai[MEM] = {};
  bool visited[MEM] = {};
  REP(i, N - 1) {
    G[a[i]].push_back(b[i]);
    G[b[i]].push_back(a[i]);
    kai[a[i]]++;
    kai[b[i]]++;
  }

  int root = 0;
  REP(i, N) {
    if (kai[i] == 1) {
      root = i;
      break;
    }
  }
  // cout << root << endl;
  queue<int> P;
  P.push(root);
  while (!P.empty()) {
    int q = P.front();
    P.pop();
    visited[q] = 1;
    REP(i, G[q].size()) {
      if (!visited[G[q][i]]) {
        P.push(G[q][i]);
        child[q].push_back(G[q][i]);
        parent[G[q][i]] = q;
      }
    }
  }
  parent[root] = -1;

  /*REP(i,N){
      cout << i << ": ";
      REP(j,child[i].size()){
          cout << child[i][j] << " ";
      }cout << endl;
  }*/

  P.push(root);
  color[root] = 0;
  while (!P.empty()) {
    int q = P.front();
    P.pop();
    int now = 0;
    if (color[q] == now)
      now++;
    REP(i, child[q].size()) {
      P.push(child[q][i]);
      color[child[q][i]] = now;
      now++;
      if (color[q] == now)
        now++;
    }
  }
  vector<int> Ans;
  REP(i, N - 1) {
    if (a[i] == parent[b[i]]) {
      Ans.push_back(color[b[i]] + 1);
    } else if (b[i] == parent[a[i]]) {
      Ans.push_back(color[a[i]] + 1);
    }
  }
  Ans = compress(Ans);
  int K = 0;
  REP(i, Ans.size()) { K = max(K, Ans[i] + 1); }
  cout << K << endl;
  REP(i, Ans.size()) { cout << Ans[i] + 1 << endl; }
  return 0;
}