CF1627C.Not Assigning
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a tree of n vertices numbered from 1 to n , with edges numbered from 1 to n−1 . A tree is a connected undirected graph without cycles. You have to assign integer weights to each edge of the tree, such that the resultant graph is a prime tree.
A prime tree is a tree where the weight of every path consisting of one or two edges is prime. A path should not visit any vertex twice. The weight of a path is the sum of edge weights on that path.
Consider the graph below. It is a prime tree as the weight of every path of two or less edges is prime. For example, the following path of two edges: 2→1→3 has a weight of 11+2=13 , which is prime. Similarly, the path of one edge: 4→3 has a weight of 5 , which is also prime.
Print any valid assignment of weights such that the resultant tree is a prime tree. If there is no such assignment, then print −1 . It can be proven that if a valid assignment exists, one exists with weights between 1 and 105 as well.
输入格式
The input consists of multiple test cases. The first line contains an integer t ( 1≤t≤104 ) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer n ( 2≤n≤105 ) — the number of vertices in the tree.
Then, n−1 lines follow. The i -th line contains two integers u and v ( 1≤u,v≤n ) denoting that edge number i is between vertices u and v . It is guaranteed that the edges form a tree.
It is guaranteed that the sum of n over all test cases does not exceed 105 .
输出格式
For each test case, if a valid assignment exists, then print a single line containing n−1 integers a1,a2,…,an−1 ( 1≤ai≤105 ), where ai denotes the weight assigned to the edge numbered i . Otherwise, print −1 .
If there are multiple solutions, you may print any.
输入输出样例
输入#1
3 2 1 2 4 1 3 4 3 2 1 7 1 2 1 3 3 4 3 5 6 2 7 2
输出#1
17 2 5 11 -1
说明/提示
For the first test case, there are only two paths having one edge each: 1→2 and 2→1 , both having a weight of 17 , which is prime.
The second test case is described in the statement.
It can be proven that no such assignment exists for the third test case.