QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 512 MB 總分: 100

#3266. Destroy the "Tree Diagram

统计

Ever since the "God-blade" helped the Earthworm Kingdom increase its population by tens of millions, the Earthworm Kingdom has become increasingly prosperous! Recently, they discovered some magical papers underground, and after careful study, they turned out to be the design blueprints for a supercomputer from City X, Country D!

This computer is called a "treediagram," consisting of $n$ computing nodes and $n-1$ bidirectional communication cables. All computing nodes are numbered with positive integers not exceeding $n$. As the name suggests, this forms a tree structure.

The King of the Earthworm Kingdom has mastered the complete information of this tree from the blueprints, including the value of $n$ and the connection information of the $n-1$ cables. The King decided to send the two most powerful hackers of the Earthworm Kingdom, Little P and Little H, to invade the "treediagram" and destroy it as much as possible.

Little P and Little H are proficient in the world's best programming languages. After some discussion, they decided to take the following steps in order:

  • Little P chooses a computing node as his starting point for the invasion and adds a P-mark to that node.
  • Repeat the following operation several times (possibly 0 times):
    • Starting from his current computing node, Little P chooses a cable that has not been marked yet, invades the computing node at the other end of the cable, and adds a P-mark to both the traversed cable and the destination computing node.
  • Little H chooses a computing node as her starting point for the invasion and adds an H-mark to that node.
  • Repeat the following operation several times (possibly 0 times):
    • Starting from her current computing node, Little H chooses a cable that has not been marked yet, invades the computing node at the other end of the cable, and adds an H-mark to both the traversed cable and the destination computing node. (Note: Little H cannot pass through cables with a P-mark, but she can pass through computing nodes with a P-mark.)
  • Delete all marked computing nodes and cables.
  • For each remaining cable, if one or both of its endpoints were deleted in the previous step, delete this cable as well.

After the above operations, the "treediagram" will be disconnected, leaving several (possibly 0) connected components. To achieve the goal of destruction, the King of the Earthworm Kingdom hopes that the number of connected components will be as large as possible. He has come to you, hoping you can help him calculate this maximum number.

Little P and Little H are very anxious. Before you calculate the plan, they might have already calculated the optimal solution or part of it. You are given a value $x$:

  • If $x = 0$, it means Little P and Little H have not calculated the optimal solution, and you need to determine their invasion routes.
  • If $x = 1$, it means Little P has already calculated his invasion route in a certain optimal cooperative plan. He will choose a starting point $p_0$ and invade along the cables to the target point $p_1$, and he will not invade further along any cables; you only need to determine Little H's invasion route.
  • If $x = 2$, it means Little P and Little H have already calculated an optimal cooperative plan. Little P invaded from point $p_0$ to $p_1$ and stopped, and Little H invaded from point $h_0$ to $h_1$ and stopped. At this point, you do not need to direct their invasion; you only need to calculate the number of remaining connected components after the final two steps of deleting computing nodes and cables.

Figure 1. Example of a tree structure for the treediagram.

Input

Read data from the file treediagram.in.

Each input file contains multiple test cases. The first line of the input file contains two integers $T$ and $x$, where $T$ represents the number of test cases in the file, and the meaning of $x$ is as described above. (The value of $x$ is the same for all test cases in the same input file.)

The following lines contain the data for each test case. The first line of each test case contains several integers: If $x = 0$, the line contains only one integer $n$. If $x = 1$, the line contains three integers $n, p_0, p_1$. * If $x = 2$, the line contains five integers $n, p_0, p_1, h_0, h_1$. It is guaranteed that $p_0, p_1, h_0, h_1$ are all positive integers not exceeding $n$.

Each test case is followed by $n-1$ lines, each containing two positive integers not exceeding $n$, representing a cable connecting these two computing nodes. It is guaranteed that the input forms a tree.

Adjacent integers on the same line are separated by exactly one space.

Output

Output to the file treediagram.out.

For each test case, output one line representing the maximum number of remaining connected components under the given conditions.

Examples

Input 1

1 0
13
1 2
2 3
2 4
4 5
4 6
4 7
7 8
7 9
9 10
10 11
10 12
12 13

Output 1

8

Note 1

This input file contains only one test case. One optimal solution is as follows: Little P starts the invasion from node 2, and node 2 is marked by Little P. Little P invades from node 2 to node 4; node 4 and the traversed cable are marked by Little P. Little P invades from node 4 to node 7; node 7 and the traversed cable are marked by Little P. Little H starts the invasion from node 10, and node 10 is marked by Little H. Delete the marked nodes 2, 4, 7, 10 and the marked cables (2, 4) and (4, 7). Delete any cables that have an endpoint deleted in the previous step.

At this point, 8 connected components remain. Among them, nodes 1, 3, 5, 6, 8, 9, 11 each form a connected component, and nodes 12, 13 form a connected component.

Subtasks

For an integer $k$, let $\sum n^k$ be the sum of $n^k$ for the $T$ test cases in an input file.

All input files satisfy $T \le 10^5$ and $\sum n^1 \le 5 \times 10^5$.

The detailed data range for each test point is shown in the table below.

If "Complete Binary" in the table is "Yes", then every test case in the input file satisfies: the two numbers input in the $j$-th line ($1 \le j < n$) of the cable information are $\lfloor \frac{j+1}{2} \rfloor$ and $j+1$ respectively.

Test Point $x$ $n$ $\sum n^k$ Complete Binary $T$
1 $n \le 1$ $\sum n^0 \le 10^2$ No $\le 10^2$
2 $n \le 2$
3 $n \le 3$
4 $n \le 4$
5 $n \le 5$ $\sum n^0 \le 10^3$ $\le 10^3$
6 $n \le 6$
7 $n \le 7$ $\sum n^0 \le 10^4$ $\le 10^4$
8 = 2 $\sum n^3 \le 10^7$ Yes
9 = 1
10 = 0
11 = 2 $\sum n^3 \le 10^7$ No
12 = 1
13 = 0
14 = 2 $\sum n^2 \le 10^7$ Yes
15 = 1
16 = 0
17 = 2 $\sum n^2 \le 10^7$ No
18 = 1
19 = 0
20 = 2 $\sum n^1 \le 5 \times 10^5$ Yes
21 = 1
22 = 0
23 = 2 $\sum n^1 \le 5 \times 10^5$ No
24 = 1
25 = 0

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.