CF2178F.Conquer or of Forest
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Define the unique ornamental coloring of a rooted tree∗ as the following vertex coloring:
- A vertex v is colored white if the number of vertices in the subtree† rooted at v is even;
- Otherwise, v is colored black.
On his quest to conquer a forest of Christmas trees, Yuuki encountered an ornamentally colored tree T with n vertices labeled from 1 to n, rooted at vertex 1.
Yuuki considers the tree conquered if and only if at least one of the following conditions holds:
- There are no white vertices in the tree, or
- There exists some vertex v such that all white vertices lie on the simple path from the root 1 to v.
To conquer the tree, Yuuki can apply the following operation on T an arbitrary number of times (possibly zero):
- First, choose a vertex w that is colored white and is not the root of T. Let pw be the parent of w.
- Then, remove the edge connecting pw and w, and add an edge between any two vertices such that T remains a tree.
- Finally, recolor the vertices of T such that it is ornamentally colored. Note that T is always rooted at vertex 1.
A possible application of the operation in the first test case. The resulting tree is conquered since all white vertices lie on the path between vertices 1 and 3.
Compute the number of distinct‡ conquered trees that Yuuki can construct by applying the above operation an arbitrary number of times on T. Since the answer may be large, output it modulo 998244353.
Note that Yuuki cannot stop midway through an operation (in particular, he must recolor the tree before checking if it is conquered). Additionally, Yuuki is allowed to apply the operation even if the tree is already conquered.
∗A tree is a connected graph without cycles.
†A subtree of vertex v is the subgraph of v, all its descendants, and all the edges between them.
‡Two trees are considered distinct if and only if there exists a pair of vertices such that there is an edge between them in one of the trees, and not in the other.
定义一棵有根树∗的唯一装饰性染色(unique ornamental coloring)为如下顶点染色方案:
- 若以顶点 v 为根的子树†中所含顶点数为偶数,则将 v 染成白色;
- 否则,将 v 染成黑色。
在征服一片圣诞树林的征途中,悠木遇到了一棵具有装饰性染色的树 T,该树共有 n 个顶点,编号为 1 至 n,且以顶点 1 为根。
悠木认为该树已被“征服”,当且仅当满足以下任一条件:
- 树中不存在白色顶点;或
- 存在某个顶点 v,使得所有白色顶点均位于从根 1 到 v 的简单路径上。
为征服该树,悠木可对 T 任意次(包括零次)执行如下操作:
- 首先,选择一个染成白色且非根的顶点 w;记 pw 为 w 的父节点;
- 然后,删去连接 pw 与 w 的边,并在任意两个顶点之间添加一条新边,使得 T 仍为一棵树;
- 最后,对 T 的所有顶点重新进行装饰性染色(注意:树始终以顶点 1 为根)。
第一个测试用例中一次操作的可能示例。所得树已被征服,因为所有白色顶点均位于顶点 1 与 3 之间的路径上。
请计算:通过对 T 执行上述操作任意次数(包括零次),悠木所能构造出的互不相同‡ 的已被征服的树的总数。由于答案可能很大,请输出其对 998244353 取模的结果。
注意:悠木不能在一次操作中途停止(特别地,他必须在检查树是否已被征服前完成重染色)。此外,即使当前树已处于被征服状态,悠木仍可继续执行操作。
∗ 树是无环的连通图。
† 顶点 v 的子树是指由 v、其所有后代以及它们之间的所有边构成的子图。
‡ 当且仅当存在一对顶点,使得其中一棵树包含该两点间的边而另一棵树不包含时,这两棵树被视为互不相同。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains a single integer n (2≤n≤2⋅105) — the number of vertices in T.
Then n−1 lines follow, the i-th line containing two integers ui and vi (1≤ui<vi≤n) — the two vertices that the i-th edge connects.
It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(2≤n≤2⋅105)——树 T 中的顶点数量。
接下来是 n−1 行,其中第 i 行包含两个整数 ui 和 vi(1≤ui<vi≤n)——表示第 i 条边所连接的两个顶点。
保证所给的边构成一棵树。
保证所有测试用例中 n 的总和不超过 2⋅105。
输出格式
For each test case, output a single integer — the number of distinct conquered trees that can be constructed from T, modulo 998244353.
对于每个测试用例,输出一个整数——即可以从 T 构造出的不同被征服树(conquered trees)的数量,对 998244353 取模。
输入输出样例
输入#1
5 4 1 2 1 3 3 4 5 1 2 1 3 1 4 1 5 5 1 2 2 3 1 4 4 5 6 1 2 2 3 2 4 2 6 5 6 11 2 10 6 8 1 6 3 7 5 11 5 8 5 9 4 7 6 7 2 6
输出#1
4 1 16 8 2048
说明/提示
In the first test case, below are the four conquered trees that can be constructed and a sequence of operations that constructs each one.
Explanation
Illustration
- The operation is applied zero times.
- The given tree is already conquered since all white vertices lie on the simple path between vertices 1 and 4.

- The first and only operation selects w to be vertex 3 (pw is vertex 1) and draws an edge between vertices 2 and 4.
- The resulting tree is conquered since all white vertices lie on the simple path between vertices 1 and 3.

- The first and only operation selects w to be vertex 3 (pw is vertex 1) and draws an edge between vertices 2 and 3.
- The resulting tree is conquered since all white vertices lie on the simple path between vertices 1 and 3.

- The first operation selects w to be vertex 3 (pw is vertex 1) and draws an edge between vertices 2 and 4.
- The second operation selects w to be vertex 4 (pw is vertex 2) and draws an edge between vertices 1 and 4.
- The resulting tree is conquered since all white vertices lie on the simple path between vertices 1 and 4.

In the second test case, Yuuki cannot apply the operation on T since there are no white vertices. Additionally, T is already conquered since there are no white vertices. Thus, the answer is 1.
在第一个测试用例中,以下是可构造的四棵被征服的树,以及构造每棵树的一系列操作。
说明
图示
- 操作应用了零次。
- 给定的树本身已是被征服的,因为所有白色顶点均位于顶点 1 与顶点 4 之间的简单路径上。

- 第一次(也是唯一一次)操作选择 w 为顶点 3(其父节点 pw 为顶点 1),并在顶点 2 与顶点 4 之间添加一条边。
- 所得树是被征服的,因为所有白色顶点均位于顶点 1 与顶点 3 之间的简单路径上。

- 第一次(也是唯一一次)操作选择 w 为顶点 3(其父节点 pw 为顶点 1),并在顶点 2 与顶点 3 之间添加一条边。
- 所得树是被征服的,因为所有白色顶点均位于顶点 1 与顶点 3 之间的简单路径上。

- 第一次操作选择 w 为顶点 3(其父节点 pw 为顶点 1),并在顶点 2 与顶点 4 之间添加一条边。
- 第二次操作选择 w 为顶点 4(其父节点 pw 为顶点 2),并在顶点 1 与顶点 4 之间添加一条边。
- 所得树是被征服的,因为所有白色顶点均位于顶点 1 与顶点 4 之间的简单路径上。

在第二个测试用例中,由于树 T 中不存在白色顶点,柚木无法对其执行该操作;此外,T 本身已是被征服的(因其中无白色顶点)。因此,答案为 1。
输入解题思路,AI测评打分。不知道怎么写?