prithivMLmods/Coder-Stat huggingface dataset:
Problem p02850 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
ki = [[] for _ in range(n)]
for i in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    ki[a].append([b, i])
ans = [-1] * (n - 1)


# print(ki)


def d(index, color):
    cnt = 1
    for t, jid in ki[index]:
        if cnt == color:
            cnt += 1
        ans[jid] = cnt
        d(t, cnt)
        cnt += 1


d(0, 0)
print(max(ans))
for i in ans:
    print(i)


Fixed submission:
import sys

sys.setrecursionlimit(10**7)

n = int(input())
ki = [[] for _ in range(n)]
for i in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    ki[a].append([b, i])
ans = [-1] * (n - 1)


# print(ki)


def d(index, color):
    cnt = 1
    for t, jid in ki[index]:
        if cnt == color:
            cnt += 1
        ans[jid] = cnt
        d(t, cnt)
        cnt += 1


d(0, 0)
print(max(ans))
for i in ans:
    print(i)
Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

struct Edge {
  int to, id;
};

vector<vector<Edge>> g;
vector<int> ans;

void dfs(int v, int c = -1, int p = -1) {
  int k = 1;
  rep(i, g[v].size()) {
    int u = g[v][i].to, ei = g[v][i].id;
    if (u == p)
      continue;
    if (k == c)
      ++k;
    ans[ei] = k;
    ++k;
    dfs(u, ans[ei], v);
  }
}

int main() {
  int n;
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    g[a].push_back((Edge){b, i});
    g[b].push_back((Edge){a, i});
  }
  dfs(0);
  int mx = 0;
  rep(i, n) mx = max(mx, int(g[i].size()));
  cout << mx << endl;
  rep(i, n - 1) { cout << ans[i] << endl; }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

struct Edge {
  int to, id;
};

vector<vector<Edge>> g;
vector<int> ans;

void dfs(int v, int c = -1, int p = -1) {
  int k = 1;
  rep(i, g[v].size()) {
    int u = g[v][i].to, ei = g[v][i].id;
    if (u == p)
      continue;
    if (k == c)
      ++k;
    ans[ei] = k;
    ++k;
    dfs(u, ans[ei], v);
  }
}

int main() {
  int n;
  cin >> n;
  g.resize(n);
  ans = vector<int>(n - 1);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    g[a].push_back((Edge){b, i});
    g[b].push_back((Edge){a, i});
  }
  dfs(0);
  int mx = 0;
  rep(i, n) mx = max(mx, int(g[i].size()));
  cout << mx << endl;
  rep(i, n - 1) { cout << ans[i] << endl; }
}
Problem p02850 (C++)

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

int n;
vector<int> way[10010], way2[10010];
int way_num[10010], point[10010], max_c;
bool v[10010];

void dfs(int now, int colour) {
  v[now] = 1;
  int n_c = 1;
  for (int i = 0; i < way[now].size(); i++) {
    if (v[way[now][i]] == 0) {
      if (n_c == colour)
        n_c++;
      way_num[way2[now][i]] = n_c;
      dfs(way[now][i], n_c);
      n_c++;
    }
  }
  max_c = max(max_c, n_c - 1);
}

int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int in1, in2;
    cin >> in1 >> in2;
    way[in1].push_back(in2);
    way[in2].push_back(in1);
    way2[in2].push_back(i);
    way2[in1].push_back(i);
  }

  dfs(1, 0);

  cout << max_c << endl;
  for (int i = 1; i < n; i++)
    cout << way_num[i] << endl;

  return 0;
}


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

int n;
vector<int> way[100010], way2[100010];
int way_num[100010], point[100010], max_c;
bool v[100010];

void dfs(int now, int colour) {
  v[now] = 1;
  int n_c = 1;
  for (int i = 0; i < way[now].size(); i++) {
    if (v[way[now][i]] == 0) {
      if (n_c == colour)
        n_c++;
      way_num[way2[now][i]] = n_c;
      dfs(way[now][i], n_c);
      n_c++;
    }
  }
  max_c = max(max_c, n_c - 1);
}

int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int in1, in2;
    cin >> in1 >> in2;
    way[in1].push_back(in2);
    way[in2].push_back(in1);
    way2[in2].push_back(i);
    way2[in1].push_back(i);
  }

  dfs(1, 0);

  cout << max_c << endl;
  for (int i = 1; i < n; i++)
    cout << way_num[i] << endl;

  return 0;
}
Problem p02850 (C++)

Buggy submission (status: Runtime Error):
//  ----- + ------ + + ------ JAI SHREE RAM ----- +------ + ------ + ------ +
//  ------
/* ＿人人人人人人人人人人人人人人人＿
＞　S H U B H A M    A N A N D　＜
 ￣Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y￣   */

#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long
const int N = 1e3 + 10;
vector<pair<int, int>> adj[N];
vector<int> vis(N);
vector<int> color(N);

void dfs(int x, int par, int clr) {
  vis[x] = 1;
  int tempclr = 1;
  for (auto i : adj[x]) {
    if (!vis[i.first]) {
      if (tempclr == clr)
        tempclr++;
      color[i.second] = tempclr;
      dfs(i.first, x, tempclr);
      tempclr++;
    }
  }
}

int32_t main() {
  int n;
  cin >> n;
  int ans = 0;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back({y, i});
    adj[y].push_back({x, i});
  }
  dfs(1, -1, -1);
  for (int i = 1; i <= n; i++)
    ans = max(ans, (int)adj[i].size());
  cout << ans << endl;
  for (int i = 1; i < n; i++)
    cout << color[i] << endl;
  return 0;
}


Fixed submission:
//  ----- + ------ + + ------ JAI SHREE RAM ----- +------ + ------ + ------ +
//  ------
/* ＿人人人人人人人人人人人人人人人＿
＞　S H U B H A M    A N A N D　＜
 ￣Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y￣   */

#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long
const int N = 1e5 + 10;
vector<pair<int, int>> adj[N];
vector<int> vis(N);
vector<int> color(N);

void dfs(int x, int par, int clr) {
  vis[x] = 1;
  int tempclr = 1;
  for (auto i : adj[x]) {
    if (!vis[i.first]) {
      if (tempclr == clr)
        tempclr++;
      color[i.second] = tempclr;
      dfs(i.first, x, tempclr);
      tempclr++;
    }
  }
}

int32_t main() {
  int n;
  cin >> n;
  int ans = 0;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back({y, i});
    adj[y].push_back({x, i});
  }
  dfs(1, -1, -1);
  for (int i = 1; i <= n; i++)
    ans = max(ans, (int)adj[i].size());
  cout << ans << endl;
  for (int i = 1; i < n; i++)
    cout << color[i] << endl;
  return 0;
}
Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
template <class Read> void in(Read &x) {
  x = 0;
  int f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    f |= (ch == '-');
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  x = f ? -x : x;
  return;
}
struct Node {
  int next;
  int to;
  int id;
  int col;
} a[100005];
int n, top, ans, head[100005];
void add(int x, int y, int id) {
  a[++top].next = head[x];
  a[top].to = y;
  a[top].id = id;
  head[x] = top;
}
bool cmp(Node x, Node y) {
  if (x.id != y.id)
    return x.id < y.id;
  return x.col > y.col;
}
void dfs(int x, int fa, int col) {
  int sum = 0;
  for (int i = head[x]; i; i = a[i].next) {
    if (a[i].to != fa) {
      sum++;
      if (sum == col)
        sum++;
      a[i].col = sum;
      ans = max(ans, sum);
      dfs(a[i].to, x, sum);
    }
  }
}
int main() {
  in(n);
  for (int i = 1; i < n; i++) {
    int x, y;
    in(x);
    in(y);
    add(x, y, i);
    add(y, x, i);
  }
  dfs(1, 0, 0);
  cout << ans << endl;
  sort(a + 1, a + (n - 1) * 2 + 1, cmp);
  for (int i = 1; i <= (n - 1) * 2; i++) {
    if (a[i].id != a[i - 1].id)
      printf("%d\n", a[i].col);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
template <class Read> void in(Read &x) {
  x = 0;
  int f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    f |= (ch == '-');
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  x = f ? -x : x;
  return;
}
struct Node {
  int next;
  int to;
  int id;
  int col;
} a[200005];
int n, top, ans, head[100005];
void add(int x, int y, int id) {
  a[++top].next = head[x];
  a[top].to = y;
  a[top].id = id;
  head[x] = top;
}
bool cmp(Node x, Node y) {
  if (x.id != y.id)
    return x.id < y.id;
  return x.col > y.col;
}
void dfs(int x, int fa, int col) {
  int sum = 0;
  for (int i = head[x]; i; i = a[i].next) {
    if (a[i].to != fa) {
      sum++;
      if (sum == col)
        sum++;
      a[i].col = sum;
      ans = max(ans, sum);
      dfs(a[i].to, x, sum);
    }
  }
}
int main() {
  in(n);
  for (int i = 1; i < n; i++) {
    int x, y;
    in(x);
    in(y);
    add(x, y, i);
    add(y, x, i);
  }
  dfs(1, 0, 0);
  cout << ans << endl;
  sort(a + 1, a + (n - 1) * 2 + 1, cmp);
  for (int i = 1; i <= (n - 1) * 2; i++) {
    if (a[i].id != a[i - 1].id)
      printf("%d\n", a[i].col);
  }
  return 0;
}
Problem p02850 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int n;
  cin >> n;
  vector<vector<int>> G(n);
  vector<pair<int, int>> vp;
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].emplace_back(b);
    G[b].emplace_back(a);
    vp.emplace_back(a, b);
  }
  vector<int> cs(n, 0);
  map<pair<int, int>, int> I;
  vector<int> used(n, 0);
  queue<int> que;
  que.emplace(0);
  used[0] = 1;
  int k = 0;
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    if (k < (int)G[v].size())
      k = G[v].size();
    int cur = 1;
    for (int u : G[v]) {
      if (used[u])
        continue;
      if (cur == cs[v])
        cur++;
      cs[u] = I[make_pair(u, v)] = I[make_pair(v, u)] = cur++;
      used[u] = 1;
      que.emplace(u);
    }
  }
  cout << k << endl;
  for (auto p : vp)
    cout << I[p] << endl;
  return 0;
}


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int n;
  cin >> n;
  vector<vector<int>> G(n);
  vector<pair<int, int>> vp;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].emplace_back(b);
    G[b].emplace_back(a);
    vp.emplace_back(a, b);
  }
  vector<int> cs(n, 0);
  map<pair<int, int>, int> I;
  vector<int> used(n, 0);
  queue<int> que;
  que.emplace(0);
  used[0] = 1;
  int k = 0;
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    if (k < (int)G[v].size())
      k = G[v].size();
    int cur = 1;
    for (int u : G[v]) {
      if (used[u])
        continue;
      if (cur == cs[v])
        cur++;
      cs[u] = I[make_pair(u, v)] = I[make_pair(v, u)] = cur++;
      used[u] = 1;
      que.emplace(u);
    }
  }
  cout << k << endl;
  for (auto p : vp)
    cout << I[p] << endl;
  return 0;
}
Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100000;
pair<int, int> edge_color[N];
vector<int> tree[N];
int maxc = 0;
void dfs(pair<int, int> now) {
  int next_color = 1;
  for (auto i : tree[now.first]) {
    if (edge_color[i].second)
      continue;
    if (next_color == now.second)
      next_color++;
    maxc = max(maxc, next_color);
    edge_color[i].second = next_color;
    dfs(edge_color[i]);
    next_color++;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    if (a > b)
      swap(a, b);
    tree[a].push_back(i);
    edge_color[i] = {b, 0};
  }
  dfs({1, 0});
  cout << maxc << endl;
  for (int i = 0; i < n - 1; i++) {
    cout << edge_color[i].second << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100001;
pair<int, int> edge_color[N];
vector<int> tree[N];
int maxc = 0;
void dfs(pair<int, int> now) {
  int next_color = 1;
  for (auto i : tree[now.first]) {
    if (edge_color[i].second)
      continue;
    if (next_color == now.second)
      next_color++;
    maxc = max(maxc, next_color);
    edge_color[i].second = next_color;
    dfs(edge_color[i]);
    next_color++;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    if (a > b)
      swap(a, b);
    tree[a].push_back(i);
    edge_color[i] = {b, 0};
  }
  dfs({1, 0});
  cout << maxc << endl;
  for (int i = 0; i < n - 1; i++) {
    cout << edge_color[i].second << endl;
  }
}
Problem p02850 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<vector<int>> G(N);
  vector<pair<int, int>> vp;
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].emplace_back(b);
    G[b].emplace_back(a);
    vp.emplace_back(a, b);
  }

  int K = 0;
  map<pair<int, int>, int> I;
  vector<int> cs(N, 0);

  vector<int> used(N, 0);
  queue<int> que;
  used[0] = 1;
  que.emplace(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    if (K < (int)G[v].size())
      K = G[v].size();
    int cur = 1;
    for (int u : G[v]) {
      if (used[u])
        continue;
      if (cur == cs[v])
        cur++;
      cs[u] = I[make_pair(u, v)] = I[make_pair(v, u)] = cur++;
      used[u] = 1;
      que.emplace(u);
    }
  }

  cout << K << endl;
  for (auto p : vp) {
    cout << I[p] << endl;
  }
  return 0;
}

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

int main() {
  int N;
  cin >> N;
  vector<vector<int>> G(N);
  vector<pair<int, int>> vp;
  for (int i = 1; i < N; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].emplace_back(b);
    G[b].emplace_back(a);
    vp.emplace_back(a, b);
  }

  int K = 0;
  map<pair<int, int>, int> I;
  vector<int> cs(N, 0);

  vector<int> used(N, 0);
  queue<int> que;
  used[0] = 1;
  que.emplace(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    if (K < (int)G[v].size())
      K = G[v].size();
    int cur = 1;
    for (int u : G[v]) {
      if (used[u])
        continue;
      if (cur == cs[v])
        cur++;
      cs[u] = I[make_pair(u, v)] = I[make_pair(v, u)] = cur++;
      used[u] = 1;
      que.emplace(u);
    }
  }

  cout << K << endl;
  for (auto p : vp) {
    cout << I[p] << endl;
  }
  return 0;
}
Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int N = 1e5 + 5;
vector<pii> a[N];
int visited[N];
int mincolor;
int color[N];

void dfs(int cur, int col) {
  vector<int> used(a[cur].size() + 1);
  int st = 1;
  for (auto branch : a[cur]) {
    if (!visited[branch.first]) {
      visited[branch.first] = 1;
      for (int i = st; i <= mincolor; i++) {
        if (!used[i] && i != col) {
          used[i] = 1;
          color[branch.second] = i;
          st = i + 1;
          break;
        }
      }
      dfs(branch.first, color[branch.second]);
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    a[u].push_back({v, i});
    a[v].push_back({u, i});
  }
  mincolor = 1;
  for (int i = 1; i <= n; i++) {
    mincolor = max(mincolor, (int)a[i].size());
  }
  cout << mincolor << "\n";
  int root;
  for (int i = 1; i <= n; i++) {
    if (mincolor == a[i].size()) {
      root = i;
      break;
    }
  }
  dfs(root, 1);
  for (int i = 1; i < n; i++) {
    cout << color[i] << "\n";
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int N = 1e5 + 5;
vector<pii> a[N];
int visited[N];
int mincolor;
int color[N];

void dfs(int cur, int col) {
  vector<int> used(mincolor + 1);
  int st = 1;
  for (auto branch : a[cur]) {
    if (!visited[branch.first]) {
      visited[branch.first] = 1;
      for (int i = st; i <= mincolor; i++) {
        if (!used[i] && i != col) {
          used[i] = 1;
          color[branch.second] = i;
          st = i + 1;
          break;
        }
      }
      dfs(branch.first, color[branch.second]);
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    a[u].push_back({v, i});
    a[v].push_back({u, i});
  }
  mincolor = 1;
  for (int i = 1; i <= n; i++) {
    mincolor = max(mincolor, (int)a[i].size());
  }
  cout << mincolor << "\n";
  int root;
  for (int i = 1; i <= n; i++) {
    if (mincolor == a[i].size()) {
      root = i;
      break;
    }
  }
  dfs(root, 1);
  for (int i = 1; i < n; i++) {
    cout << color[i] << "\n";
  }
  return 0;
}
Problem p02850 (C++)

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

using namespace std;

const int N = 1e5 + 5;
int ans[N], k;
vector<pair<int, int>> adj[N];

int dfs(int node, int par, int col) {
  int nxt = 1;
  for (auto &c : adj[node]) {
    if (c.first == par)
      continue;
    nxt += nxt == col;
    k = max(k, nxt);
    ans[c.second] = nxt;
    dfs(c.first, node, nxt);
    nxt++;
  }
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  int n;
  cin >> n;

  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back({v, i});
    adj[v].push_back({u, i});
  }

  dfs(1, 1, -1);

  cout << k << '\n';
  for (int i = 0; i < n - 1; i++)
    cout << ans[i] << '\n';
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;
int ans[N], k;
vector<pair<int, int>> adj[N];

void dfs(int node, int par, int col) {
  int nxt = 1;
  for (auto &c : adj[node]) {
    if (c.first == par)
      continue;
    nxt += nxt == col;
    k = max(k, nxt);
    ans[c.second] = nxt;
    dfs(c.first, node, nxt);
    nxt++;
  }
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  int n;
  cin >> n;

  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back({v, i});
    adj[v].push_back({u, i});
  }

  dfs(1, 1, -1);

  cout << k << '\n';
  for (int i = 0; i < n - 1; i++)
    cout << ans[i] << '\n';
}
Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define P pair<int, int>
#define MAX_N 1000010
using namespace std;

// 辺の塗り分けの最小値 = 出次数の最大値 = max(k,G[u].size())

// 節点uに対し時節点vの色は 1,2,~,G[u].size()
// ただし、節点uの色以外 if(c[u] == cur(=c[v])) cur++;
// 組み合わせ(辺) (u,v)の色(cur)を保存 map<P,int> ec
// c[v = 0~G[u].size()] = cur++;
// ただし(c[u] == c[v](=cur) ) cur++;
// e_c[u,v] = ec[v,c] = cur;

int main() {
  int n;
  cin >> n;
  vector<vector<int>> G;
  vector<int> visited(n, 0);
  vector<int> c(n, 0);
  vector<P> e;
  map<P, int> ec;

  for (int i = 0; i < (n - 1); i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
    e.push_back(make_pair(a, b));
  }

  int k = 0;
  visited[0] = 1;
  queue<int> que;
  que.push(0);

  while (!que.empty()) {
    int u = que.front();
    que.pop();

    if (k < G[u].size())
      k = G[u].size();
    int cur = 1;

    for (int j = 0; j < G[u].size(); j++) {
      int v = G[u][j];
      if (visited[v])
        continue;
      if (c[u] == cur)
        cur++;
      c[v] = ec[make_pair(u, v)] = ec[make_pair(v, u)] = cur++;
      que.push(v);
      visited[v] = 1;
    }
  }

  cout << k << endl;
  for (int i = 0; i < (n - 1); i++) {
    pair<int, int> key = e[i];
    int ans = ec[key];
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define P pair<int, int>
#define MAX_N 1000010
using namespace std;

// 辺の塗り分けの最小値 = 出次数の最大値 = max(k,G[u].size())

// 節点uに対し時節点vの色は 1,2,~,G[u].size()
// ただし、節点uの色以外 if(c[u] == cur(=c[v])) cur++;
// 組み合わせ(辺) (u,v)の色(cur)を保存 map<P,int> ec
// c[v = 0~G[u].size()] = cur++;
// ただし(c[u] == c[v](=cur) ) cur++;
// e_c[u,v] = ec[v,c] = cur;

int main() {
  int n;
  cin >> n;
  vector<vector<int>> G(n);
  vector<int> visited(n, 0);
  vector<int> c(n, 0);
  vector<P> e;
  map<P, int> ec;

  for (int i = 0; i < (n - 1); i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
    e.push_back(make_pair(a, b));
  }

  int k = 0;
  visited[0] = 1;
  queue<int> que;
  que.push(0);

  while (!que.empty()) {
    int u = que.front();
    que.pop();

    if (k < G[u].size())
      k = G[u].size();
    int cur = 1;

    for (int j = 0; j < G[u].size(); j++) {
      int v = G[u][j];
      if (visited[v])
        continue;
      if (c[u] == cur)
        cur++;
      c[v] = ec[make_pair(u, v)] = ec[make_pair(v, u)] = cur++;
      que.push(v);
      visited[v] = 1;
    }
  }

  cout << k << endl;
  for (int i = 0; i < (n - 1); i++) {
    pair<int, int> key = e[i];
    int ans = ec[key];
    cout << ans << endl;
  }
  return 0;
}
Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
void dfs(vector<int> &c, vector<vector<pair<int, int>>> &E, int v = 0) {
  int cnt = E[v].size();
  set<int> st;
  for (int i = 1; i <= cnt; i++) {
    st.insert(cnt);
  }
  for (auto P : E[v]) {
    int id = P.second;
    if (c[id] != -1) {
      st.erase(c[id]);
    }
  }
  vector<int> color;
  for (int x : st) {
    color.push_back(x);
  }
  for (auto P : E[v]) {
    int w = P.first;
    int id = P.second;
    if (c[id] == -1) {
      c[id] = color.back();
      color.pop_back();
      dfs(c, E, w);
    }
  }
}
int main() {
  int N;
  cin >> N;
  vector<vector<pair<int, int>>> E(N);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    E[a].push_back(make_pair(b, i));
    E[b].push_back(make_pair(a, i));
  }
  vector<int> c(N - 1, -1);
  dfs(c, E);
  int K = 0;
  for (int i = 0; i < N - 1; i++) {
    K = max(K, c[i]);
  }
  cout << K << endl;
  for (int i = 0; i < N - 1; i++) {
    cout << c[i] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
void dfs(vector<int> &c, vector<vector<pair<int, int>>> &E, int v = 0) {
  int cnt = E[v].size();
  set<int> st;
  for (int i = 1; i <= cnt; i++) {
    st.insert(i);
  }
  for (auto P : E[v]) {
    int id = P.second;
    if (c[id] != -1) {
      st.erase(c[id]);
    }
  }
  vector<int> color;
  for (int x : st) {
    color.push_back(x);
  }
  for (auto P : E[v]) {
    int w = P.first;
    int id = P.second;
    if (c[id] == -1) {
      c[id] = color.back();
      color.pop_back();
      dfs(c, E, w);
    }
  }
}
int main() {
  int N;
  cin >> N;
  vector<vector<pair<int, int>>> E(N);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    E[a].push_back(make_pair(b, i));
    E[b].push_back(make_pair(a, i));
  }
  vector<int> c(N - 1, -1);
  dfs(c, E);
  int K = 0;
  for (int i = 0; i < N - 1; i++) {
    K = max(K, c[i]);
  }
  cout << K << endl;
  for (int i = 0; i < N - 1; i++) {
    cout << c[i] << endl;
  }
}
Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<pi, pi> pp;
typedef pair<ll, ll> pl;
double PI = 3.141592653589793238462643383279;
const double EPS = 1e-9;
const ll MOD = 1000000007;
// const ll MOD = 998244353
const int inf = 1 << 30;
const ll linf = 1LL << 60;

int N;
int par[100000];
int ans[100000];
vector<pi> edge[100000];

void dfs(int v) {
  int cnt = 1;
  int ex = 0;
  rep(i, edge[v].size()) {
    if (edge[v][i].first == par[v]) {
      ex = ans[edge[v][i].second];
    }
    par[edge[v][i].first] = v;
  }
  rep(i, edge[v].size()) {
    if (edge[v][i].first == par[v]) {
      continue;
    }
    if (cnt == ex)
      cnt++;
    ans[edge[v][i].second] = cnt++;
  }
  rep(i, edge[v].size()) {
    if (edge[v][i].first == par[v]) {
      continue;
    }
    dfs(edge[v][i].first);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edge[a].push_back(pi(b, i));
    edge[b].push_back(pi(a, i));
  }
  memset(par, -1, sizeof(par));
  dfs(0);
  cout << *max_element(ans, ans + N - 1) << endl;
  rep(i, N - 1) { cout << ans[i] << endl; }
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<pi, pi> pp;
typedef pair<ll, ll> pl;
double PI = 3.141592653589793238462643383279;
const double EPS = 1e-9;
const ll MOD = 1000000007;
// const ll MOD = 998244353
const int inf = 1 << 30;
const ll linf = 1LL << 60;

int N;
int par[100000];
int ans[100000];
vector<pi> edge[100000];

void dfs(int v) {
  int cnt = 1;
  int ex = 0;
  rep(i, edge[v].size()) {
    if (edge[v][i].first == par[v]) {
      ex = ans[edge[v][i].second];
      continue;
    }
    par[edge[v][i].first] = v;
  }
  rep(i, edge[v].size()) {
    if (edge[v][i].first == par[v]) {
      continue;
    }
    if (cnt == ex)
      cnt++;
    ans[edge[v][i].second] = cnt++;
  }
  rep(i, edge[v].size()) {
    if (edge[v][i].first == par[v]) {
      continue;
    }
    dfs(edge[v][i].first);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edge[a].push_back(pi(b, i));
    edge[b].push_back(pi(a, i));
  }
  memset(par, -1, sizeof(par));
  dfs(0);
  cout << *max_element(ans, ans + N - 1) << endl;
  rep(i, N - 1) { cout << ans[i] << endl; }
}
Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

struct Edge {
  int to, id;
};

vector<vector<Edge>> g;
vector<int> ans;

void dfs(int v, int c = -1, int p = -1) {
  int k = 1;
  rep(i, g[v].size()) {
    int u = g[v][i].to, ei = g[v][i].id;
    if (u == p)
      continue;
    if (k == c)
      ++k;
    ans[ei] = k;
    ++k;
    dfs(u, ans[ei], v);
  }
}

int main() {
  int n;
  cin >> n;
  g.resize(n);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].push_back((Edge){b, i});
    g[b].push_back((Edge){a, i});
  }
  dfs(0);
  int mx = 0;
  rep(i, n) mx = max(mx, int(g[i].size()));
  cout << mx << endl;
  rep(i, n - 1) { cout << ans[i] << endl; }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

struct Edge {
  int to, id;
};

vector<vector<Edge>> g;
vector<int> ans;

void dfs(int v, int c = -1, int p = -1) {
  int k = 1;
  rep(i, g[v].size()) {
    int u = g[v][i].to, ei = g[v][i].id;
    if (u == p)
      continue;
    if (k == c)
      ++k;
    ans[ei] = k;
    ++k;
    dfs(u, ans[ei], v);
  }
}

int main() {
  int n;
  cin >> n;
  g.resize(n);
  ans = vector<int>(n - 1);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].push_back((Edge){b, i});
    g[b].push_back((Edge){a, i});
  }
  dfs(0);
  int mx = 0;
  rep(i, n) mx = max(mx, int(g[i].size()));
  cout << mx << endl;
  rep(i, n - 1) { cout << ans[i] << endl; }
  return 0;
}
Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const int maxn = 1e5 + 10;
map<pair<int, int>, int> ans;
vector<int> edge[maxn];
priority_queue<int, vector<int>, greater<int>> q[maxn];
priority_queue<int, vector<int>, less<int>> Q[maxn];
int n, a[maxn], b[maxn], Max;
inline void dfs(int now, int fa) {
  for (size_t i = 0; i < edge[now].size(); i++) {
    int to = edge[now][i];
    if (to ^ fa) {
      int val = 0;
      if (q[now].size())
        val = q[now].top() - 1;
      if (!val)
        val = Q[now].top() + 1;
      Max = max(Max, val);
      q[now].push(val);
      q[to].push(val);
      Q[now].push(val);
      Q[to].push(val);
      ans[make_pair(now, to)] = val;
      ans[make_pair(to, now)] = val;
      dfs(to, now);
    }
  }
}
signed main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &a[i], &b[i]);
    edge[a[i]].push_back(b[i]);
    edge[b[i]].push_back(a[i]);
  }
  dfs(1, 0);
  printf("%d\n", Max);
  for (int i = 1; i < n; i++)
    printf("%d\n", ans[make_pair(a[i], b[i])]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const int maxn = 1e5 + 10;
map<pair<int, int>, int> ans;
vector<int> edge[maxn];
priority_queue<int, vector<int>, greater<int>> q[maxn];
priority_queue<int, vector<int>, less<int>> Q[maxn];
int n, a[maxn], b[maxn], Max;
inline void dfs(int now, int fa) {
  for (size_t i = 0; i < edge[now].size(); i++) {
    int to = edge[now][i];
    if (to ^ fa) {
      int val = 0;
      if (q[now].size())
        val = q[now].top() - 1;
      else
        val = 1;
      if (!val)
        val = Q[now].top() + 1;
      Max = max(Max, val);
      q[now].push(val);
      q[to].push(val);
      Q[now].push(val);
      Q[to].push(val);
      ans[make_pair(now, to)] = val;
      ans[make_pair(to, now)] = val;
      dfs(to, now);
    }
  }
}
signed main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &a[i], &b[i]);
    edge[a[i]].push_back(b[i]);
    edge[b[i]].push_back(a[i]);
  }
  dfs(1, 0);
  printf("%d\n", Max);
  for (int i = 1; i < n; i++)
    printf("%d\n", ans[make_pair(a[i], b[i])]);
  return 0;
}
Problem p02850 (C++)

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

const ll siz = 1e5;
const ll mod = 1e9;
const ll MAXX = 1e18;

map<pair<ll, ll>, ll> col;
vector<ll> graph[siz];
ll ma = 0;

void dfs(ll node, ll par) {
  ll color = 1;
  if (color == col[make_pair(node, par)]) {
    color++;
  }
  for (ll i = 0; i < graph[node].size(); i++) {
    ll v = graph[node][i];
    if (v != par) {
      pair<ll, ll> a = make_pair(node, v);
      pair<ll, ll> b = make_pair(v, node);
      col[a] = col[b] = color;
      dfs(v, node);
      color++;
      if (color == col[make_pair(node, par)]) {
        color++;
      }
    }
  }
  ma = max(color - 1, ma);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("INP.txt", "r", stdin);
  // freopen("OUT.txt", "w", stdout);
  ll n;
  cin >> n;
  vector<pair<ll, ll>> hold;
  for (ll i = 1; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
    hold.push_back(make_pair(a, b));
  }
  dfs(1, 1);
  cout << ma << '\n';
  for (ll i = 0; i < hold.size(); i++) {
    cout << col[hold[i]] << '\n';
  }
  return 0;
}


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

const ll siz = 1e5;
const ll mod = 1e9;
const ll MAXX = 1e18;

map<pair<ll, ll>, ll> col;
vector<ll> graph[siz + 10];
ll ma = 0;

void dfs(ll node, ll par) {
  ll color = 1;
  if (color == col[make_pair(node, par)]) {
    color++;
  }
  for (ll i = 0; i < graph[node].size(); i++) {
    ll v = graph[node][i];
    if (v != par) {
      pair<ll, ll> a = make_pair(node, v);
      pair<ll, ll> b = make_pair(v, node);
      col[a] = col[b] = color;
      dfs(v, node);
      color++;
      if (color == col[make_pair(node, par)]) {
        color++;
      }
    }
  }
  ma = max(color - 1, ma);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("INP.txt", "r", stdin);
  // freopen("OUT.txt", "w", stdout);
  ll n;
  cin >> n;
  vector<pair<ll, ll>> hold;
  for (ll i = 1; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
    hold.push_back(make_pair(a, b));
  }
  dfs(1, 1);
  cout << ma << '\n';
  for (ll i = 0; i < hold.size(); i++) {
    cout << col[hold[i]] << '\n';
  }
  return 0;
}
Problem p02850 (C++)

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

const int N = (int)1e5 + 5;

int n;
vector<int> g[N];
vector<pair<int, int>> edge;
map<pair<int, int>, int> colorEdge;
bool used[N];
int c[N];

int main() {
#define TASK "test"
#ifndef ONLINE_JUDGE
  freopen(TASK ".inp", "r", stdin);
  freopen(TASK ".out", "w", stdout);
#endif
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    edge.push_back({u, v});
    g[u].push_back(v);
    g[v].push_back(u);
  }
  queue<int> q;
  q.push(1);
  used[1] = true;
  int nColor = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    nColor = max(nColor, (int)g[u].size());
    int cur = 1;
    for (auto v : g[u]) {
      if (used[v])
        continue;
      if (cur == c[u])
        cur++;
      c[v] = colorEdge[{u, v}] = colorEdge[{v, u}] = cur++;
      used[v] = true;
      q.push(v);
    }
  }
  cout << nColor << "\n";
  for (auto e : edge)
    cout << colorEdge[e] << "\n";
  return 0;
}

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

const int N = (int)1e5 + 5;

int n;
vector<int> g[N];
vector<pair<int, int>> edge;
map<pair<int, int>, int> colorEdge;
bool used[N];
int c[N];

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    edge.push_back({u, v});
    g[u].push_back(v);
    g[v].push_back(u);
  }
  queue<int> q;
  q.push(1);
  used[1] = true;
  int nColor = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    nColor = max(nColor, (int)g[u].size());
    int cur = 1;
    for (auto v : g[u]) {
      if (used[v])
        continue;
      if (cur == c[u])
        cur++;
      c[v] = colorEdge[{u, v}] = colorEdge[{v, u}] = cur++;
      used[v] = true;
      q.push(v);
    }
  }
  cout << nColor << "\n";
  for (auto e : edge)
    cout << colorEdge[e] << "\n";
  return 0;
}
Problem p02850 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define maxs(a, b) a = max(a, b)
#define mins(a, b) a = min(a, b)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll linf = (1ll << 61);
const int inf = 1001001001;
const int mod = 1000000007;

int n;
vector<int> g[100005];
vector<int> ans(100005);
map<P, int> mp;
int ans1;

void dfs(int v, int p = -1, int prev = -1) {
  vector<int> k(ans1);
  iota(k.begin(), k.end(), 0);
  for (int i = 0; i < ans1; ++i) {
    if (i == prev) {
      k.erase(k.begin() + i);
      break;
    }
  }
  int pos = 0;
  int li = ans1;
  while (pos == prev || pos == li)
    pos++;
  for (int c : g[v]) {
    if (c == p)
      continue;
    mp[P(c, v)] = pos;
    mp[P(v, c)] = pos;
    dfs(c, v, pos);
    pos++;
    while (pos == prev || pos == li)
      pos++;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  vector<P> res(n - 1);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
    res[i] = P(a, b);
  }
  ans1 = 0;
  rep(i, n) maxs(ans1, int(g[i].size()));
  printf("%d\n", ans1);
  dfs(0);
  rep(i, n - 1) { printf("%d\n", mp[res[i]] + 1); }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define maxs(a, b) a = max(a, b)
#define mins(a, b) a = min(a, b)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll linf = (1ll << 61);
const int inf = 1001001001;
const int mod = 1000000007;

int n;
vector<int> g[100005];
vector<int> ans(100005);
map<P, int> mp;
int ans1;

void dfs(int v, int p = -1, int prev = -1) {
  int pos = 0;
  int li = ans1;
  while (pos == prev || pos == li)
    pos++;
  for (int c : g[v]) {
    if (c == p)
      continue;
    mp[P(c, v)] = pos;
    mp[P(v, c)] = pos;
    dfs(c, v, pos);
    pos++;
    while (pos == prev || pos == li)
      pos++;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  vector<P> res(n - 1);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
    res[i] = P(a, b);
  }
  ans1 = 0;
  rep(i, n) maxs(ans1, int(g[i].size()));
  printf("%d\n", ans1);
  dfs(0);
  rep(i, n - 1) { printf("%d\n", mp[res[i]] + 1); }
  return 0;
}
Problem p02850 (C++)

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

int fill_color(vector<vector<int>> &tree,
               unordered_map<int, unordered_map<int, int>> &color, int parent,
               int parent_color) {
  int max_color = 0;
  for (int i = 0; i < tree[parent].size(); i++) {
    int current_color;
    if (i + 1 < parent_color) {
      current_color = i + 1;
    } else {
      current_color = i + 2;
    }

    int child = tree[parent][i];
    color[parent][child] = current_color;

    int submax_color = fill_color(tree, color, child, current_color);

    if (max_color < current_color)
      max_color = current_color;
    if (max_color < submax_color)
      max_color = submax_color;
  }
  return max_color;
}

int main() {
  int n;
  cin >> n;

  vector<int> a(n - 1);
  vector<int> b(n - 1);
  vector<vector<int>> tree(n + 1);
  unordered_map<int, unordered_map<int, int>> color;

  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    tree[a[i]].push_back(b[i]);
  }

  int max_color = fill_color(tree, color, 1, __INT_MAX__);

  cout << max_color << endl;
  for (int i = 0; i < n - 1; i++) {
    cout << color[a[i]][b[i]] << endl;
  }
}


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

int fill_color(vector<vector<int>> &tree,
               unordered_map<int, unordered_map<int, int>> &color, int parent,
               int parent_color) {
  int max_color = 0;
  for (int i = 0; i < tree[parent].size(); i++) {
    int current_color;
    if (i + 1 < parent_color) {
      current_color = i + 1;
    } else {
      current_color = i + 2;
    }

    int child = tree[parent][i];
    color[parent][child] = current_color;

    int submax_color = fill_color(tree, color, child, current_color);

    if (max_color < current_color)
      max_color = current_color;
    if (max_color < submax_color)
      max_color = submax_color;
  }
  return max_color;
}

int main() {
  int n;
  cin >> n;

  vector<int> a(n - 1);
  vector<int> b(n - 1);
  vector<vector<int>> tree(n + 1);
  unordered_map<int, unordered_map<int, int>> color;

  for (int i = 0; i < n - 1; i++) {
    cin >> a[i] >> b[i];
    tree[a[i]].push_back(b[i]);
  }

  int max_color = fill_color(tree, color, 1, __INT_MAX__);

  cout << max_color << endl;
  for (int i = 0; i < n - 1; i++) {
    cout << color[a[i]][b[i]] << endl;
  }
}
Problem p02850 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());
  ll n;
  cin >> n;
  vector<vector<pair<ll, ll>>> g(n);
  rep(i, n - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(make_pair(b, i));
    g[b].push_back(make_pair(a, i));
  }
  vector<ll> ans(n - 1, -1);
  vector<set<ll>> color(n);
  queue<ll> q;
  q.push(0);
  while (!q.empty()) {
    ll v = q.front();
    q.pop();
    ll cur = 0;
    for (auto x : g[v]) {
      ll u = x.first, idx = x.second;
      if (ans[idx] != -1)
        continue;
      reps(i, cur, n) {
        if ((color[u].count(i) == 0) && (color[v].count(i) == 0)) {
          color[u].insert(i);
          color[v].insert(i);
          ans[idx] = i;
          q.push(u);
          break;
        }
      }
    }
  }
  printf("%lld\n", *max_element(all(ans)) + 1);
  rep(i, n - 1) printf("%lld\n", ans[i] + 1);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());
  ll n;
  cin >> n;
  vector<vector<pair<ll, ll>>> g(n);
  rep(i, n - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(make_pair(b, i));
    g[b].push_back(make_pair(a, i));
  }
  vector<ll> ans(n - 1, -1);
  vector<set<ll>> color(n);
  queue<ll> q;
  q.push(0);
  while (!q.empty()) {
    ll v = q.front();
    q.pop();
    ll cur = 0;
    for (auto x : g[v]) {
      ll u = x.first, idx = x.second;
      if (ans[idx] != -1)
        continue;
      reps(i, cur, n) {
        if ((color[u].count(i) == 0) && (color[v].count(i) == 0)) {
          cur = i + 1;
          color[u].insert(i);
          color[v].insert(i);
          ans[idx] = i;
          q.push(u);
          break;
        }
      }
    }
  }
  printf("%lld\n", *max_element(all(ans)) + 1);
  rep(i, n - 1) printf("%lld\n", ans[i] + 1);
  return 0;
}
Problem p02850 (C++)

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

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i < (ll)(n); i++)
#define INF 10000000000
#define MOD 1000000007
using ll = long long;
using Graph = vector<set<int>>;

int main() {
  int N;
  cin >> N;
  vector<int> A(N), B(N);
  map<pair<int, int>, int> Edge;
  Graph G(N + 1);
  rep(i, N - 1) {
    cin >> A.at(i) >> B.at(i);
    Edge[make_pair(A.at(i), B.at(i))] = -1;
    Edge[make_pair(B.at(i), A.at(i))] = -1;
    G.at(A.at(i)).insert(B.at(i));
    G.at(B.at(i)).insert(A.at(i));
  }

  int Colors = 1;
  rep1(i, N + 1) Colors = max(Colors, (int)G.at(i).size());
  cout << Colors << endl;

  vector<bool> seen(N + 1, false);
  queue<int> Q;
  Q.push(1);
  map<int, int> par;
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop();
    seen.at(v) = true;
    queue<int> CV;
    int banned_num = -1;
    if (v != 1) {
      banned_num = Edge[make_pair(v, par[v])];
    }
    rep1(i, Colors + 1) {
      if (i != banned_num)
        CV.push(i);
    }
    for (auto nv : G.at(v)) {
      if (seen.at(nv))
        continue;
      int edge_color = CV.front();
      CV.pop();
      Edge[make_pair(v, nv)] = edge_color;
      Edge[make_pair(nv, v)] = edge_color;
      par[nv] = v;
      Q.push(nv);
    }
  }
  rep(i, N - 1) cout << Edge[make_pair(A.at(i), B.at(i))] << endl;
}


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

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i < (ll)(n); i++)
#define INF 10000000000
#define MOD 1000000007
using ll = long long;
using Graph = vector<set<int>>;

int main() {
  int N;
  cin >> N;
  vector<int> A(N), B(N);
  map<pair<int, int>, int> Edge;
  Graph G(N + 1);
  rep(i, N - 1) {
    cin >> A.at(i) >> B.at(i);
    Edge[make_pair(A.at(i), B.at(i))] = -1;
    Edge[make_pair(B.at(i), A.at(i))] = -1;
    G.at(A.at(i)).insert(B.at(i));
    G.at(B.at(i)).insert(A.at(i));
  }

  int Colors = 1;
  rep1(i, N + 1) Colors = max(Colors, (int)G.at(i).size());
  cout << Colors << endl;

  vector<bool> seen(N + 1, false);
  queue<int> Q;
  Q.push(1);
  map<int, int> par;
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop();
    seen.at(v) = true;
    queue<int> CV;
    int banned_num = -1;
    if (v != 1) {
      banned_num = Edge[make_pair(v, par[v])];
    }
    rep1(i, (int)G.at(v).size() + 1) {
      if (i != banned_num)
        CV.push(i);
    }
    for (auto nv : G.at(v)) {
      if (seen.at(nv))
        continue;
      int edge_color = CV.front();
      CV.pop();
      Edge[make_pair(v, nv)] = edge_color;
      Edge[make_pair(nv, v)] = edge_color;
      par[nv] = v;
      Q.push(nv);
    }
  }
  rep(i, N - 1) cout << Edge[make_pair(A.at(i), B.at(i))] << endl;
}
Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

struct Edge {
  int to, id;
};

vector<vector<Edge>> g;
vector<int> ans;

void dfs(int v, int c = -1, int p = -1) {
  int k = 1;
  rep(i, g[v].size()) {
    int u = g[v][i].to, ei = g[v][i].id;
    if (u == p)
      continue;
    if (k == c)
      k++;
    ans[ei] = k;
    k++;
    dfs(u, ans[ei], v);
  }
}

int main() {
  int n;
  cin >> n;
  ans = vector<int>(n - 1);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back((Edge){b, i});
    g[b].push_back((Edge){a, i});
  }
  dfs(0);
  int mx = 0;
  rep(i, n) mx = max(mx, (int)g[i].size());
  cout << mx << endl;
  rep(i, n - 1) cout << ans[i] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

struct Edge {
  int to, id;
};

vector<vector<Edge>> g;
vector<int> ans;

void dfs(int v, int c = -1, int p = -1) {
  int k = 1;
  rep(i, g[v].size()) {
    int u = g[v][i].to, ei = g[v][i].id;
    if (u == p)
      continue;
    if (k == c)
      k++;
    ans[ei] = k;
    k++;
    dfs(u, ans[ei], v);
  }
}

int main() {
  int n;
  cin >> n;
  g.resize(n);
  ans = vector<int>(n - 1);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back((Edge){b, i});
    g[b].push_back((Edge){a, i});
  }
  dfs(0);
  int mx = 0;
  rep(i, n) mx = max(mx, (int)g[i].size());
  cout << mx << endl;
  rep(i, n - 1) cout << ans[i] << endl;
  return 0;
}