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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Math;
using static System.Console;


class Program
{
    static long[] sizes = new long[51];

    public class Tree
    {
        public class Node
        {
            public int? Parent = null;
            public List<int[]> Children = new List<int[]>();
            public int ParentColor = -1;
        }

        public Tree(int nodeNumber)
        {
            Nodes = new Node[nodeNumber];
            for (int i = 0; i < Nodes.Length; i++)
            {
                Nodes[i] = new Node();
                Connenters = new List<int[]>();

            }
        }

        /// <summary>
        /// 頂点の配列。それぞれの頂点には、一つの親と、複数の子がある
        /// </summary>
        public Node[] Nodes { get; }

        public List<int[]> Connenters { get; }

        public void Connent(int to ,int from,int id = -1)
        {
            Connenters.Add(new int[3] { to, from, 0 });
            Nodes[to].Children.Add(new int[2] {from,id });
            Nodes[from].Parent = to;
        }


    }

    static void Main()
    {
        int N = ReadInt();
        Tree tree = new Tree(N);

        for (int i = 0; i < N-1; i++)
        {
            int[] ab = ReadIntN();
            tree.Connent(ab[0]-1, ab[1]-1,i);
        }

        int[] colors = new int[N - 1];

        Queue<Tree.Node> queue = new Queue<Tree.Node>();
        queue.Enqueue(tree.Nodes[0]);
        int[] ans = new int[N - 1];
        int colorNum = 0;
        while (queue.Count >0)
        {
            Tree.Node node = queue.Dequeue();
            int pCol = node.ParentColor;

            int nextColor = 0;
            for (int i = 0; i < node.Children.Count; i++)
            {
                if (nextColor == pCol) nextColor++;
                int[] childNode = node.Children[i];
                ans[childNode[1]] = nextColor+1;
                tree.Nodes[childNode[0]].ParentColor = nextColor;
                colorNum = Max(colorNum, nextColor);
                nextColor++;
                queue.Enqueue(tree.Nodes[childNode[0]]);
            }
        }

        WriteLine(colorNum+1);
        WriteLine(string.Join("\n",ans));
        ReadLine();
    }

    static long F(long N,long X)
    {
        if (X == 1 && N == 0) return 1;
        else if (X == 1) return 0;
        else if (X > 1 && X < sizes[N] / 2 + 1) return F(N - 1, X - 1);
        else if (X == sizes[N] / 2 + 1) return F(N - 1, sizes[N - 1]) + 1;
        else if (X > sizes[N] / 2 + 1 && X < sizes[N])
        {
            return F(N - 1, sizes[N - 1]) + 1 + F(N - 1, X - (sizes[N] / 2 + 1));
        }
        else return 2 * F(N - 1, sizes[N - 1]) + 1;

    }

    #region Reader
    static string ReadStr()
    {
        return Console.ReadLine();
    }
    static string[] ReadStrN()
    {
        return Console.ReadLine().Split(' ');
    }
    static int ReadInt()
    {
        return Convert.ToInt32(Console.ReadLine());
    }
    static int[] ReadIntN()
    {
        return Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray();
    }
    static long ReadLong()
    {
        return Convert.ToInt64(Console.ReadLine());
    }
    static long[] ReadLongN()
    {
        return Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray();
    }
    static long[,] ReadLongMatrix(int r, int c)
    {
        var mat = new long[r, c];
        for (int i = 0; i < r; ++i)
        {
            var x = ReadLongN();
            for (int j = 0; j < c; ++j)
            {
                mat[i, j] = x[j];
            }
        }
        return mat;
    }
    static Tuple<string, string>[] ReadTupleStr(int n)
    {
        List<Tuple<string, string>> list = new List<Tuple<string, string>>();
        for (int i = 0; i < n; ++i)
        {
            var ab = ReadStrN();
            list.Add(new Tuple<string, string>(ab[0], ab[1]));
        }
        return list.ToArray();
    }
    static Tuple<long, long>[] ReadTupleLong(int n)
    {
        List<Tuple<long, long>> list = new List<Tuple<long, long>>();
        for (int i = 0; i < n; ++i)
        {
            var ab = ReadLongN();
            list.Add(new Tuple<long, long>(ab[0], ab[1]));
        }
        return list.ToArray();
    }
    static Tuple<int, int>[] ReadTupleInt(int n)
    {
        List<Tuple<int, int>> list = new List<Tuple<int, int>>();
        for (int i = 0; i < n; ++i)
        {
            var ab = ReadIntN();
            list.Add(new Tuple<int, int>(ab[0], ab[1]));
        }
        return list.ToArray();
    }
    static Tuple<double, double>[] ReadTupleDouble(int n)
    {
        List<Tuple<double, double>> list = new List<Tuple<double, double>>();
        for (int i = 0; i < n; ++i)
        {
            var ab = ReadLongN();
            list.Add(new Tuple<double, double>(ab[0], ab[1]));
        }
        return list.ToArray();
    }
    static Dictionary<int, HashSet<int>> ReadAdjacencyList(int N, int M)
    {
        Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>();
        for (int i = 0; i <= N; ++i)
        {
            dict.Add(i, new HashSet<int>());
        }
        for (int i = 0; i < M; ++i)
        {
            var ab = ReadIntN();
            dict[ab[0]].Add(ab[1]);
            dict[ab[1]].Add(ab[0]);
        }
        return dict;
    }
    #endregion
}