Problem p02850 (C++)

Buggy submission (status: Runtime Error):

/**
 *    author  : 𝒌𝒚𝒐𝒎𝒖𝒌𝒚𝒐𝒎𝒖𝒑𝒖𝒓𝒊𝒏
 *    created : 2019-12-07 22:21:13
 **/

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using int64 = long long;
#define debug(_) cerr << #_ << ": " << (_) << '\n'
template <class T> ostream &operator<<(ostream &os, vector<T> &_) {
  os << '{';
  for (T &x : _) {
    os << x;
    if (&x != &_.back())
      os << ',';
  }
  os << '}';
  return os;
}
template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> &p) {
  return os << '{' << p.first << " " << p.second << '}';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  vector<vector<int>> graph(n, vector<int>(n));
  vector<int> cnt(n, 0);
  vector<pair<int, int>> in(n);

  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    ++cnt[a];
    ++cnt[b];
    graph[a].emplace_back(b);
    graph[b].emplace_back(a);
    in[i] = {a, b};
  }
  // cerr << '\n';
  cout << *max_element(cnt.begin(), cnt.end()) << '\n';

  map<pair<int, int>, int> mp;
  vector<int> color(n, -1);
  vector<bool> visited(n, false);

  queue<int> q;
  q.push(0);
  visited[0] = true;

  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    int num = 1;
    for (int next : graph[cur]) {
      if (visited[next])
        continue;
      if (num == color[cur])
        ++num;
      color[next] = num;
      mp[{cur, next}] = mp[{next, cur}] = num;
      ++num;
      visited[next] = true;
      q.push(next);
    }
  }

  for (int i = 0; i < n - 1; ++i) {
    cout << mp[in[i]] << '\n';
  }

  return 0;
}

Fixed submission:

/**
 *    author  : 𝒌𝒚𝒐𝒎𝒖𝒌𝒚𝒐𝒎𝒖𝒑𝒖𝒓𝒊𝒏
 *    created : 2019-12-07 22:21:13
 **/

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using int64 = long long;
#define debug(_) cerr << #_ << ": " << (_) << '\n'
template <class T> ostream &operator<<(ostream &os, vector<T> &_) {
  os << '{';
  for (T &x : _) {
    os << x;
    if (&x != &_.back())
      os << ',';
  }
  os << '}';
  return os;
}
template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> &p) {
  return os << '{' << p.first << " " << p.second << '}';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  vector<vector<int>> graph(n);
  vector<int> cnt(n, 0);
  vector<pair<int, int>> in(n);

  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    ++cnt[a];
    ++cnt[b];
    graph[a].emplace_back(b);
    graph[b].emplace_back(a);
    in[i] = {a, b};
  }
  // cerr << '\n';
  cout << *max_element(cnt.begin(), cnt.end()) << '\n';

  map<pair<int, int>, int> mp;
  vector<int> color(n, -1);
  vector<bool> visited(n, false);

  queue<int> q;
  q.push(0);
  visited[0] = true;

  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    int num = 1;
    for (int next : graph[cur]) {
      if (visited[next])
        continue;
      if (num == color[cur])
        ++num;
      color[next] = num;
      mp[{cur, next}] = mp[{next, cur}] = num;
      ++num;
      visited[next] = true;
      q.push(next);
    }
  }

  for (int i = 0; i < n - 1; ++i) {
    cout << mp[in[i]] << '\n';
  }

  return 0;
}