Problem p02850 (C++)

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

/// Typedef
typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define pb push_back
#define ppb pop_back
#define MP make_pair
#define ff first
#define ss second
#define sf scanf
#define pf printf
#define SQR(x) ((x) * (x))
#define loop(i, y) for (int i = 0; i < int(y); i++)
#define FOR(i, x, y) for (int i = int(x); i < int(y); i++)
#define ROF(i, x, y) for (int i = int(x); i >= int(y); i--)
#define all(c) c.begin(), c.end()
#define sz(c) int(c.size())
#define clr(x, y) memset(x, y, sizeof(x))
#define si(x) scanf("%d", &x)
#define sii(x, y) scanf("%d %d", &x, &y)
#define siii(x, y, z) scanf("%d %d %d", &x, &y, &z)
#define sl(x) scanf("%lld", &x)
#define sll(x, y) scanf("%lld %lld", &x, &y)
#define slll(x, y, z) scanf("%lld %lld %lld", &x, &y, &z)
#ifdef VAMP
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " = " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " = " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define dbg(...)
#endif

template <class T> inline T bigMod(T p, T e, T M) {
  ll ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1)
      ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}
template <class T> inline T modInverse(T a, T M) { return bigMod(a, M - 2, M); }
template <class T> inline T gcd(T a, T b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
template <class T> inline T lcm(T a, T b) {
  a = abs(a);
  b = abs(b);
  return (a / gcd(a, b)) * b;
}
template <class T> inline string int2String(T a) {
  ostringstream str;
  str << a;
  return str.str();
}
template <class T> inline T string2int(string a) {
  stringstream str(a);
  T b;
  str >> b;
  return b;
}
template <class T> inline void read(T &r) {
  r = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' or ch > '9') {
    f = (ch == '-' ? -1 : 1), ch = getchar();
  }
  while (ch >= '0' and ch <= '9') {
    r = r * 10 + ch - '0', ch = getchar();
  }
  r *= f;
}

/// Constants
#define MAX 201005
#define MOD 1000000007
#define base 1000000007
#define eps 1e-9
#define INF 1llu << 61 // 2,305,843,009,213,693,952
#define inf (ll)1e18   // 536,870,912
#define PI acos(-1.0)  // 3.1415926535897932
// #define INF              0x3f3f3f3f3f3f3f3f
// #define inf              0x3f3f3f3f
vi G[MAX];
bool use[MAX];
vector<pii> tmp;
map<pii, int> ans;
int main() {
#ifdef VAMP
  clock_t tStart = clock();
  freopen("C:\\Users\\raiha.DESKTOP-5M3GJRN\\Videos\\Raihan\\input.txt", "r",
          stdin);
  freopen("C:\\Users\\raiha.DESKTOP-5M3GJRN\\Videos\\Raihan\\output.txt", "w",
          stdout);
#endif
  int n, u, v;
  si(n);
  FOR(i, 1, n) {
    sii(u, v);
    G[u].pb(v);
    G[v].pb(u);
    tmp.pb(MP(u, v));
  }
  queue<int> q;
  q.push(1);
  use[1] = 1;
  int k = 0;
  vector<int> track(n, 0);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (k < sz(G[u]))
      k = sz(G[u]);
    int cur = 1;
    for (auto v : G[u]) {
      if (use[v])
        continue;
      if (cur == track[u])
        cur++;
      track[v] = ans[MP(u, v)] = ans[MP(v, u)] = cur;
      use[v] = 1;
      cur++;
      q.push(v);
    }
  }
  cout << k << endl;
  for (auto x : tmp)
    cout << ans[x] << endl;
#ifdef VAMP
  printf("\n>>Runtime: %.10fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC);
#endif
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

/// Typedef
typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define pb push_back
#define ppb pop_back
#define MP make_pair
#define ff first
#define ss second
#define sf scanf
#define pf printf
#define SQR(x) ((x) * (x))
#define loop(i, y) for (int i = 0; i < int(y); i++)
#define FOR(i, x, y) for (int i = int(x); i < int(y); i++)
#define ROF(i, x, y) for (int i = int(x); i >= int(y); i--)
#define all(c) c.begin(), c.end()
#define sz(c) int(c.size())
#define clr(x, y) memset(x, y, sizeof(x))
#define si(x) scanf("%d", &x)
#define sii(x, y) scanf("%d %d", &x, &y)
#define siii(x, y, z) scanf("%d %d %d", &x, &y, &z)
#define sl(x) scanf("%lld", &x)
#define sll(x, y) scanf("%lld %lld", &x, &y)
#define slll(x, y, z) scanf("%lld %lld %lld", &x, &y, &z)
#ifdef VAMP
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " = " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " = " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define dbg(...)
#endif

template <class T> inline T bigMod(T p, T e, T M) {
  ll ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1)
      ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}
template <class T> inline T modInverse(T a, T M) { return bigMod(a, M - 2, M); }
template <class T> inline T gcd(T a, T b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
template <class T> inline T lcm(T a, T b) {
  a = abs(a);
  b = abs(b);
  return (a / gcd(a, b)) * b;
}
template <class T> inline string int2String(T a) {
  ostringstream str;
  str << a;
  return str.str();
}
template <class T> inline T string2int(string a) {
  stringstream str(a);
  T b;
  str >> b;
  return b;
}
template <class T> inline void read(T &r) {
  r = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' or ch > '9') {
    f = (ch == '-' ? -1 : 1), ch = getchar();
  }
  while (ch >= '0' and ch <= '9') {
    r = r * 10 + ch - '0', ch = getchar();
  }
  r *= f;
}

/// Constants
#define MAX 201005
#define MOD 1000000007
#define base 1000000007
#define eps 1e-9
#define INF 1llu << 61 // 2,305,843,009,213,693,952
#define inf (ll)1e18   // 536,870,912
#define PI acos(-1.0)  // 3.1415926535897932
// #define INF              0x3f3f3f3f3f3f3f3f
// #define inf              0x3f3f3f3f
vi G[MAX];
bool use[MAX];
vector<pii> tmp;
map<pii, int> ans;
int main() {
#ifdef VAMP
  clock_t tStart = clock();
  freopen("C:\\Users\\raiha.DESKTOP-5M3GJRN\\Videos\\Raihan\\input.txt", "r",
          stdin);
  freopen("C:\\Users\\raiha.DESKTOP-5M3GJRN\\Videos\\Raihan\\output.txt", "w",
          stdout);
#endif
  int n, u, v;
  si(n);
  FOR(i, 1, n) {
    sii(u, v);
    G[u].pb(v);
    G[v].pb(u);
    tmp.pb(MP(u, v));
  }
  queue<int> q;
  q.push(1);
  use[1] = 1;
  int k = 0;
  vector<int> track(n + 5, 0);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (k < sz(G[u]))
      k = sz(G[u]);
    int cur = 1;
    for (auto v : G[u]) {
      if (use[v])
        continue;
      if (cur == track[u])
        cur++;
      track[v] = ans[MP(u, v)] = ans[MP(v, u)] = cur;
      use[v] = 1;
      cur++;
      q.push(v);
    }
  }
  cout << k << endl;
  for (auto x : tmp)
    cout << ans[x] << endl;
#ifdef VAMP
  printf("\n>>Runtime: %.10fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC);
#endif
  return 0;
}