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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.io.*;

class Main{
    public static void main(String args[])throws IOException{
      try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))){
        String con = br.readLine();
        int size = Integer.parseInt(con);

        List<String> color_list = new ArrayList<>();
        List<List<Boolean>> bool_list = new ArrayList<>();
        Boolean[][] bool_array = new Boolean[size][size];

        List<Integer> result_list = new ArrayList<>();

        for(Boolean[] x: bool_array){
          Arrays.fill(x, false);
        }

        for(int i = 0; i < size -1; i++){
          String[] str_array = br.readLine().split(" ");
          bool_array[Integer.parseInt(str_array[0]) - 1][Integer.parseInt(str_array[1]) - 1] = true;
          bool_array[Integer.parseInt(str_array[1]) - 1][Integer.parseInt(str_array[0]) - 1] = true;
        }

        color_list = init_string_list(size, "WHITE");
        bool_list = init_bool_list(bool_array);

        result_list = breadth_search(size, color_list, bool_list);
        
        System.out.println(Collections.max(result_list));
        result_list.forEach((i) -> {
          System.out.println(i);
        });
      }
    }

    public static List<Integer> breadth_search(int size, List<String> color_list, List<List<Boolean>> bool_list){
      List<Integer> origin_color = new ArrayList<>();
      List<Integer> result = new ArrayList<>();
      Map<Integer, Integer> mp_color = new HashMap<>();
      Map<Integer, List<Integer>> mp_list = new HashMap<>();

      for(int i=0; i < size-1; i++){
        origin_color.add(i+1);
      }

      Queue q = new Queue(size);
      int start = 0, min = 0, tmp;

      color_list.set(start, "GREY");
      q.enqueue(start);

      while(!(q.isEmpty())){
        List<Integer> tmp_list = new ArrayList<>();
        int var = (int)q.dequeue();

        for(int i = 0; i < size; i++){
          if(bool_list.get(var).get(i) && color_list.get(i).equals("WHITE")){  
            q.enqueue(i);
            color_list.set(i, "GREY");
            tmp_list.add(i+1);
          }
        }
        mp_list.put(var+1, tmp_list);
        color_list.set(var, "BLACK");
      }

      for(Integer key: mp_list.keySet()){
        List<Integer> tmp_color = new ArrayList<>(origin_color);
        if(key == 1){
          for(Integer i: mp_list.get(key)){
            min = Collections.min(tmp_color);
            tmp_color.remove(tmp_color.indexOf(min));
            mp_color.put(i, min);
            result.add(min);
          }
        }else{
          tmp = mp_color.get(key);
          tmp_color.remove(tmp_color.indexOf(tmp));
          for(Integer i: mp_list.get(key)){
            min = Collections.min(tmp_color);
            tmp_color.remove(tmp_color.indexOf(min));
            mp_color.put(i, min);
            result.add(min);
          }
        }
      }
      //System.out.println(mp_list);
      return result;
    }
    
    public static List<String> init_string_list(int size, String str){
      List<String> list = new ArrayList<>();
      for(int i=0; i < size; i++){
        list.add(str);
      }
      return list;
    }

    public static List<List<Boolean>> init_bool_list(Boolean[][] bool_array){
      List<List<Boolean>> list = new ArrayList<>();
      for(Boolean[] array : bool_array){
        list.add(Arrays.asList(array));
      }
      return list;
    }
}

class Queue{
  private Object queue[];
  private int queueSize;
  private int front;
  private int rear;

  private static final int default_size = 100;

  public Queue(){
    this(default_size);
  }

  public Queue(int size){
    queueSize = size;
    queue = new Object[queueSize];
    front = rear = 0;
  }

  private int next(int a){
    return (a + 1) % queueSize;
  }

  public boolean isEmpty(){
    return front == rear;
  }

  public void clear(){
    front = 0;
    rear = 0;
    Arrays.fill(queue, 0, queueSize, null);
  }

  public void enqueue(Object x){
    if(next(rear) == front){
      throw new IllegalStateException("Enq-Error");
    }else{
      queue[rear] = x;
      rear = next(rear);
    }

  }

  public Object dequeue(){
    if(front == rear){
      throw new NoSuchElementException("Deq-Error");
    }else{
      Object x = queue[front];
      queue[front] = null;
      front = next(front);
      return x;
    }

  }
}