Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define debug(...)
#include <complex.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
vvi g;
int n;

template <typename T> class Queue {
  int head, tail, capacity;
  T *a;

public:
  Queue() {
    head = 0, tail = 0, capacity = 1;
    a = new T[1];
  }
  T push(T data) {
    if (tail == capacity) {
      T *tmp = new T[capacity * 2];
      int id = 0;
      for (int i = head; i < tail; i++) {
        tmp[id++] = a[i];
      }
      tail = tail - head;
      head = 0;
      delete[] a;
      a = tmp;
    }
    a[tail++] = data;
  }
  T front() { return a[head]; }
  void pop() { head++; }
  int size() { return tail - head; }
};

map<pair<int, int>, int> BFS(int s) {
  vi clr(n + 1, -1);
  const int N = (int)1e6;
  Queue<int> q;
  q.push(s);
  vi used(n + 1);
  used[s] = 1;
  map<pair<int, int>, int> hhh;
  while (q.size()) {
    int from = q.front();
    q.pop();
    int id = 1;
    for (int to : g[from])
      if (!used[to]) {
        if (id == clr[from])
          id++;
        used[to] = 1;
        hhh[make_pair(from, to)] = clr[to] = id;
        id++;
        q.push(to);
      }
  }
  return hhh;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
#ifdef LOCAL
  freopen("f.txt", "r", stdin);
  // freopen("g.txt", "w", stdout);
#endif

  cin >> n;
  g = vvi(n + 1);
  size_t mx = 0;
  vector<pair<int, int>> edges;
  for (int i = 1; i < n; i++) {
    int from, to;
    cin >> from >> to;
    g[from].push_back(to);
    g[to].push_back(from);
    mx = max({mx, g[from].size(), g[to].size()});
    if (from > to)
      swap(from, to);
    edges.emplace_back(from, to);
  }

  map<pair<int, int>, int> clr = BFS(1);
  cout << mx << '\n';
  for (pair<int, int> p : edges) {
    cout << clr[p] << '\n';
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define debug(...)
#include <complex.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
vvi g;
int n;

template <typename T> class Queue {
  int head, tail, capacity;
  T *a;

public:
  Queue() {
    head = 0, tail = 0, capacity = 1;
    a = new T[1];
  }
  T push(T data) {
    if (tail == capacity) {
      T *tmp = new T[capacity * 2];
      capacity *= 2;
      int id = 0;
      for (int i = head; i < tail; i++) {
        tmp[id++] = a[i];
      }
      tail = tail - head;
      head = 0;
      delete[] a;
      a = tmp;
    }
    a[tail++] = data;
  }
  T front() { return a[head]; }
  void pop() { head++; }
  int size() { return tail - head; }
};

map<pair<int, int>, int> BFS(int s) {
  vi clr(n + 1, -1);
  const int N = (int)1e6;
  Queue<int> q;
  q.push(s);
  vi used(n + 1);
  used[s] = 1;
  map<pair<int, int>, int> hhh;
  while (q.size()) {
    int from = q.front();
    q.pop();
    int id = 1;
    for (int to : g[from])
      if (!used[to]) {
        if (id == clr[from])
          id++;
        used[to] = 1;
        hhh[make_pair(from, to)] = clr[to] = id;
        id++;
        q.push(to);
      }
  }
  return hhh;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
#ifdef LOCAL
  freopen("f.txt", "r", stdin);
  // freopen("g.txt", "w", stdout);
#endif

  cin >> n;
  g = vvi(n + 1);
  size_t mx = 0;
  vector<pair<int, int>> edges;
  for (int i = 1; i < n; i++) {
    int from, to;
    cin >> from >> to;
    g[from].push_back(to);
    g[to].push_back(from);
    mx = max({mx, g[from].size(), g[to].size()});
    if (from > to)
      swap(from, to);
    edges.emplace_back(from, to);
  }

  map<pair<int, int>, int> clr = BFS(1);
  cout << mx << '\n';
  for (pair<int, int> p : edges) {
    cout << clr[p] << '\n';
  }

  return 0;
}