Language: C++

/**
 * D - Coloring Edges on Tree
 * https://atcoder.jp/contests/abc146/tasks/abc146_d
 **/
#include<iostream>
#include<vector>
#include<queue>

using namespace std;

const int MAX_N = 100000;

int n;
int c[MAX_N+1][MAX_N+1];
int side[MAX_N+1][MAX_N+1];
int side_num[MAX_N+1];
int k;

queue<pair<int, int>> que;

void solve() {
    que.push(pair<int, int>(1, 0));
    while(!que.empty()) {
        int tmp_c[MAX_N+1] = {};
        // キューから頂点を一つ取り出す
        pair<int, int> p = que.front();
        int tmp_n = p.first;
        int before_color = p.second;
        que.pop();
        tmp_c[before_color] = 1;
        // 全ての分岐を一つすすめる
        for(int i=tmp_n+1;i<n+1;i++) {
            if(side[tmp_n][i] == 1) {
                // 色の決定
                for(int j=1; j<k+1; j++) {
                    if(tmp_c[j] == 0) {
                        c[tmp_n][i] = j;
                        tmp_c[j] = 1;
                        break;
                    }
                }
                // キューに追加
                que.push(pair<int, int>(i, c[tmp_n][i]));
            }
        }
    }
}

int main() {
    queue<pair<int, int>> pair_n;
    cin >> n;
    for (int i = 1; i<n; i++) {
        int tmp_a, tmp_b;
        cin >> tmp_a >> tmp_b;
        pair_n.push(pair<int, int>(tmp_a, tmp_b));
        side[tmp_a][tmp_b] = 1;
        side_num[tmp_a] += 1;
        side_num[tmp_b] += 1;
    }
    // 色の数
    for (int i=1;i<n+1;i++) {
        if(k < side_num[i]) {
            k = side_num[i];
        }
    }
    // 色決定
    solve();

    // 解答出力
    cout << k << endl;
    while(!pair_n.empty()) {
        pair<int, int> p = pair_n.front();
        pair_n.pop();
        cout << c[p.first][p.second] << endl;
    }

}

Compiler output:
/tmp/cc1vQ8rE.o: in function `solve()':
a.cc:(.text+0x3a): relocation truncated to fit: R_X86_64_PC32 against symbol `que' defined in .bss section in /tmp/cc1vQ8rE.o
a.cc:(.text+0x67): relocation truncated to fit: R_X86_64_PC32 against symbol `que' defined in .bss section in /tmp/cc1vQ8rE.o
a.cc:(.text+0x89): relocation truncated to fit: R_X86_64_PC32 against symbol `que' defined in .bss section in /tmp/cc1vQ8rE.o
a.cc:(.text+0xd3): relocation truncated to fit: R_X86_64_PC32 against symbol `side' defined in .bss section in /tmp/cc1vQ8rE.o
a.cc:(.text+0x13e): relocation truncated to fit: R_X86_64_PC32 against symbol `k' defined in .bss section in /tmp/cc1vQ8rE.o
a.cc:(.text+0x18b): relocation truncated to fit: R_X86_64_PC32 against symbol `que' defined in .bss section in /tmp/cc1vQ8rE.o
a.cc:(.text+0x1b4): relocation truncated to fit: R_X86_64_PC32 against symbol `que' defined in .bss section in /tmp/cc1vQ8rE.o
/tmp/cc1vQ8rE.o: in function `main':
a.cc:(.text+0x27b): relocation truncated to fit: R_X86_64_PC32 against symbol `side' defined in .bss section in /tmp/cc1vQ8rE.o
a.cc:(.text+0x296): relocation truncated to fit: R_X86_64_PC32 against symbol `side_num' defined in .bss section in /tmp/cc1vQ8rE.o
a.cc:(.text+0x2b0): relocation truncated to fit: R_X86_64_PC32 against symbol `side_num' defined in .bss section in /tmp/cc1vQ8rE.o
a.cc:(.text+0x2c7): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status