Road Repair in Treeland
Contest: Kotlin Heroes: Episode 2
Difficulty rating: 3100
Tags: *special, binary search, dp, trees

There are $$$n$$$ cities and $$$n-1$$$ two-way roads in Treeland. Each road connects a pair of different cities. From any city you can drive to any other, moving only along the roads. Cities are numbered from $$$1$$$ to $$$n$$$. Yes, of course, you recognized an undirected tree in this description.

The government plans to repair all the roads. Each road will be repaired by some private company. In total, the country has $$$10^6$$$ private companies that are numbered from $$$1$$$ to $$$10^6$$$. It is possible that some companies will not repair roads at all, and some will repair many roads.

To simplify the control over the work of private companies, the following restriction was introduced: for each city, we calculate the number of different companies that repair roads that have this city at one end. This number for each city should not exceed $$$2$$$. In other words, for each city, there should be no more than two different companies that repair roads related to this city.

The National Anti-Corruption Committee of Treeland raises concerns that all (or almost all) of the work will go to one private company. For this reason, the committee requires that roads be distributed among companies in such a way as to minimize the value of $$$r$$$. For each company, we calculate the number of roads assigned to it, the maximum among all companies is called the number $$$r$$$.

Help the government find such a way to distribute all roads among companies in the required way.

Input format:
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of input cases in the input. Next, the input cases themselves are given. The first line of each test case set contains an integer $$$n$$$ ($$$2 \le n \le 3000$$$) — the number of cities in Treeland. Next, in the $$$n-1$$$ lines the roads are written: a line contains two integers $$$x_i$$$, $$$y_i$$$ ($$$1 \le x_i, y_i \le n$$$), indicating that the $$$i$$$-th road connects the cities $$$x_i$$$ and $$$y_i$$$.

It is guaranteed that the sum of all values $$$n$$$ ​​for all test cases in the input does not exceed $$$3000$$$.

Output format:
Print the answers for all $$$t$$$ test cases in the input. Each test case must begin with a line that contains $$$r$$$ — the minimum possible number of roads assigned to the most used company. Next, in the next line print $$$n-1$$$ the number $$$c_1, c_2, \dots, c_{n-1}$$$ ($$$1 \le c_i \le 10^6$$$), where $$$c_i$$$ indicates the company to repair the $$$i$$$-th road. If there are several optimal assignments, print any of them.

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Example input:
3
3
1 2
2 3
6
1 2
1 3
1 4
1 5
1 6
7
3 1
1 4
4 6
5 1
2 4
1 7

Example output:
1
10 20
3
1 1 1 2 2 
2
11 11 12 13 12 13

Editorial:
Let's do a binary by answer. For each candidate $$$c$$$ you should check if it is possible to color edges in such a way that:
• at most two colors are incident to any vertex,
• the usage of the most frequent color is at most $$$c$$$.
Let's use a dynamic programming approach to check it. Build a recursive function to check if it possible to color subtree in the required way it the edge to the parent is included in a subtree (if such edge exists). In case of positive answer also return the minimal number of usages of color $$$c'$$$ (where $$$c'$$$ is a color of the edge to the parent).
Formally, $$$d[u]$$$ is:
• $$$-1$$$ if it is impossible to color subtree rooted in $$$u$$$ in the required way, the edge to the parent of $$$u$$$ (if any) is included in the subtree;
• $$$f$$$ if such coloring exists and $$$f$$$ is the minimal number of usages of the color in which the edge to the parent of $$$u$$$ is colored (of $$$f$$$ is just any number in case of root, i.e. no parent).
Now to calculate $$$f[u]$$$ you need to solve an instance of a knapsack problem: find such subset of children which should be colored same a the parent edge that the sum $$$d[v_1] + d[v_2] + \dots + d[v_k] + 1$$$ is less or equal than $$$c$$$, where $$$x=v_1, v_2, \dots, v_k$$$ are chosen children vertices. You should erase $$$+1$$$ addend for the case $$$u$$$ is the root. Among all such cases, you need to find a subset of children with the greatest value $$$x$$$ less or equal than $$$c$$$. It is exactly a knapsack problem.
As the total degree of all vertices in a tree is $$$O(n)$$$, the total complexity of the algorithm is $$$O(n^2)$$$ for fixed $$$c$$$ and $$$O(n^2 \log n)$$$ for the whole solution (remember about a binary search).