CF BUDDY
← Problems·

1830A · Copil Copac Draws Trees

1400 · dfs and similar, dp, graphs

Problem: Copil Copac is given a list of n1n-1 edges describing a tree of nn vertices. He decides to draw it using the following algorithm:

  • Step 00: Draws the first vertex (vertex 11). Go to step 11.
  • Step 11: For every edge in the input, in order: if the edge connects an already drawn vertex uu to an undrawn vertex vv, he will draw the undrawn vertex vv and the edge. After checking every edge, go to step 22.
  • Step 22: If all the vertices are drawn, terminate the algorithm. Else, go to step 11.

The number of readings is defined as the number of times Copil Copac performs step 11.

Find the number of readings needed by Copil Copac to draw the tree.

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

The first line of each test case contains a single integer nn (2n21052 \le n \le 2 \cdot 10^5) — the number of vertices of the tree.

The following n1n - 1 lines of each test case contain two integers uiu_i and viv_i (1ui,vin1 \le u_i, v_i \le n, uiviu_i \neq v_i) — indicating that (ui,vi)(u_i,v_i) is the ii-th edge in the list. It is guaranteed that the given edges form a tree.

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output Format: For each test case, output the number of readings Copil Copac needs to draw the tree.

Note: In the first test case:

After the first reading, the tree will look like this:

After the second reading:

Therefore, Copil Copac needs 22 readings to draw the tree.

Sample Cases

Case 1

Input

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

Output

2
3

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