Problem:
Score : 400 points

Problem Statement

Given is a tree G with N vertices.
The vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i.

Consider painting the edges in G with some number of colors.
We want to paint them so that, for each vertex, the colors of the edges incident to that vertex are all different.

Among the colorings satisfying the condition above, construct one that uses the minimum number of colors.

Constraints

2 \le N \le 10^5

1 \le a_i \lt b_i \le N

All values in input are integers.

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}

Output

Print N lines.

The first line should contain K, the number of colors used.

The (i+1)-th line (1 \le i \le N-1) should contain c_i, the integer representing the color of the i-th edge, where 1 \le c_i \le K must hold.

If there are multiple colorings with the minimum number of colors that satisfy the condition, printing any of them will be accepted.

Sample Input 1

3
1 2
2 3

Sample Output 1

2
1
2

Sample Input 2

8
1 2
2 3
2 4
2 5
4 7
5 6
6 8

Sample Output 2

4
1
2
3
4
1
1
2

Sample Input 3

6
1 2
1 3
1 4
1 5
1 6

Sample Output 3

5
1
2
3
4
5

Solution in Rust (1.42.0) (judge verdict: Accepted)

// -*- coding:utf-8-unix -*-

#[macro_use]
extern crate lazy_static;
extern crate num_bigint; // 0.2.2
extern crate num_traits; // 0.2.8
use num_bigint::BigInt;
use num_bigint::ToBigInt;
use num_traits::Pow;
use num_traits::{One, Zero};

// use proconio::derive_readable;
use proconio::fastout;
use proconio::input;
// use std::convert::TryInto;
// use ascii::Chars;
use libm::*;
use proconio::marker::{Bytes, Chars};
use std::cmp::*;
use std::collections::{BinaryHeap, HashMap, HashSet, VecDeque};
use std::io::*;
use std::ops::Range;
use std::str::FromStr;
use superslice::*;

use lazy_static::lazy_static;
use std::sync::Mutex;

pub fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

lazy_static! {
    // static ref CHILDS: Mutex<Vec<Vec<usize>>> = Mutex::default();
    static ref ADJ: Mutex<Vec<Vec<usize>>> = Mutex::default();
    static ref PARENTS: Mutex<Vec<usize>> = Mutex::default();
    static ref TOIDX: Mutex<HashMap<(usize, usize), usize>> = Mutex::default();
}

// const MOD: usize = 9997063;
// const MOD: usize = 100_000_000_ + 7;
const LARGEN: usize = 10000000;
//abc146-D
// #[fastout]
fn main() {
    input![n: usize, ab: [(usize, usize); n - 1]];

    // let mut adjs = ADJ.lock().unwrap();
    let mut adjs = vec![Vec::new(); n];
    {
        let mut edgeToIdx = TOIDX.lock().unwrap();
        for (idx, (a, b)) in ab.iter().enumerate() {
            let a = a - 1;
            let b = b - 1;
            adjs[a].push(b);
            adjs[b].push(a);
            edgeToIdx.insert((a, b), idx);
        }
    }

    {
        let mut parents = PARENTS.lock().unwrap();
        *parents = vec![LARGEN; n];
        // eprintln!("{:?}", parents);
    }

    let mut is_visits = vec![false; n];
    let min_color = make_tree(0, &mut is_visits, &adjs);

    let mut colors = vec![10000000; n - 1];
    let mut is_visits = vec![false; n];

    let mut cant_use_color = None;
    make_color(0, cant_use_color, &mut colors, &mut is_visits, &adjs);

    {
        let parents = PARENTS.lock().unwrap();
        // eprintln!("{:?}", parents);
    }
    println!("{}", min_color);
    for c in colors.iter() {
        println!("{}", c + 1);
    }
    // eprintln!("{:?}", adj);
    // println!("{}", ans);
}

fn make_color(
    start: usize,
    cant_use_color: Option<usize>,
    colors: &mut Vec<usize>,
    is_visits: &mut Vec<bool>,
    adjs: &Vec<Vec<usize>>,
) {
    // eprintln!("make_color: {}", start);
    if is_visits[start] {
        return;
    }

    is_visits[start] = true;
    let mut is_root = true;
    // let adj = &ADJ.lock().unwrap()[start].clone();
    // let mut anss = Vec::new();

    let mut usable = 0;

    for to in adjs[start].iter() {
        if is_visits[*to] {
            {
                let mut parents = PARENTS.lock().unwrap();
                parents[start] = *to;
            }
            is_root = false;
            continue;
        }

        {
            let edgeToIdx = TOIDX.lock().unwrap();
            if let Some(cant_use) = cant_use_color {
                if cant_use == usable {
                    usable += 1;
                }
            }
            let edge_idx = *edgeToIdx.get(&(start, *to)).unwrap();
            colors[edge_idx] = usable;
            // eprintln!(
            //     "start: {}, to: {}, edge: {}, c: {}",
            //     start, to, edge_idx, usable
            // );
        }

        make_color(*to, Some(usable), colors, is_visits, adjs);
        usable += 1;
    }
}

// (myself, max)
fn make_tree(start: usize, is_visits: &mut Vec<bool>, adjs: &Vec<Vec<usize>>) -> usize {
    // if is_visits[start] {
    //     return ;
    // }

    is_visits[start] = true;
    let mut is_root = true;
    // let adj = &ADJ.lock().unwrap()[start].clone();
    let mut anss = Vec::new();
    for to in adjs[start].iter() {
        if is_visits[*to] {
            {
                let mut parents = PARENTS.lock().unwrap();
                parents[start] = *to;
            }
            is_root = false;
            continue;
        }

        anss.push(make_tree(*to, is_visits, adjs));
    }

    let ans = anss
        .iter()
        // .enumerate()
        // .map(|(min_color)| min_color)
        .max()
        .map(|min_color| max(*min_color, adjs[start].len()))
        .unwrap_or(1);

    // eprintln!("make_tree: {}, {}", start, ans);

    return ans;
}

// let mut values = VALUES.lock().unwrap();
// values.extend_from_slice(&[1, 2, 3, 4]);
// assert_eq!(&*values, &[1, 2, 3, 4]);

// -100000000
//  1000000000