CF BUDDY
← Problems·

1627C · Not Assigning

1400 · constructive algorithms, dfs and similar, number theory

Problem: You are given a tree of nn vertices numbered from 11 to nn, with edges numbered from 11 to n1n-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: 2132 \to 1 \to 3 has a weight of 11+2=1311 + 2 = 13, which is prime. Similarly, the path of one edge: 434 \to 3 has a weight of 55, 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-1. It can be proven that if a valid assignment exists, one exists with weights between 11 and 10510^5 as well.

Input Format: The input consists of multiple test cases. The first line contains an integer tt (1t1041 \leq t \leq 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains one integer nn (2n1052 \leq n \leq 10^5) — the number of vertices in the tree.

Then, n1n-1 lines follow. The ii-th line contains two integers uu and vv (1u,vn1 \leq u, v \leq n) denoting that edge number ii is between vertices uu and vv. It is guaranteed that the edges form a tree.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

Output Format: For each test case, if a valid assignment exists, then print a single line containing n1n-1 integers a1,a2,,an1a_1, a_2, \dots, a_{n-1} (1ai1051 \leq a_i \le 10^5), where aia_i denotes the weight assigned to the edge numbered ii. Otherwise, print 1-1.

If there are multiple solutions, you may print any.

Note: For the first test case, there are only two paths having one edge each: 121 \to 2 and 212 \to 1, both having a weight of 1717, 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.

Sample Cases

Case 1

Input

3
2
1 2
4
1 3
4 3
2 1
7
1 2
1 3
3 4
3 5
6 2
7 2

Output

17
2 5 11
-1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback