题目收集
题目列表题单
返回列表

LP and Rifleman

CodeforcesP0052· 3000ms· 1024 MB· ZJU Summer 2026 Contest 5原题链接
博弈论树形 dp

创建于 2026/07/18 · 更新于 2026/07/18

题目描述

LP and Rifleman are playing a game.

Initially, there is an unrooted tree with nnn vertices.

Two players, LP and Rifleman, play alternately. LP moves first.

1. LP chooses a vertex whose degree is not equal to 2 and removes it, or she may do nothing.

2. Then Rifleman chooses two vertices that are not connected (i.e., belong to different connected components) and adds an edge between them.

If after a non-zero number of operations , the graph becomes connected again, the game ends immediately.

LP wants to minimize the number of remaining vertices, Rifleman wants to maximize it. Determine the final number of vertices when both play optimally.

输入格式

The first line contains the number of testcases T(1≤T≤105)T(1\leq T\leq 10^5)T(1≤T≤105) .

For each testcase, the first line contains the number of vertices n(1≤∑n≤106)n(1\leq \sum n\leq 10^6)n(1≤∑n≤106).

Each for the next n−1n-1n−1 lines contains two integers uuu and vvv ,meaning that there is an edge between vertex uuu and vvv.

输出格式

For each testcase,you need to print the final number of vertices in a single line.

样例输入 #1

2
4
1 2
1 3
1 4
4
1 2
2 3
3 4

样例输出 #1

2
3

样例输入 #2

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

样例输出 #2

3
4