Spectral::Cup 2026 Round 3 (Codeforces Round 1110, Div. 1 + Div. 2)
E
汤姆和杰瑞
Tom and Jerry
Codeforces
2300
原题
← D2F →

汤姆和杰瑞AI (CI翻译)

You are given an undirected tree consisting of $n$ vertices. A simple path $p$ of length $k$ is defined as a sequence of distinct vertices $p_0,p_1,\ldots,p_k$ such that there exists an undirected edge between vertices $p_i$ and $p_{i+1}$ for every $0 \le i \lt k$. A simple path is uniquely identified by the unordered pair of its endpoints, $(p_0,p_k)$. That is, $(u,v)$ and $(v,u)$ represent the same simple path.

给定一棵由 $n$ 个顶点组成的无向树。长度为 $k$ 的简单路径 $p$ 被定义为一个由互不相同的顶点 $p_0,p_1,\ldots,p_k$ 组成的序列,并且对于每个 $0 \le i \lt k$,顶点 $p_i$ 与 $p_{i+1}$ 之间存在一条无向边。一个简单路径可以唯一地由其两个端点的无序对 $(p_0,p_k)$ 确定。也就是说,$(u,v)$ 和 $(v,u)$ 表示同一条简单路径。AI (CI翻译)

Tom and Jerry are playing a game on this tree. The players take turns, with Tom going first. In the $i$-th turn ($i \ge 1$), the current player chooses a simple path $(x_i,y_i)$ that satisfies the following conditions:

汤姆和杰瑞在这棵树上进行一场游戏。双方轮流行动,汤姆先手。在第 $i$ 回合($i \ge 1$)中,当前玩家选择一条满足以下条件的简单路径 $(x_i,y_i)$:AI (CI翻译)

  • $x_i \ne y_i$.
  • The path does not share any edges with any of the previously chosen paths.
  • Either $x_i$ or $y_i$ belongs to the path chosen in the $(i-1)$-th turn. Note that this condition only applies when $i \ge 2$.
  • $x_i \ne y_i$。
  • 该路径与之前选择的任意路径都不共享边。
  • $x_i$ 或 $y_i$ 至少有一个属于第 $(i-1)$ 回合中选择的路径。注意,此条件仅在 $i \ge 2$ 时适用。
AI (CI翻译)

The player who is unable to choose a valid path on their turn loses the game.

在自己的回合无法选择有效路径的玩家输掉游戏。AI (CI翻译)

Assuming both Tom and Jerry play optimally, your task is to compute the total number of distinct simple paths Tom can choose in the first turn such that he can guarantee a win.

假设汤姆和杰瑞都采取最优策略,请计算汤姆在第一回合可以选择、且能够保证获胜的不同简单路径的总数。AI (CI翻译)

Input

输入格式

AI (CI翻译)

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

每个测试包含多个测试用例。第一行包含测试用例数量 $t$($1 \le t \le 10^4$)。接下来是各测试用例的描述。AI (CI翻译)

The first line of each test case contains an integer $n$ ($2 \le n \le 2 \cdot 10^5$), representing the number of vertices in the tree.

每个测试用例的第一行包含一个整数 $n$($2 \le n \le 2 \cdot 10^5$),表示树中的顶点数。AI (CI翻译)

Each of the next $n-1$ lines contains two integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$), representing an undirected edge between vertices $u$ and $v$. It is guaranteed that the edges form a valid tree.

接下来的 $n-1$ 行中,每行包含两个整数 $u$ 和 $v$($1 \le u,v \le n$,$u \ne v$),表示顶点 $u$ 和顶点 $v$ 之间的一条无向边。保证这些边构成一棵合法的树。AI (CI翻译)

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

保证所有测试用例的 $n$ 之和不超过 $2 \cdot 10^5$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, output an integer representing the total number of distinct simple paths Tom can choose in the first turn such that he can guarantee a win.

对于每个测试用例,输出一个整数,表示汤姆在第一回合可以选择、且能够保证获胜的不同简单路径的总数。AI (CI翻译)

Note

说明

AI (CI翻译)

In the first test case, the tree consists of only two vertices and a single edge between vertex $1$ and vertex $2$. Tom can choose the simple path $(1,2)$ on his first turn. Since this path consumes the only edge in the tree, Jerry will have no unused edges available to form a valid path on his turn. Thus, Jerry is unable to make a move, and Tom wins the game. There is exactly $1$ winning path for Tom.

在第一个测试用例中,树仅由两个顶点和连接顶点 $1$ 与顶点 $2$ 的一条边组成。汤姆可以在第一回合选择简单路径 $(1,2)$。由于这条路径消耗了树中的唯一一条边,杰瑞在自己的回合中没有未使用的边可以组成有效路径。因此,杰瑞无法行动,汤姆获胜。汤姆恰好有 $1$ 条获胜路径。AI (CI翻译)

In the third test case, the tree is a star graph with vertex $2$ at the center, connected to vertices $1, 3, 4,$ and $5$. There are $10$ possible simple paths in total.

在第三个测试用例中,树是一棵星形图,顶点 $2$ 位于中心,并与顶点 $1$、$3$、$4$ 和 $5$ 相连。总共有 $10$ 条可能的简单路径。AI (CI翻译)

One of the winning paths is $(1,3)$:

其中一条获胜路径是 $(1,3)$:AI (CI翻译)

  • On his first turn, Tom chooses $(1,3)$. This path uses edges $(1,2)$ and $(2,3)$, and its vertices are $\{1, 2, 3\}$. The unused edges remaining in the tree are $(2,4)$ and $(2,5)$.
  • On Jerry's turn, he must pick an edge-disjoint path with at least one endpoint in Tom's path $\{1, 2, 3\}$. The only valid paths Jerry can choose are $(2,4)$ or $(2,5)$. Jerry cannot choose the path $(4,5)$ because its endpoints $4$ and $5$ do not belong to Tom's path $\{1, 2, 3\}$.
  • Suppose Jerry chooses $(2,4)$. His path vertices are now $\{2, 4\}$.
  • On Tom's next turn, he must pick a path with an endpoint in $\{2, 4\}$. He can simply choose the last remaining path $(2,5)$.
  • After this, all edges are exhausted, Jerry has no moves left, and Tom wins.
  • 在第一回合,汤姆选择 $(1,3)$。这条路径使用了边 $(1,2)$ 和 $(2,3)$,其顶点集合为 $\{1, 2, 3\}$。树中剩余的未使用边为 $(2,4)$ 和 $(2,5)$。
  • 在杰瑞的回合,他必须选择一条与已使用边不相交、且至少有一个端点属于汤姆路径 $\{1, 2, 3\}$ 的路径。杰瑞唯一可以选择的有效路径是 $(2,4)$ 或 $(2,5)$。杰瑞不能选择路径 $(4,5)$,因为其端点 $4$ 和 $5$ 都不属于汤姆的路径 $\{1, 2, 3\}$。
  • 假设杰瑞选择 $(2,4)$。此时他的路径顶点为 $\{2, 4\}$。
  • 在汤姆的下一回合,他必须选择一条端点属于 $\{2, 4\}$ 的路径。他可以直接选择最后剩下的路径 $(2,5)$。
  • 此后所有边都已被使用,杰瑞无法继续行动,汤姆获胜。
AI (CI翻译)

One of the losing paths is $(1,2)$:

其中一条失败路径是 $(1,2)$:AI (CI翻译)

  • Suppose Tom starts by choosing $(1,2)$. The vertices on his path are $\{1, 2\}$, and the remaining edges are $(2,3), (2,4),$ and $(2,5)$.
  • Jerry can respond by choosing the path $(2,3)$. This is valid because the endpoint $2$ is on Tom's previous path. The vertices of Jerry's path are $\{2, 3\}$.
  • Tom is now forced to choose an unused path with an endpoint in $\{2, 3\}$. He must choose either $(2,4)$ or $(2,5)$. Let's say he chooses $(2,4)$.
  • Jerry will then easily choose the final remaining path $(2,5)$, as its endpoint $2$ belongs to Tom's previous path $\{2, 4\}$.
  • All edges are now used. Tom cannot make a move on his turn and loses the game.
  • 假设汤姆首先选择 $(1,2)$。汤姆路径上的顶点为 $\{1, 2\}$,剩余的边为 $(2,3)$、$(2,4)$ 和 $(2,5)$。
  • 杰瑞可以选择路径 $(2,3)$ 作为回应。这是有效的,因为端点 $2$ 位于汤姆上一回合的路径上。杰瑞路径上的顶点为 $\{2, 3\}$。
  • 现在汤姆必须选择一条端点属于 $\{2, 3\}$ 的未使用路径。他必须选择 $(2,4)$ 或 $(2,5)$。假设他选择 $(2,4)$。
  • 随后杰瑞可以轻松选择最后剩下的路径 $(2,5)$,因为其端点 $2$ 属于汤姆上一回合的路径 $\{2, 4\}$。
  • 此时所有边都已被使用。汤姆在自己的回合无法行动,输掉游戏。
AI (CI翻译)

样例

Input #1
5
2
1 2
3
1 2
2 3
5
1 2
2 3
2 4
2 5
5
1 2
2 3
3 4
4 5
7
1 2
2 3
2 4
4 5
5 6
5 7
Output #1
1
1
6
1
5
InputCopy OutputCopy
题目信息
题号
2245E
时间限制
2 s
内存限制
256 MB
标签
DFS 及类似
并查集
博弈论
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。