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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main implements Runnable { // クラス名はMainD1

	public static void main(String[] args) {
		Thread.setDefaultUncaughtExceptionHandler((t, e) -> System.exit(1));
		new Thread(null, new Main(), "", 1024 * 1024 * 1024).start(); // 16MBスタックを確保して実行
	}

	/**
	 * for (int i = 0; i < N; i++) {} System.out.println(); HashMap<Integer,
	 * Integer> hm = new HashMap<>(); int[] s = new int[M]; ArrayDeque<Integer> deq
	 * = new ArrayDeque<>();
	 *
	 * 文字列のSort String sorted = S.chars() .sorted() .collect(StringBuilder::new,
	 * StringBuilder::appendCodePoint, StringBuilder::append) .toString(); for (char
	 * i = 'a'; i <= 'z'; i++) {}
	 */
	public void run() {
		FastScannerMainD1 sc = new FastScannerMainD1(System.in);

		try {
			int N = sc.nextInt();
			NodeList[] nls = new NodeList[N];
			EdgeList[] els = new EdgeList[N - 1];

			for (int i = 0; i < N; i++) {
				NodeList nodelist = new NodeList();
				nls[i] = nodelist;
			}
			for (int i = 0; i < N - 1; i++) {
				int a = sc.nextInt() - 1;
				int b = sc.nextInt() - 1;
				nls[a].neighbours.add(b);
				// nls[b].neighbours.add(a);
				EdgeList el = new EdgeList();
				el.i = i;
				el.from = a;
				el.to = b;
				nls[a].edges.add(el);
				// nls[b].edges.add(el);

				els[i] = el;
			}

			bfs(0, nls, N);

			// for (char i = 'a'; i <= 'z'; i++) {}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void bfs(int start, NodeList[] nls, int N) {
		PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
		pq.offer(start);
		boolean[] visit = new boolean[N];
		visit[start] = true;
		int maxcolor = 0;

		while (pq.size() > 0) {
			int loc = pq.poll();
			int color = 1;
			visit[loc] = true;

			for (int i = 0; i < nls[loc].neighbours.size(); i++) {
				// System.out.println(
				// "color:" + color + ",loc:" + loc + " ,i:" + i + " ,from:" +
				// nls[loc].edges.get(i).from + " ,to:"
				// + nls[loc].edges.get(i).to + ",colors:" + nls[loc].edges.get(i).color + " ,
				// nei:"
				// + nls[loc].neighbours.get(i) + " visit:" +
				// visit[nls[loc].neighbours.get(i)]);

				if (!visit[nls[loc].neighbours.get(i)]) {
					while (nls[loc].colors.contains(color)) {
						color++;
					}
					nls[loc].edges.get(i).color = color;
					nls[loc].colors.add(color);
					nls[nls[loc].neighbours.get(i)].colors.add(color);
					pq.offer(nls[loc].neighbours.get(i));
					if (maxcolor < color) {
						maxcolor = color;
					}
					color++;
				}
			}

		}

		/*
		 * System.out.println("---------"); for (int i = 0; i < N - 1; i++) {
		 * System.out.println(i + " " + nls[i].neighbours); }
		 * System.out.println("---------"); for (int i = 0; i < N; i++) {
		 * System.out.println("--"); for (int j = 0; j < nls[i].edges.size(); j++) {
		 * System.out.println(i + " ,from:" + nls[i].edges.get(j).from + " ,to:" +
		 * nls[i].edges.get(j).to + ",color:" + nls[i].edges.get(j).color); } }
		 */
		// System.out.println("Ans---------");
		System.out.println(maxcolor);
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < nls[i].edges.size(); j++) {
				System.out.println(nls[i].edges.get(j).color);
			}
		}

	}
}

class NodeList {
	ArrayList<Integer> neighbours = new ArrayList<Integer>();
	ArrayList<EdgeList> edges = new ArrayList<EdgeList>();
	ArrayList<Integer> colors = new ArrayList<Integer>();
}

class EdgeList {
	int i;
	int from;
	int to;
	int color;
}

// 高速なScanner
class FastScannerMainD1 {
	private BufferedReader reader = null;
	private StringTokenizer tokenizer = null;

	public FastScannerMainD1(InputStream in) {
		reader = new BufferedReader(new InputStreamReader(in));
		tokenizer = null;
	}

	public String next() {
		if (tokenizer == null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken();
	}

	public String nextLine() {
		if (tokenizer == null || !tokenizer.hasMoreTokens()) {
			try {
				return reader.readLine();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken("\n");
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}

	public int[] nextIntArray(int n) {
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = nextInt();
		return a;
	}

	public long[] nextLongArray(int n) {
		long[] a = new long[n];
		for (int i = 0; i < n; i++)
			a[i] = nextLong();
		return a;
	}
}