306539: CF1211H. Road Repair in Treeland

Memory Limit:256 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Road Repair in Treeland

题意翻译

给你一棵有 $n$ 个节点 $n-1$ 条边的树。你需要给每一条边染上一种颜色(从 $1\sim10^6$ 编号)。 使得对每个点满足,与它直接相连的所有边中,最多只有两种不同的颜色。 最小化被使用次数最多的颜色的使用次数,输出这个数和任意一种方案。

题目描述

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.

输入输出格式

输入格式


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 $ .

输出格式


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.

输入输出样例

输入样例 #1

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

输出样例 #1

1
10 20
3
1 1 1 2 2 
2
11 11 12 13 12 13

Input

题意翻译

给你一棵有 $n$ 个节点 $n-1$ 条边的树。你需要给每一条边染上一种颜色(从 $1\sim10^6$ 编号)。 使得对每个点满足,与它直接相连的所有边中,最多只有两种不同的颜色。 最小化被使用次数最多的颜色的使用次数,输出这个数和任意一种方案。

加入题单

上一题 下一题 算法标签: