DeepInfra Round 1(Codeforces Round 1126,Div. 1 + Div. 2)
H2
个人问题(困难版)
Personal Issues (Hard Version)
Codeforces
3300
原题
← H1→

个人问题(困难版)AI (CI翻译)

This is the hard version of the problem. The difference between the versions is that in this version, $t \le 10^4$, $n \le 2 \cdot 10^5$ and the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. You can hack only if you solved all versions of this problem.

这是本题的困难版本。与其他版本的区别在于,本版本中 $t \le 10^4$、$n \le 2 \cdot 10^5$,且所有测试用例的 $n$ 之和不超过 $2 \cdot 10^5$。只有解决了本题的所有版本,才能进行 hack。AI (CI翻译)

The rivals Axial and Nife have had yet another argument, and they decided to settle it by playing a game on a tree.

对手 Axial 和 Nife 又发生了争吵,于是他们决定在一棵树上进行游戏来解决争端。AI (CI翻译)

They are given a tree with $n$ vertices numbered from $1$ to $n$. Each vertex contains either $0$ or $1$. Initially, two distinct vertices $x$ and $y$ contain $0$, and all other vertices contain $1$.

给定一棵有 $n$ 个顶点的树,顶点编号为 $1$ 到 $n$。每个顶点上的值为 $0$ 或 $1$。初始时,两个不同的顶点 $x$ 和 $y$ 上的值为 $0$,其他所有顶点上的值均为 $1$。AI (CI翻译)

The players take turns, with Axial going first. On each turn, the current player chooses a vertex $v$ containing $1$ and changes its value to $0$. Before changing the value of $v$, let $c_v$ be the number of neighbors of $v$ that contain $0$. If $c_v = 1$, the current player gets one point. Otherwise, they get no points. The game ends when all vertices contain $0$.

两名玩家轮流行动,由 Axial 先手。每回合,当前玩家选择一个值为 $1$ 的顶点 $v$,并将其值改为 $0$。在修改 $v$ 的值之前,令 $c_v$ 表示 $v$ 的邻居中值为 $0$ 的顶点数量。如果 $c_v = 1$,当前玩家得一分;否则不得分。所有顶点的值都变为 $0$ 时,游戏结束。AI (CI翻译)

Let $p$ and $q$ be the final scores of Axial and Nife, respectively. Axial wants to maximize $p - q$, while Nife wants to minimize it. Assuming both players play optimally, determine the value of $p - q$ at the end of the game.

设 Axial 和 Nife 最终的得分分别为 $p$ 和 $q$。Axial 希望最大化 $p - q$,而 Nife 希望最小化它。假设双方都采取最优策略,求游戏结束时 $p - q$ 的值。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 three integers $n$, $x$, and $y$ ($2 \le n \le 2 \cdot 10^5$, $1 \le x, y \le n$, $x \ne y$) — the number of vertices in the tree and the two vertices that initially contain $0$.

每个测试用例的第一行包含三个整数 $n$、$x$ 和 $y$($2 \le n \le 2 \cdot 10^5$,$1 \le x, y \le n$,$x \ne y$)——树的顶点数,以及初始值为 $0$ 的两个顶点。AI (CI翻译)

Each of the next $n - 1$ lines contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \ne v$), denoting an edge between vertices $u$ and $v$.

接下来的 $n - 1$ 行,每行包含两个整数 $u$ 和 $v$($1 \le u, v \le n$,$u \ne v$),表示顶点 $u$ 和 $v$ 之间有一条边。AI (CI翻译)

It is guaranteed that the given graph is a tree.

保证给定的图是一棵树。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 a single integer — the value of $p - q$ at the end of the game.

对于每个测试用例,输出一个整数——游戏结束时 $p - q$ 的值。AI (CI翻译)

Note

说明

AI (CI翻译)

In the figures below, blue vertices initially contain $0$, while white vertices initially contain $1$.

在下方的图中,蓝色顶点初始值为 $0$,白色顶点初始值为 $1$。AI (CI翻译)

For the first example, the initial state is:

对于第一个样例,初始状态如下:AI (CI翻译)

image

imageAI (CI翻译)

Axial changes vertex $3$ to $0$. It has exactly one neighbor containing $0$, so Axial gets one point. Therefore, $p - q=1$.

Axial 将顶点 $3$ 的值改为 $0$。它恰好有一个邻居的值为 $0$,因此 Axial 得一分。所以,$p - q=1$。AI (CI翻译)

For the second example, the initial state is:

对于第二个样例,初始状态如下:AI (CI翻译)

image

imageAI (CI翻译)

Axial can change either vertex $2$ or vertex $3$ to $0$ and get one point. After that, the remaining vertex has two neighbors containing $0$, so Nife gets no points. Therefore, $p - q=1$.

Axial 可以将顶点 $2$ 或顶点 $3$ 的值改为 $0$,并得一分。之后,剩下的那个顶点有两个邻居的值为 $0$,因此 Nife 不得分。所以,$p - q=1$。AI (CI翻译)

For the third example, the initial state is:

对于第三个样例,初始状态如下:AI (CI翻译)

image

imageAI (CI翻译)

The only possible move is to change vertex $2$ to $0$. It has two neighbors containing $0$, so Axial gets no points. Therefore, $p - q=0$.

唯一可能的操作是将顶点 $2$ 的值改为 $0$。它有两个邻居的值为 $0$,因此 Axial 不得分。所以,$p - q=0$。AI (CI翻译)

样例

Input #1
4
3 1 2
1 2
2 3
4 1 4
1 2
2 3
3 4
3 1 3
1 2
2 3
6 2 3
1 2
1 3
1 4
1 5
1 6
Output #1
1
1
0
0
InputCopy OutputCopy
题目信息
题号
2271H2
时间限制
2 s
内存限制
256 MB
标签
博弈论
树

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