Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <list>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <vector>
// #include <boost/multiprecision/cpp_dec_float.hpp>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
#define REP(i, n) for (int64_t i = 0; i < (int64_t)n; i++)
#define REP_FOR(i, a, b) for (int64_t i = (int64_t)a; i < (int64_t)b; i++)
#define REP_REV(i, a, b) for (int64_t i = (int64_t)a; i > (int64_t)b; i--)
#define FORALL(v) (v).begin(), (v).end()
#define mp(a, b) make_pair(a, b)
#define eb(x) emplace_back(x)
#define F first
#define S second
typedef long long ll;
typedef long double ld;
typedef pair<int64_t, int64_t> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int64_t> vii;
typedef vector<vii> vvii;
const ll mod = 1e9 + 7;
const int64_t infi = 1147483600;
const ll infl = 4e18 + 5;
const char EOL = '\n';
// cout << fixed << setprecision(17) << res << endl;
//  const ll MOD = 1000000009;

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << std::fixed << setprecision(15);
  auto unit_r = 180.0 / M_PI;

  int64_t n;

  cin >> n;

  vector<tuple<ll, ll, ll, ll>> ab(n - 1);
  for (ll i = 0; i < n - 1; ++i) {
    ll a, b;
    cin >> a >> b;
    ab[i] = make_tuple(i, a, b, 0);
  }

  sort(ab.begin(), ab.end(), [](const auto &x, const auto &y) {
    return get<1>(x) != get<1>(y) ? get<1>(x) < get<1>(y)
                                  : get<2>(x) < get<2>(y);
  });

  // for (ll i = 0; i < n-1; ++i) {
  //     cerr << "ab[" << i << "] == " << get<1>(ab[i]) << ", " << get<2>(ab[i])
  //     << endl;
  // }

  ll m = 0;
  ll curVertex = 0;
  ll curColor = 0;
  vector<ll> color(n, 1);
  color[1] = 0;
  for (ll i = 0; i < n - 1; ++i) {
    auto v = get<1>(ab[i]);
    if (v != curVertex) {
      curColor = 1;
      curVertex = v;
    } else {
      curColor = curColor + 1;
    }
    if (color[v] == curColor)
      curColor++;
    m = max(m, curColor);
    get<3>(ab[i]) = curColor;
    color[get<2>(ab[i])] = curColor;
  }

  sort(ab.begin(), ab.end());

  cout << m << endl;
  for (ll i = 0; i < n - 1; ++i) {
    cout << get<3>(ab[i]) << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <list>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <vector>
// #include <boost/multiprecision/cpp_dec_float.hpp>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
#define REP(i, n) for (int64_t i = 0; i < (int64_t)n; i++)
#define REP_FOR(i, a, b) for (int64_t i = (int64_t)a; i < (int64_t)b; i++)
#define REP_REV(i, a, b) for (int64_t i = (int64_t)a; i > (int64_t)b; i--)
#define FORALL(v) (v).begin(), (v).end()
#define mp(a, b) make_pair(a, b)
#define eb(x) emplace_back(x)
#define F first
#define S second
typedef long long ll;
typedef long double ld;
typedef pair<int64_t, int64_t> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int64_t> vii;
typedef vector<vii> vvii;
const ll mod = 1e9 + 7;
const int64_t infi = 1147483600;
const ll infl = 4e18 + 5;
const char EOL = '\n';
// cout << fixed << setprecision(17) << res << endl;
//  const ll MOD = 1000000009;

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << std::fixed << setprecision(15);
  auto unit_r = 180.0 / M_PI;

  int64_t n;

  cin >> n;

  vector<tuple<ll, ll, ll, ll>> ab(n - 1);
  for (ll i = 0; i < n - 1; ++i) {
    ll a, b;
    cin >> a >> b;
    ab[i] = make_tuple(i, a, b, 0);
  }

  sort(ab.begin(), ab.end(), [](const auto &x, const auto &y) {
    return get<1>(x) != get<1>(y) ? get<1>(x) < get<1>(y)
                                  : get<2>(x) < get<2>(y);
  });

  // for (ll i = 0; i < n-1; ++i) {
  //     cerr << "ab[" << i << "] == " << get<1>(ab[i]) << ", " << get<2>(ab[i])
  //     << endl;
  // }

  ll m = 0;
  ll curVertex = 0;
  ll curColor = 0;
  vector<ll> color(n + 10, 1);
  color[1] = 0;
  for (ll i = 0; i < n - 1; ++i) {
    auto v = get<1>(ab[i]);
    if (v != curVertex) {
      curColor = 1;
      curVertex = v;
    } else {
      curColor = curColor + 1;
    }
    if (color[v] == curColor)
      curColor++;
    m = max(m, curColor);
    get<3>(ab[i]) = curColor;
    color[get<2>(ab[i])] = curColor;
  }

  sort(ab.begin(), ab.end());

  cout << m << endl;
  for (ll i = 0; i < n - 1; ++i) {
    cout << get<3>(ab[i]) << endl;
  }
}