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.Generic;
using System.Linq;

namespace AtCoder
{
    class Program
    {
        static void Main(string[] args)
        {
            input_iter = LazyLoadAllLines();
            //------------------------------------------------//
            /*var a = Read<long>();
            var b = Read<long>();
            var x = Read<long>();*/
            var n = Read<long>();
            //var s = Read();
            /*List<string> sls = new List<string>();
            for(int i = 0; i < h; ++i)
            {
                sls.Add(Read());
            }*/
            //var als = Read<long>(n);
            //var fls = Read<long>(n);
            //List<long> als = new List<long>(), bls = new List<long>();
            /*List<List<long>> dcls = new List<List<long>>();
            for (int i = 0; i < m; ++i)
            {
                dcls.Add(Read<long>(2));
            }*/
            List<List<long>> abls = new List<List<long>>();
            //List<long> cls = new List<long>();
            List<long> edgeNum = Enumerable.Repeat(0L, (int)n).ToList();
            for (int i = 0; i < n - 1; ++i)
            {
                abls.Add(Read<long>(2));
                abls[i].Add(i);
                abls[i].Add(-1);
                --abls[i][0];
                --abls[i][1];
                //cls.Add(Read<long>());
                ++edgeNum[(int)abls[i][0]];
                ++edgeNum[(int)abls[i][1]];
            }

            var colorNum = edgeNum.Max();
            List<long> parentColor = Enumerable.Repeat(-1L, (int)n).ToList();
            abls.Sort((List<long> l, List<long> r) => (int)(l[0] - r[0]));

            Console.WriteLine(colorNum);
            var prevNode = -1L;
            var currentCol = 1;
            foreach(var ab in abls)
            {
                if (prevNode == ab[0]) ++currentCol;
                else currentCol = 1;

                if (currentCol == parentColor[(int)ab[0]]) ++currentCol;

                parentColor[(int)ab[1]] = currentCol;
                ab[3] = currentCol;

                prevNode = ab[0];
            }

            abls.Sort((List<long> l, List<long> r) => (int)(l[2] - r[2]));
            foreach (var ab in abls) { Console.WriteLine(ab[3]); };

            //als.Sort((l, r) => (int)(l - r));

                /*var nMax = (long)Math.Min(x / a, (x / b + 1) > 18 ? Math.Pow(10, 18) : Math.Pow(10, 1.0 * x / b + 1));
                nMax = Math.Min(nMax, (long)Math.Pow(10, 9));
                long item = 0;
                long nMin = 1;
                for(long i = nMax; i > 0; i /= 3)
                {
                    if(a * i + b * GetDigit(i) <= x)
                    {
                        nMin = i;
                        break;
                    }
                }

                if(nMax == nMin)
                {
                    Console.WriteLine(nMax);
                    return;
                }

                for (long i = nMin; i <= nMax; ++i)
                {
                    if (a * i + b * GetDigit(i) > x)
                    {
                        item = i - 1;
                        break;
                    }
                }

                a*i+b*(Math.Log10(i) + 1) <= x

                Console.WriteLine(item);*/
        }

        static int GetDigit(long num)
        {
            return (num == 0) ? 1 : ((int)Math.Log10(num) + 1);
        }

        static IEnumerator<string> input_iter;

        static _T Read<_T>() where _T : struct
        {
            input_iter.MoveNext();
            return (_T)Convert.ChangeType(input_iter.Current, typeof(_T));
        }
        static string Read()
        {
            input_iter.MoveNext();
            return input_iter.Current;
        }
        static List<_T> Read<_T>(long num) where _T : struct { return new List<_T>(Enumerate<_T>(num)); }


        static IEnumerable<_T> Enumerate<_T>(long num)
        {
            for (long i = 0; i < num; ++i)
            {
                input_iter.MoveNext();
                yield return (_T)Convert.ChangeType(input_iter.Current, typeof(_T));
            }
        }
        static IEnumerator<string> LazyLoadAllLines()
        {
            string line;
            do
            {
                line = Console.ReadLine();
                foreach (var s in line.Split(' '))
                    yield return s;
            } while (line != null);
        }

        public class PriorityQueue<T> where T : IComparable
        {
            private IComparer<T> comparer = null;
            private bool isAccendingOrder;

            private List<T> heap;
            private int heapCurrentSize = 0;

            //public int Count { get; private set; } = 0;

            public PriorityQueue(IComparer<T> comparer, int initialHeapSize = 256)
            {
                heap = new List<T>(initialHeapSize);
                this.comparer = comparer;
            }

            public PriorityQueue(bool isAccending = true, int initialHeapSize = 256)
            {
                heap = new List<T>(initialHeapSize);
                isAccendingOrder = isAccending;
            }

            private int Compare(T x, T y)
            {
                if (comparer != null) return comparer.Compare(x, y);
                return isAccendingOrder ? x.CompareTo(y) : y.CompareTo(x);
            }

            public bool Empty()
            {
                return Count() == 0;
            }

            public int Count()
            {
                return heapCurrentSize;
            }

            public void Push(T x)
            {
                //node number
                var idx = heapCurrentSize++;
                if (heapCurrentSize > heap.Count) heap.Capacity *= 2;

                while (idx > 0)
                {
                    //parent node number
                    var parentIdx = (idx - 1) / 2;

                    if (Compare(heap[parentIdx], x) <= 0) break;

                    heap[idx] = heap[parentIdx];
                    idx = parentIdx;
                }

                heap[idx] = x;
            }

            public T Pop()
            {
                T ret = heap[0];
                T x = heap[--heapCurrentSize];

                int idx = 0;
                while (idx * 2 + 1 < heapCurrentSize)
                {
                    //children
                    int a = idx * 2 + 1;
                    int b = idx * 2 + 2;

                    if (b < heapCurrentSize && Compare(heap[b], heap[a]) < 0) a = b;

                    if (Compare(heap[a], x) >= 0) break;

                    heap[idx] = heap[a];
                    idx = a;
                }

                heap[idx] = x;

                return ret;
            }

            public T Peek()
            {
                return heap[0];
            }

            public bool Contains(T x)
            {
                for (int i = 0; i < heapCurrentSize; i++) if (x.Equals(heap[i])) return true;
                return false;
            }

            public void Clear()
            {
                while (heapCurrentSize > 0) Pop();
            }

            public IEnumerator<T> GetEnumerator()
            {
                var temp = new List<T>();

                while (heapCurrentSize > 0)
                {
                    temp.Add(Pop());
                }

                foreach (var value in temp)
                {
                    Push(value);
                    yield return value;
                }
            }

            public T[] ToArray()
            {
                T[] array = new T[heapCurrentSize];
                int i = 0;

                foreach (var r in this)
                {
                    array[i] = r;
                    ++i;
                }

                return array;
            }
        }
    }
}