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

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int N;
int a[150000];
int b[150000];
int i, j;
queue<pair<int, int>> que;
pair<int, int> p;

int main() {
  vector<int> v[100000];
  int cnt[100000];
  int c[100000];
  int s = 1;
  cin >> N;
  for (i = 0; i < N; i++) {
    cnt[i] = 0;
    c[i] = 0;
  }
  for (i = 0; i < N - 1; i++) {
    cin >> a[i] >> b[i];
    cnt[a[i] - 1]++;
    cnt[b[i] - 1]++;
    v[a[i]].push_back(i);
    v[b[i]].push_back(i);
  }
  int k = 0;
  for (i = 0; i < N; i++) {
    k = max(k, cnt[i]);
  }
  int color = 1;
  for (i = 0; i < N - 1; i++) {
    if (a[i] == s) {
      que.push(make_pair(i, b[i]));
      c[i] = color;
      color++;
    }
  }
  while (!que.empty()) {
    color = 1;
    p = que.front();
    que.pop();
    for (i = 0; i < v[p.second].size(); i++) {
      if (color == c[p.first])
        color++;
      int t = v[p.second][i];
      if (a[t] == p.second && c[t] == 0) {
        que.push(make_pair(t, b[t]));
        c[t] = color;
        color++;
      }
      if (b[t] == p.second && c[t] == 0) {
        que.push(make_pair(i, a[i]));
        c[t] = color;
        color++;
      }
    }
  }
  cout << k << endl;
  for (i = 0; i < N - 1; i++)
    cout << c[i] << endl;
}


Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int N;
int a[150000];
int b[150000];
int i, j;
queue<pair<int, int>> que;
pair<int, int> p;

int main() {
  vector<int> v[100005];
  int cnt[100000];
  int c[100000];
  int s = 1;
  cin >> N;
  for (i = 0; i < N; i++) {
    cnt[i] = 0;
    c[i] = 0;
  }
  for (i = 0; i < N - 1; i++) {
    cin >> a[i] >> b[i];
    cnt[a[i] - 1]++;
    cnt[b[i] - 1]++;
    v[a[i]].push_back(i);
    v[b[i]].push_back(i);
  }
  int k = 0;
  for (i = 0; i < N; i++) {
    k = max(k, cnt[i]);
  }
  int color = 1;
  for (i = 0; i < N - 1; i++) {
    if (a[i] == s) {
      que.push(make_pair(i, b[i]));
      c[i] = color;
      color++;
    }
  }
  while (!que.empty()) {
    color = 1;
    p = que.front();
    que.pop();
    for (i = 0; i < v[p.second].size(); i++) {
      if (color == c[p.first])
        color++;
      int t = v[p.second][i];
      if (a[t] == p.second && c[t] == 0) {
        que.push(make_pair(t, b[t]));
        c[t] = color;
        color++;
      }
      if (b[t] == p.second && c[t] == 0) {
        que.push(make_pair(i, a[i]));
        c[t] = color;
        color++;
      }
    }
  }
  cout << k << endl;
  for (i = 0; i < N - 1; i++)
    cout << c[i] << endl;
}
Problem p02850 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n - 1), b(n - 1);

  vector<queue<int>> edge(n);
  for (int i = 0; i < n - 1; i++) {
    cin >> a[i] >> b[i];
    --a[i];
    --b[i];
    edge[a[i]].push(i);
    edge[b[i]].push(i);
  }

  int ans = 0; // 塗られた色の種類数

  vector<int> ANS(n - 1, 0); // 各辺に塗る色，0のときは未訪問
  vector<set<int>> colors(n); // 各頂点を端点とする辺に塗られた色の集合

  queue<int> st; // 訪問待ちの頂点
  st.push(a[0]);
  colors[a[0]].insert(1);
  colors[b[0]].insert(1);

  while (st.size()) {
    int v = st.front();
    st.pop();
    while (edge[v].size()) {
      int i = edge[v].front();
      edge[v].pop();

      if (ANS[i] == 0 && (v == a[i] || v == b[i])) {

        // 訪問待ちの頂点を追加
        if (v == a[i]) {
          st.push(b[i]);
        } else {
          st.push(a[i]);
        }

        // 色の決定
        for (int j = 1; j <= ans; j++) {
          if (colors[a[i]].find(j) == colors[a[i]].end() &&
              colors[b[i]].find(j) == colors[b[i]].end()) {
            ANS[i] = j;
            colors[a[i]].insert(j);
            colors[b[i]].insert(j);
            break;
          }
        }
        if (ANS[i] == 0) {
          ans++;
          ANS[i] = ans;
          colors[a[i]].insert(ans);
          colors[b[i]].insert(ans);
        }
      }
    }
  }

  cout << ans << endl;
  for (int i = 0; i < n - 1; i++) {
    cout << ANS[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n - 1), b(n - 1);

  vector<queue<int>> edge(n);
  for (int i = 0; i < n - 1; i++) {
    cin >> a[i] >> b[i];
    --a[i];
    --b[i];
    edge[a[i]].push(i);
    edge[b[i]].push(i);
  }

  int ans = 0; // 塗られた色の種類数

  vector<int> ANS(n - 1, 0); // 各辺に塗る色，0のときは未訪問
  vector<set<int>> colors(n); // 各頂点を端点とする辺に塗られた色の集合

  queue<int> st; // 訪問待ちの頂点
  st.push(a[0]);
  colors[a[0]].insert(1);
  colors[b[0]].insert(1);

  while (st.size()) {
    int v = st.front();
    st.pop();
    while (edge[v].size()) {
      int i = edge[v].front();
      edge[v].pop();

      if (ANS[i] == 0 && (v == a[i] || v == b[i])) {

        // 訪問待ちの頂点を追加
        if (v == a[i]) {
          st.push(b[i]);
        } else {
          st.push(a[i]);
        }

        // 色の決定
        if (colors[a[i]].size() == ans || colors[b[i]].size() == ans) {
          ans++;
          ANS[i] = ans;
          colors[a[i]].insert(ans);
          colors[b[i]].insert(ans);
          continue;
        }
        for (int j = 1; j <= ans; j++) {
          if (colors[a[i]].find(j) == colors[a[i]].end() &&
              colors[b[i]].find(j) == colors[b[i]].end()) {
            ANS[i] = j;
            colors[a[i]].insert(j);
            colors[b[i]].insert(j);
            break;
          }
        }
        if (ANS[i] == 0) {
          ans++;
          ANS[i] = ans;
          colors[a[i]].insert(ans);
          colors[b[i]].insert(ans);
        }
      }
    }
  }

  cout << ans << endl;
  for (int i = 0; i < n - 1; i++) {
    cout << ANS[i] << endl;
  }
  return 0;
}
Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int next;
  int to;
  int color;
} edge[100005];
int head[100005], cnt, m[100005], ans, n;

void add(int u, int v) {
  edge[++cnt].next = head[u];
  edge[cnt].to = v;
  head[u] = cnt;
}
void dfs(int x, int fa) {
  //	cout<<x<<endl;
  int us = 1, ban = 0;
  for (int i = head[x]; i; i = edge[i].next) {
    if (edge[i].to == fa) {
      //		cout<<i<<endl;
      ban = edge[i].color;
      break;
    }
  }
  //	cout<<x<<' '<<ban<<endl;
  for (int i = head[x]; i; i = edge[i].next) {

    if (edge[i].to == fa)
      continue; //	cout<<x<<' '<<us<<endl;
    if (us == ban)
      us++;
    edge[i].color = edge[i + 1].color = us;

    dfs(edge[i].to, x);
    us++;
  }
}
int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    if (u > v)
      swap(u, v);
    add(u, v);
    add(v, u);
    m[u]++;
    m[v]++;
  }
  for (int i = 1; i <= n; i++) {
    //		t[i]=m[i];
    ans = max(ans, m[i]);
  }
  cout << ans << endl;
  dfs(1, 0);
  for (int i = 1; i <= 2 * (n - 1); i += 2)
    cout << edge[i].color << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int next;
  int to;
  int color;
} edge[200005];
int head[100005], cnt, m[100005], ans, n;

void add(int u, int v) {
  edge[++cnt].next = head[u];
  edge[cnt].to = v;
  head[u] = cnt;
}
void dfs(int x, int fa) {
  //	cout<<x<<endl;
  int us = 1, ban = 0;
  for (int i = head[x]; i; i = edge[i].next) {
    if (edge[i].to == fa) {
      //		cout<<i<<endl;
      ban = edge[i].color;
      break;
    }
  }
  //	cout<<x<<' '<<ban<<endl;
  for (int i = head[x]; i; i = edge[i].next) {

    if (edge[i].to == fa)
      continue; //	cout<<x<<' '<<us<<endl;
    if (us == ban)
      us++;
    edge[i].color = edge[i + 1].color = us;

    dfs(edge[i].to, x);
    us++;
  }
}
int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    if (u > v)
      swap(u, v);
    add(u, v);
    add(v, u);
    m[u]++;
    m[v]++;
  }
  for (int i = 1; i <= n; i++) {
    //		t[i]=m[i];
    ans = max(ans, m[i]);
  }
  cout << ans << endl;
  dfs(1, 0);
  for (int i = 1; i <= 2 * (n - 1); i += 2)
    cout << edge[i].color << endl;
  return 0;
}
Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using Edge = pair<int, int>; // (隣接頂点, 辺番号)
using Graph = vector<Edge>;
int N;

int main() {
  cin >> N;
  Graph G[N];
  for (int i = 0; i < N - 1; ++i) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    G[a].push_back(Edge(b, -1));
    G[b].push_back(Edge(a, -1));
  }
  int max_color = 0;
  for (int i = 0; i < N; ++i)
    chmax(max_color, (int)G[i].size());
  vector<int> res(N - 1, -1);

  vector<int> dist(N, -1);
  queue<pair<int, int>> que; // (頂点, 前回の色)
  que.push({0, -1});
  dist[0] = 0;
  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    int v = p.first, c = p.second;
    int color = 1;
    if (color == c)
      ++color;
    for (auto e : G[v]) {
      if (dist[e.first] == -1) {
        dist[e.first] = dist[v] + 1;
        que.push({e.first, color});
        res[e.second] = color;
        ++color;
        if (color == c)
          ++color;
      }
    }
  }
  cout << max_color << endl;
  for (auto v : res)
    cout << v << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using Edge = pair<int, int>; // (隣接頂点, 辺番号)
using Graph = vector<Edge>;
int N;

int main() {
  cin >> N;
  Graph G[N];
  for (int i = 0; i < N - 1; ++i) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    G[a].push_back(Edge(b, i));
    G[b].push_back(Edge(a, i));
  }
  int max_color = 0;
  for (int i = 0; i < N; ++i)
    chmax(max_color, (int)G[i].size());
  vector<int> res(N - 1, -1);

  vector<int> dist(N, -1);
  queue<pair<int, int>> que; // (頂点, 前回の色)
  que.push({0, -1});
  dist[0] = 0;
  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    int v = p.first, c = p.second;
    int color = 1;
    if (color == c)
      ++color;
    for (auto e : G[v]) {
      if (dist[e.first] == -1) {
        dist[e.first] = dist[v] + 1;
        que.push({e.first, color});
        res[e.second] = color;
        ++color;
        if (color == c)
          ++color;
      }
    }
  }
  cout << max_color << endl;
  for (auto v : res)
    cout << v << endl;
}
Problem p02850 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<vector<int>> G(n);
  vector<pii> 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);
  }

  int K = 0;

  vector<int> cs(n, 0);
  vector<int> used(n, 0);
  used[0] = 1;

  map<pii, int> I;
  queue<int> que;
  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 (cs[v] == cur)
        cur++;
      cs[u] = I[make_pair(u, v)] = I[make_pair(v, u)] = cur;
      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;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
typedef long long ll;
typedef pair<int, int> pii;

int main() {
  int n;
  cin >> n;
  vector<vector<int>> G(n);
  vector<pii> 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;

  vector<int> cs(n, 0);
  vector<int> used(n, 0);
  used[0] = 1;

  map<pii, int> I;
  queue<int> que;
  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 (cs[v] == cur)
        cur++;
      cs[u] = I[make_pair(u, v)] = I[make_pair(v, u)] = cur;
      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>
// #include <ext/numeric>

using namespace std;
// using namespace __gnu_cxx;

const int MAXN = (int)1e5 + 5;
vector<int> adjList[MAXN];
int mx = 0;
map<pair<int, int>, int> mp;

int getColor(int u, int v) {
  if (u > v)
    swap(u, v);
  return mp[{u, v}];
}

int setColor(int u, int v, int color) {
  if (u > v)
    swap(u, v);
  mp[{u, v}] = color;
}

void dfs(int u, int par) {
  int c = 1;
  int come = getColor(u, par);
  for (int v : adjList[u]) {
    if (v == par)
      continue;
    if (c == come)
      ++c;
    setColor(u, v, c);
    ++c;
    dfs(v, u);
  }
  mx = max(mx, max(c - 1, come));
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(10),
      cout << fixed;

  int n;
  cin >> n;
  vector<pair<int, int>> edges;
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    adjList[u].push_back(v);
    adjList[v].push_back(u);
    edges.push_back({u, v});
  }

  dfs(1, 0);

  cout << mx << '\n';
  for (auto e : edges) {
    cout << getColor(e.first, e.second) << '\n';
  }

  return 0;
}

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

using namespace std;
// using namespace __gnu_cxx;

const int MAXN = (int)1e5 + 5;
vector<int> adjList[MAXN];
int mx = 0;
map<pair<int, int>, int> mp;

int getColor(int u, int v) {
  if (u > v)
    swap(u, v);
  return mp[{u, v}];
}

void setColor(int u, int v, int color) {
  if (u > v)
    swap(u, v);
  mp[{u, v}] = color;
}

void dfs(int u, int par) {
  int c = 1;
  int come = getColor(u, par);
  for (int v : adjList[u]) {
    if (v == par)
      continue;
    if (c == come)
      ++c;
    setColor(u, v, c);
    ++c;
    dfs(v, u);
  }
  mx = max(mx, max(c - 1, come));
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(10),
      cout << fixed;

  int n;
  cin >> n;
  vector<pair<int, int>> edges;
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    adjList[u].push_back(v);
    adjList[v].push_back(u);
    edges.push_back({u, v});
  }

  dfs(1, 0);

  cout << mx << '\n';
  for (auto e : edges) {
    cout << getColor(e.first, e.second) << '\n';
  }

  return 0;
}
Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

ll n, ans[100010], ans1;
vector<pair<ll, ll>> ch[100010];
void dep(ll p, ll c) {
  ll cnt = 1;
  if (ch[p].size() == 1 && p != 0) {
    return;
  }
  for (ll i = 0; i < ch[p].size(); i++) {
    ll next = ch[p][i].first;
    ll noi = ch[p][i].second;
    if (ans[noi] != 0) {
      continue;
    }
    if (cnt == c) {
      cnt++;
    }
    ans[next] = cnt;
    ans1 = max(ans1, cnt);
    dep(next, cnt);
    cnt++;
  }
}
int main() {
  cin >> n;
  for (ll i = 0; i < n - 1; i++) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    ch[x].push_back(make_pair(y, i));
    ch[y].push_back(make_pair(x, i));
  }
  dep(0, 0);
  cout << ans1 << endl;
  for (ll i = 0; i < n - 1; i++) {
    cout << ans[i] << endl;
  }
  // your code goes here
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

ll n, ans[100010], ans1;
vector<pair<ll, ll>> ch[100010];
void dep(ll p, ll c) {
  ll cnt = 1;
  if (ch[p].size() == 1 && p != 0) {
    return;
  }
  for (ll i = 0; i < ch[p].size(); i++) {
    ll next = ch[p][i].first;
    ll noi = ch[p][i].second;
    if (ans[noi] != 0) {
      continue;
    }
    if (cnt == c) {
      cnt++;
    }
    ans[noi] = cnt;
    ans1 = max(ans1, cnt);
    dep(next, cnt);
    cnt++;
  }
}
int main() {
  cin >> n;
  for (ll i = 0; i < n - 1; i++) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    ch[x].push_back(make_pair(y, i));
    ch[y].push_back(make_pair(x, i));
  }
  dep(0, 0);
  cout << ans1 << endl;
  for (ll i = 0; i < n - 1; i++) {
    cout << ans[i] << endl;
  }
  // your code goes here
  return 0;
}
Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
void nuri(int, int);

int N, a[100000], b[100000], cco[100000] = {0}, coun[100000] = {0},
                             col[100000] = {0}, maxN;
vector<vector<int>> to;

int main() {
  maxN = 0;
  cin >> N;
  to.resize(N);
  for (int i = 0; i < N - 1; i++) {
    cin >> a[i] >> b[i];
    to[a[i]].push_back(i);
    to[b[i]].push_back(i);
    coun[a[i]]++;
    coun[b[i]]++;
    if (coun[a[i]] > maxN)
      maxN = coun[a[i]];
    if (coun[b[i]] > maxN)
      maxN = coun[b[i]];
  }
  cout << maxN << endl;
  nuri(1, -1);
  for (int i = 0; i < N - 1; i++)
    cout << col[i] << endl;
  return 0;
}
void nuri(int start, int nuse) {
  int j = 1, k;
  for (int i = 0; i < to[start].size(); i++) {
    k = to[start][i];
    if (j == nuse)
      j++;
    if (col[k] == 0) {
      col[k] = j;
      if (start == a[k])
        nuri(b[k], j);
      else
        nuri(a[k], j);
      j++;
    }
  }
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
void nuri(int, int);

int N, a[100000], b[100000], cco[100000] = {0}, coun[100000] = {0},
                             col[100000] = {0}, maxN;
vector<vector<int>> to;

int main() {
  maxN = 0;
  cin >> N;
  to.resize(N + 1);
  for (int i = 0; i < N - 1; i++) {
    cin >> a[i] >> b[i];
    to[a[i]].push_back(i);
    to[b[i]].push_back(i);
    coun[a[i]]++;
    coun[b[i]]++;
    if (coun[a[i]] > maxN)
      maxN = coun[a[i]];
    if (coun[b[i]] > maxN)
      maxN = coun[b[i]];
  }
  cout << maxN << endl;
  nuri(1, -1);
  for (int i = 0; i < N - 1; i++)
    cout << col[i] << endl;
  return 0;
}
void nuri(int start, int nuse) {
  int j = 1, k;
  for (int i = 0; i < to[start].size(); i++) {
    k = to[start][i];
    if (j == nuse)
      j++;
    if (col[k] == 0) {
      col[k] = j;
      if (start == a[k])
        nuri(b[k], j);
      else
        nuri(a[k], j);
      j++;
    }
  }
}
Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>

using namespace std;

const int N = 1e5 + 10;

int ans[N];
int to[N], nxt[N], id[N], lst[N];
inline void add(int u, int v, int i) {
  static int cnt = 0;
  ++cnt, to[cnt] = v, nxt[cnt] = lst[u], lst[u] = cnt, id[cnt] = i, ++cnt,
         to[cnt] = u, nxt[cnt] = lst[v], lst[v] = cnt, id[cnt] = i;
}

int n;

int deg[N];
inline void dfs(int x, int f, int c) {
  int i, y, nc = 0;
  for (i = lst[x], y = to[i]; i; i = nxt[i], y = to[i])
    if (y ^ f)
      nc = (nc + 1 == c ? c + 1 : nc + 1), ans[id[i]] = nc, dfs(y, x, nc);
  return;
}

int main() {
  int i, u, v;
  scanf("%d", &n);
  for (i = 1; i < n; i++)
    scanf("%d%d", &u, &v), add(u, v, i), deg[u]++, deg[v]++;
  int num = 0;
  for (i = 1; i <= n; i++)
    num = max(num, deg[i]);
  printf("%d\n", num);
  dfs(1, 1, 0);
  for (i = 1; i < n; i++)
    printf("%d\n", ans[i]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>

using namespace std;

const int N = 2e5 + 10;

int ans[N];
int to[N], nxt[N], id[N], lst[N];
inline void add(int u, int v, int i) {
  static int cnt = 0;
  ++cnt, to[cnt] = v, nxt[cnt] = lst[u], lst[u] = cnt, id[cnt] = i, ++cnt,
         to[cnt] = u, nxt[cnt] = lst[v], lst[v] = cnt, id[cnt] = i;
}

int n;

int deg[N];
inline void dfs(int x, int f, int c) {
  int i, y, nc = 0;
  for (i = lst[x], y = to[i]; i; i = nxt[i], y = to[i])
    if (y ^ f)
      nc = (nc + 1 == c ? c + 1 : nc + 1), ans[id[i]] = nc, dfs(y, x, nc);
  return;
}

int main() {
  int i, u, v;
  scanf("%d", &n);
  for (i = 1; i < n; i++)
    scanf("%d%d", &u, &v), add(u, v, i), deg[u]++, deg[v]++;
  int num = 0;
  for (i = 1; i <= n; i++)
    num = max(num, deg[i]);
  printf("%d\n", num);
  dfs(1, 1, 0);
  for (i = 1; i < n; i++)
    printf("%d\n", ans[i]);
  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++)
using namespace std;
typedef long long ll;
const int INF = 1e9;

struct Edge {
  int to, id;
};

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

int main() {
  int n;
  cin >> n;
  g.resize(n);
  ans.resize(n - 1);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    g[a].emplace_back((Edge){b, i});
    g[b].emplace_back((Edge){a, i});
  }
  queue<int> q;
  q.push(0);
  vector<int> used(n);
  used[0] = 1;
  while (q.size()) {
    int v = q.front();
    q.pop();
    int c = -1;

    rep(i, g[v].size()) {
      int u = g[v][i].to, ei = g[v][i].id;
      if (used[u])
        c = ans[ei];
    }

    int k = 1;
    rep(i, g[v].size()) {
      int u = g[v][i].to, ei = g[v][i].id;
      if (used[u])
        continue;
      if (k == c)
        k++;
      ans[ei] = k;
      k++;
      q.push(u);
    }
  }

  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;
const int INF = 1e9;

struct Edge {
  int to, id;
};

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

int main() {
  int n;
  cin >> n;
  g.resize(n);
  ans.resize(n - 1);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    g[a].emplace_back((Edge){b, i});
    g[b].emplace_back((Edge){a, i});
  }
  queue<int> q;
  q.push(0);
  vector<int> used(n);
  used[0] = 1;
  while (q.size()) {
    int v = q.front();
    q.pop();
    int c = -1;

    rep(i, g[v].size()) {
      int u = g[v][i].to, ei = g[v][i].id;
      if (used[u])
        c = ans[ei];
    }

    int k = 1;
    rep(i, g[v].size()) {
      int u = g[v][i].to, ei = g[v][i].id;
      if (used[u])
        continue;
      if (k == c)
        k++;
      ans[ei] = k;
      k++;
      q.push(u);
      used[u] = 1;
    }
  }

  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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
long long mod = 1000000007;
const int INF = 1001001001;

int main() {
  int n;
  cin >> n;
  vector<vector<int>> G(n, vector<int>(0));
  vector<pair<int, int>> vp(n - 1);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G.at(a).push_back(b);
    G.at(b).push_back(a);
    vp.at(i) = make_pair(a, b);
  }
  int K = 0;
  map<pair<int, int>, int> I; // 辺のラベルを管理
  vector<int> cs(n, -1); // 親との辺が何番になっているか？初期値は-1
  vector<bool> flag(n, false); // 頂点をcheckしたか
  queue<int> q;                // BFS用
  q.push(0);
  flag.at(0) = true;
  while (!q.empty()) {
    // v->uの辺を考える
    int v = q.front();
    q.pop();
    int label = 1;
    K = max(K, (int)G.at(v).size());
    for (auto u : G.at(v)) { // vの行先を全て調査
      if (flag.at(u) == true)
        continue;
      if (cs.at(v) == label)
        label++;
      cs.at(u) = label;
      I[make_pair(v, u)] = label;
      I[make_pair(u, v)] = label;
      label++;
      q.push(u);
      flag.at(u) = true;
    }
    cout << K << endl;
    for (auto p : vp) {
      cout << I.at(p) << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
long long mod = 1000000007;
const int INF = 1001001001;

int main() {
  int n;
  cin >> n;
  vector<vector<int>> G(n, vector<int>(0));
  vector<pair<int, int>> vp(n - 1);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G.at(a).push_back(b);
    G.at(b).push_back(a);
    vp.at(i) = make_pair(a, b);
  }
  int K = 0;
  map<pair<int, int>, int> I; // 辺のラベルを管理
  vector<int> cs(n, -1); // 親との辺が何番になっているか？初期値は-1
  vector<bool> flag(n, false); // 頂点をcheckしたか
  queue<int> q;                // BFS用
  q.push(0);
  flag.at(0) = true;
  while (!q.empty()) {
    // v->uの辺を考える
    int v = q.front();
    q.pop();
    int label = 1;
    K = max(K, (int)G.at(v).size());
    for (auto u : G.at(v)) { // vの行先を全て調査
      if (flag.at(u) == true)
        continue;
      if (cs.at(v) == label)
        label++;
      cs.at(u) = label;
      I[make_pair(v, u)] = label;
      I[make_pair(u, v)] = label;
      label++;
      q.push(u);
      flag.at(u) = true;
    }
  }
  cout << K << endl;
  for (auto p : vp) {
    cout << I.at(p) << endl;
  }
}
Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

vector<int> edge[10010];
vector<int> n_edge[10010];
int color[100010];
int Max = 0;

void allocate(int a, int prev, int prev_color) {
  int count = 0;
  for (int i = 0; i < edge[a].size(); i++) {
    if (edge[a][i] != prev) {
      count++;
      if (count == prev_color)
        count++;
      color[n_edge[a][i]] = count;
      allocate(edge[a][i], a, count);
    }
  }
  if (count > Max)
    Max = count;
}
int main(void) {
  int N;
  cin >> N;
  int a, b;
  for (int i = 0; i < N - 1; i++) {
    cin >> a >> b;
    edge[a].push_back(b);
    edge[b].push_back(a);
    n_edge[a].push_back(i);
    n_edge[b].push_back(i);
  }
  allocate(1, 0, N);
  cout << Max << endl;
  for (int i = 0; i < N - 1; i++) {
    cout << color[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

vector<int> edge[100010];
vector<int> n_edge[100010];
int color[100010];
int Max = 0;

void allocate(int a, int prev, int prev_color) {
  int count = 0;
  for (int i = 0; i < edge[a].size(); i++) {
    if (edge[a][i] != prev) {
      count++;
      if (count == prev_color)
        count++;
      color[n_edge[a][i]] = count;
      allocate(edge[a][i], a, count);
    }
  }
  if (count > Max)
    Max = count;
}
int main(void) {
  int N;
  cin >> N;
  int a, b;
  for (int i = 0; i < N - 1; i++) {
    cin >> a >> b;
    edge[a].push_back(b);
    edge[b].push_back(a);
    n_edge[a].push_back(i);
    n_edge[b].push_back(i);
  }
  allocate(1, 0, N);
  cout << Max << endl;
  for (int i = 0; i < N - 1; i++) {
    cout << color[i] << endl;
  }
  return 0;
}
Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> P;

const int L = 1e5;
vector<P> ps[L];
int cs[L];
int m = 0;
void f(int r, int p, int c) {
  m = max(m, c);
  int k = 1;
  for (auto n : ps[r]) {
    if (n.first == p)
      continue;
    if (k == c) {
      k++;
    }
    cs[n.second] = k;
    f(n.first, r, k);
    k++;
  }
}
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    ps[a].push_back(P(b, i));
    ps[b].push_back(P(a, i));
  }
  f(1, 0, 0);
  cout << m << endl;
  for (int i = 0; i < N - 1; i++) {
    cout << cs[i] << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> P;

const int L = 1e5 + 1;
vector<P> ps[L];
int cs[L];
int m = 0;
void f(int r, int p, int c) {
  m = max(m, c);
  int k = 1;
  for (auto n : ps[r]) {
    if (n.first == p)
      continue;
    if (k == c) {
      k++;
    }
    cs[n.second] = k;
    f(n.first, r, k);
    k++;
  }
}
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    ps[a].push_back(P(b, i));
    ps[b].push_back(P(a, i));
  }
  f(1, 0, 0);
  cout << m << endl;
  for (int i = 0; i < N - 1; i++) {
    cout << cs[i] << endl;
  }

  return 0;
}
Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
long long mo = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <class A> void PR(A a, ll n) {
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  cout << "\n";
}
ld PI = 3.14159265358979323846;

ll N;
vector<vector<ll>> G(100010);
vector<ll> memo(100010);
map<Pll, ll> mp;
ll K = 1;

ll dfs(ll v, ll c) {
  ll cc = 1;
  for (auto &u : G[v]) {
    if (memo[u] != 0)
      continue;
    memo[u] = 1;
    if (cc == c)
      cc++;
    cmax(K, cc);
    mp[Pll(v, u)] = cc;
    mp[Pll(u, v)] = cc;
    dfs(u, cc);
    cc++;
  }
}

int main() {
  cin >> N;
  ll a, b;
  vector<Pll> v;
  rep(i, N - 1) {
    cin >> a >> b;
    a--;
    b--;
    v.push_back(Pll(a, b));
    G[a].push_back(b);
    G[b].push_back(a);
  }
  memo[0] = 1;
  dfs(0, 0);
  cout << K << endl;
  rep(i, N - 1) { cout << mp[v[i]] << endl; }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
long long mo = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <class A> void PR(A a, ll n) {
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  cout << "\n";
}
ld PI = 3.14159265358979323846;

ll N;
vector<vector<ll>> G(100010);
vector<ll> memo(100010);
map<Pll, ll> mp;
ll K = 1;

ll dfs(ll v, ll c) {
  ll cc = 1;
  for (auto &u : G[v]) {
    if (memo[u] != 0)
      continue;
    memo[u] = 1;
    if (cc == c)
      cc++;
    cmax(K, cc);
    mp[Pll(v, u)] = cc;
    mp[Pll(u, v)] = cc;
    dfs(u, cc);
    cc++;
  }
  return 0;
}

int main() {
  cin >> N;
  ll a, b;
  vector<Pll> v;
  rep(i, N - 1) {
    cin >> a >> b;
    a--;
    b--;
    v.push_back(Pll(a, b));
    G[a].push_back(b);
    G[b].push_back(a);
  }
  memo[0] = 1;
  dfs(0, 0);
  cout << K << endl;
  rep(i, N - 1) { cout << mp[v[i]] << endl; }
}
Problem p02850 (C++)

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

// def
#define debug(x) cout << #x << ": " << x << endl
#define out(x) cout << x << endl
#define repeat(i, a, b) for (int i = (a); i < (b); i++)
#define revrepeat(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define revrep(i, n) for (int i = (n)-1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
const int M = 1e9 + 7;
const int INF = 1e9;
const int MAX_V = 1000 + 5;
const int MAX_N = 1e5 + 5;

bool seen[100020];

int main() {
  int n;
  cin >> n;
  vector<vector<int>> v(n);
  map<P, int> edge_color_map;
  vector<P> edges;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    edges.emplace_back(a, b);
  }
  vector<int> color(n - 1, 0);
  queue<int> que;
  seen[0] = true;
  que.push(0);
  int max_degree = 0;
  while (!que.empty()) {
    int top = que.front();
    que.pop();
    vector<int> next_vertexes = v[top];
    int curr = 1;
    for (int e : next_vertexes) {
      if (seen[e])
        continue;
      que.push(e);
      if (curr == color[top])
        curr++;
      edge_color_map[make_pair(top, e)] = curr;
      color[e] = curr;
      max_degree = max(max_degree, curr);
      curr++;
    }
    seen[top] = true;
  }
  cout << max_degree << endl;
  for (auto e : edges) {
    cout << edge_color_map[e] << endl;
  }
}


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

// def
#define debug(x) cout << #x << ": " << x << endl
#define out(x) cout << x << endl
#define repeat(i, a, b) for (int i = (a); i < (b); i++)
#define revrepeat(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define revrep(i, n) for (int i = (n)-1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
const int M = 1e9 + 7;
const int INF = 1e9;
const int MAX_V = 1000 + 5;
const int MAX_N = 1e5 + 5;

bool seen[100020];

int main() {
  int n;
  cin >> n;
  vector<vector<int>> v(n);
  map<P, int> edge_color_map;
  vector<P> edges;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    edges.emplace_back(a, b);
  }
  vector<int> color(n, 0);
  queue<int> que;
  seen[0] = true;
  que.push(0);
  int max_degree = 0;
  while (!que.empty()) {
    int top = que.front();
    que.pop();
    vector<int> next_vertexes = v[top];
    int curr = 1;
    for (int e : next_vertexes) {
      if (seen[e])
        continue;
      que.push(e);
      if (curr == color[top])
        curr++;
      edge_color_map[make_pair(top, e)] = curr;
      color[e] = curr;
      max_degree = max(max_degree, curr);
      curr++;
    }
    seen[top] = true;
  }
  cout << max_degree << endl;
  for (auto e : edges) {
    cout << edge_color_map[e] << endl;
  }
}
Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

#define reps(i, s, n) for (int(i) = (s); (i) < (n); (i)++)
#define rep(i, n) reps(i, 0, n)
#define max(a, b) a < b ? b : a
using namespace std;
using ll = long long;

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);
  }

  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;
      cur++;
      used[u] = 1;
      que.emplace(u);
    }
  }

  cout << K << endl;
  for (auto p : vp)
    cout << I[p] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

#define reps(i, s, n) for (int(i) = (s); (i) < (n); (i)++)
#define rep(i, n) reps(i, 0, n)
#define max(a, b) a < b ? b : a
using namespace std;
using ll = long long;

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);
  }

  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;
      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;

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); // color of edge between its parent

  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;
  }
}


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); // color of edge between its parent

  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;
  }
}
Problem p02850 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
#define int int64
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define SORT(x) sort(x.begin(), x.end())
#define GSORT(x) sort(x.begin(), x.end(), greater<int>())
#define mk make_pair
#define fi first
#define se second
#define pb push_back
#define ALL(x) x.begin(), x.end()
#define V(T) vector<T>
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef vector<int> vi;
typedef vector<vi> vvi;
int max(int a, int b) {
  if (b > a)
    return b;
  else
    return a;
}
int min(int a, int b) {
  if (b < a)
    return b;
  else
    return a;
}

const int MAX_N = 10050;
vvi nx(MAX_N);
vi ans(MAX_N, 0);

void dfs(int s) {

  // 新たな頂点なので、色番号をリセットする
  int cnt = 0;

  // 頂点sからのびる全ての辺に色を付けていく。
  // ただし、色番号は幅優先で付けられるが、頂点sのたどり方は深さ優先である。
  // すると、頂点sにたどり着くときに通った直前の辺の色がans[s]となり、
  // 新たな辺に色を付ける際は、ans[s]だけをスキップすればよい。
  rep(i, nx[s].size()) {

    // 頂点sからのびる辺に、1から順に数えて色番号を付けていく
    cnt++;

    // 直前の辺の色番号はスキップ
    if (cnt == ans[s])
      cnt++;

    // 確定した色番号を保存
    ans[nx[s][i]] = cnt;

    // 色が確定したら、その辺をたどった先の頂点について同様の操作を始める
    dfs(nx[s][i]);
  }
}

signed main() {
  int N;
  cin >> N;

  vi a(N), b(N);
  rep(i, N - 1) {
    cin >> a[i] >> b[i];
    nx[a[i]].pb(b[i]);
  }

  ans[1] = 0;
  dfs(1);

  int mx = 0;
  rep(i, N) { mx = max(mx, ans[i + 1]); }

  cout << mx << endl;
  rep(i, N - 1) { cout << ans[b[i]] << endl; }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
#define int int64
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define SORT(x) sort(x.begin(), x.end())
#define GSORT(x) sort(x.begin(), x.end(), greater<int>())
#define mk make_pair
#define fi first
#define se second
#define pb push_back
#define ALL(x) x.begin(), x.end()
#define V(T) vector<T>
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef vector<int> vi;
typedef vector<vi> vvi;
int max(int a, int b) {
  if (b > a)
    return b;
  else
    return a;
}
int min(int a, int b) {
  if (b < a)
    return b;
  else
    return a;
}

const int MAX_N = 100050;
vvi nx(MAX_N);
vi ans(MAX_N, 0);

void dfs(int s) {

  // 新たな頂点なので、色番号をリセットする
  int cnt = 0;

  // 頂点sからのびる全ての辺に色を付けていく。
  // ただし、色番号は幅優先で付けられるが、頂点sのたどり方は深さ優先である。
  // すると、頂点sにたどり着くときに通った直前の辺の色がans[s]となり、
  // 新たな辺に色を付ける際は、ans[s]だけをスキップすればよい。
  rep(i, nx[s].size()) {

    // 頂点sからのびる辺に、1から順に数えて色番号を付けていく
    cnt++;

    // 直前の辺の色番号はスキップ
    if (cnt == ans[s])
      cnt++;

    // 確定した色番号を保存
    ans[nx[s][i]] = cnt;

    // 色が確定したら、その辺をたどった先の頂点について同様の操作を始める
    dfs(nx[s][i]);
  }
}

signed main() {
  int N;
  cin >> N;

  vi a(N), b(N);
  rep(i, N - 1) {
    cin >> a[i] >> b[i];
    nx[a[i]].pb(b[i]);
  }

  ans[1] = 0;
  dfs(1);

  int mx = 0;
  rep(i, N) { mx = max(mx, ans[i + 1]); }

  cout << mx << endl;
  rep(i, N - 1) { cout << ans[b[i]] << endl; }

  return 0;
}
Problem p02850 (C++)

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

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, a, b) for (ll i = (a); i < (b); i++)
typedef uint64_t ull;
typedef int64_t ll;
typedef std::pair<ll, ll> PLL;

using namespace std;

ll N;
ll color[100100];
vector<PLL> edges[10100]; // {nxt, ID}: 次の頂点nxt, その辺のID

void dfs(ll cur, ll par, ll col) {
  ll i = 0;
  for (auto e : edges[cur]) {
    ll nxt = e.first, id = e.second;
    if (nxt == par) {
      continue;
    }
    while (i == col) {
      i++;
    }
    color[id] = i;
    dfs(nxt, cur, i);
    i++;
  }
}

signed main() {
  cin >> N;
  rep(i, N - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    edges[a].push_back({b, i});
    edges[b].push_back({a, i});
  }
  dfs(0, -1, -1);

  ll _max = 0;
  rep(i, N - 1) { _max = max(_max, color[i] + 1); }
  cout << _max << endl;
  rep(i, N - 1) { cout << color[i] + 1 << endl; }
}


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

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, a, b) for (ll i = (a); i < (b); i++)
typedef uint64_t ull;
typedef int64_t ll;
typedef std::pair<ll, ll> PLL;

using namespace std;

ll N;
ll color[100100];
vector<PLL> edges[100100]; // {nxt, ID}: 次の頂点nxt, その辺のID

void dfs(ll cur, ll par, ll col) {
  ll i = 0;
  for (auto e : edges[cur]) {
    ll nxt = e.first, id = e.second;
    if (nxt == par) {
      continue;
    }
    while (i == col) {
      i++;
    }
    color[id] = i;
    dfs(nxt, cur, i);
    i++;
  }
}

signed main() {
  cin >> N;
  rep(i, N - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    edges[a].push_back({b, i});
    edges[b].push_back({a, i});
  }
  dfs(0, -1, -1);

  ll _max = 0;
  rep(i, N - 1) { _max = max(_max, color[i] + 1); }
  cout << _max << endl;
  rep(i, N - 1) { cout << color[i] + 1 << endl; }
}
Problem p02850 (C++)

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

vector<pair<int, int>> edges[100000];
int ans[100000];

void dfs(int i, int par, int pc) {
  int c = 1;
  for (auto p : edges[i]) {
    int j = p.first, idx = p.second;
    // if(j == par) continue;
    if (c == pc)
      c++;
    ans[idx] = c;
    dfs(j, i, c);
    c++;
  }
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edges[a].emplace_back(b, i);
    edges[b].emplace_back(a, i);
  }
  dfs(0, -1, -1);
  cout << *max_element(ans, ans + N - 1) << endl;
  for (int i = 0; i < N - 1; i++)
    cout << ans[i] << endl;
  return 0;
}


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

vector<pair<int, int>> edges[100000];
int ans[100000];

void dfs(int i, int par, int pc) {
  int c = 1;
  for (auto p : edges[i]) {
    int j = p.first, idx = p.second;
    if (j == par)
      continue;
    if (c == pc)
      c++;
    ans[idx] = c;
    dfs(j, i, c);
    c++;
  }
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edges[a].emplace_back(b, i);
    edges[b].emplace_back(a, i);
  }
  dfs(0, -1, -1);
  cout << *max_element(ans, ans + N - 1) << endl;
  for (int i = 0; i < N - 1; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p02851 (C++)

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

typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pll;

#define all(x) x.begin(), x.end()
#define kill(x) return cout << x << endl, 0;
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define file_io                                                                \
  freopen("in.txt", "r", stdin);                                               \
  freopen("out.txt", "w", stdout);
#define ld_io cout << setprecision(10) << fixed;

ll pw(ll a, ll b, ll mod) {
  if (!b)
    return 1;
  if (b & 1)
    return a * pw(a * a % mod, b / 2, mod) % mod;
  return pw(a * a % mod, b / 2, mod) % mod;
}

const ll MAXN = 1e5 + 10;
const ll INF = 8e18;
const ll MOD = 1e9 + 7; // 998244353;
const ll LOG = 32;      // log2(1e9)
const ll BLOCK = 320;   // sqrt(1e5)

ll n, k, a[MAXN], ps[MAXN], ans;
map<ll, vector<ll>> v;

int main() {
  fastio ps[0] = 0;
  v[0].push_back(0);
  cin >> n >> k;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    a[i]--;
    ps[i] = ps[i - 1] + a[i];
    v[ps[i] % k].push_back(i);
  }
  for (auto &i : v) {
    for (ll j = 0; j < i.Y.size(); j++) {
      ans += lower_bound(all(i.Y), i.Y[j] + k) - i.Y.begin() - j - 1;
    }
  }
  cout << ans << endl;
  return 0;
}


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

typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pll;

#define all(x) x.begin(), x.end()
#define kill(x) return cout << x << endl, 0;
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define file_io                                                                \
  freopen("in.txt", "r", stdin);                                               \
  freopen("out.txt", "w", stdout);
#define ld_io cout << setprecision(10) << fixed;

ll pw(ll a, ll b, ll mod) {
  if (!b)
    return 1;
  if (b & 1)
    return a * pw(a * a % mod, b / 2, mod) % mod;
  return pw(a * a % mod, b / 2, mod) % mod;
}

const ll MAXN = 1e6 + 10;
const ll INF = 8e18;
const ll MOD = 1e9 + 7; // 998244353;
const ll LOG = 32;      // log2(1e9)
const ll BLOCK = 320;   // sqrt(1e5)

ll n, k, a[MAXN], ps[MAXN], ans;
map<ll, vector<ll>> v;

int main() {
  fastio ps[0] = 0;
  v[0].push_back(0);
  cin >> n >> k;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    a[i]--;
    ps[i] = ps[i - 1] + a[i];
    v[ps[i] % k].push_back(i);
  }
  for (auto &i : v) {
    for (ll j = 0; j < i.Y.size(); j++) {
      ans += lower_bound(all(i.Y), i.Y[j] + k) - i.Y.begin() - j - 1;
    }
  }
  cout << ans << endl;
  return 0;
}