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;

// 色を決める
int paint(int before) {
    for(int i=1; i<k+1; i++) {
        if(i != before) {
            return i;
        } 
    }
}

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:
a.cc: In function 'int paint(int)':
a.cc:28:1: warning: control reaches end of non-void function [-Wreturn-type]
   28 | }
      | ^
/tmp/ccH1zcRo.o: in function `paint(int)':
a.cc:(.text+0x23): relocation truncated to fit: R_X86_64_PC32 against symbol `k' defined in .bss section in /tmp/ccH1zcRo.o
/tmp/ccH1zcRo.o: in function `solve()':
a.cc:(.text+0x6a): relocation truncated to fit: R_X86_64_PC32 against symbol `que' defined in .bss section in /tmp/ccH1zcRo.o
a.cc:(.text+0x97): relocation truncated to fit: R_X86_64_PC32 against symbol `que' defined in .bss section in /tmp/ccH1zcRo.o
a.cc:(.text+0xb9): relocation truncated to fit: R_X86_64_PC32 against symbol `que' defined in .bss section in /tmp/ccH1zcRo.o
a.cc:(.text+0x103): relocation truncated to fit: R_X86_64_PC32 against symbol `side' defined in .bss section in /tmp/ccH1zcRo.o
a.cc:(.text+0x16e): relocation truncated to fit: R_X86_64_PC32 against symbol `k' defined in .bss section in /tmp/ccH1zcRo.o
a.cc:(.text+0x1bb): relocation truncated to fit: R_X86_64_PC32 against symbol `que' defined in .bss section in /tmp/ccH1zcRo.o
a.cc:(.text+0x1e4): relocation truncated to fit: R_X86_64_PC32 against symbol `que' defined in .bss section in /tmp/ccH1zcRo.o
/tmp/ccH1zcRo.o: in function `main':
a.cc:(.text+0x2ab): relocation truncated to fit: R_X86_64_PC32 against symbol `side' defined in .bss section in /tmp/ccH1zcRo.o
a.cc:(.text+0x2c6): relocation truncated to fit: R_X86_64_PC32 against symbol `side_num' defined in .bss section in /tmp/ccH1zcRo.o
a.cc:(.text+0x2e0): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status