abc146_d - Coloring Edges On Tree

Click to show code.


using namespace std;
using ii = pair<int, int>;
using vii = vector<ii>;
int const NMAX = 1e5 + 11;
int n, ncolors, color[NMAX];
vii g[NMAX];
void dfs(int u, int p = -1, int c = -1)
{
    int free_color = 1;
    for (auto [v, i] : g[u])
    {
        if (v == p)
            continue;
        if (free_color == c)
            ++free_color;
        color[i] = free_color;
        dfs(v, u, free_color);
        ++free_color;
    }
    ncolors = max(ncolors, free_color - 1);
}
int main(void)
{
    int u, v;
    cin >> n;
    for (int i = 0; i < n - 1; ++i)
    {
        cin >> u >> v, u--, v--;
        g[u].emplace_back(v, i);
        g[v].emplace_back(u, i);
    }
    dfs(1);
    cout << ncolors << endl;
    for (int i = 0; i < n - 1; ++i)
        cout << color[i] << endl;
    return 0;
}