CF2178F.Conquer or of Forest

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Define the unique ornamental coloring of a rooted tree∗^{\text{∗}} as the following vertex coloring:

  • A vertex vv is colored white if the number of vertices in the subtree†^{\text{†}} rooted at vv is even;
  • Otherwise, vv is colored black.

On his quest to conquer a forest of Christmas trees, Yuuki encountered an ornamentally colored tree TT with nn vertices labeled from 11 to nn, rooted at vertex 11.

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 vv such that all white vertices lie on the simple path from the root 11 to vv.

To conquer the tree, Yuuki can apply the following operation on TT an arbitrary number of times (possibly zero):

  • First, choose a vertex ww that is colored white and is not the root of TT. Let pwp_w be the parent of ww.
  • Then, remove the edge connecting pwp_w and ww, and add an edge between any two vertices such that TT remains a tree.
  • Finally, recolor the vertices of TT such that it is ornamentally colored. Note that TT is always rooted at vertex 11.

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 11 and 33.

Compute the number of distinct‡^{\text{‡}} conquered trees that Yuuki can construct by applying the above operation an arbitrary number of times on TT. Since the answer may be large, output it modulo 998 244 353998\,244\,353.

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.

∗^{\text{∗}}A tree is a connected graph without cycles.

†^{\text{†}}A subtree of vertex vv is the subgraph of vv, all its descendants, and all the edges between them.

‡^{\text{‡}}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.

定义一棵有根树∗^{\text{∗}}的唯一装饰性染色(unique ornamental coloring)为如下顶点染色方案:

  • 若以顶点 vv 为根的子树†^{\text{†}}中所含顶点数为偶数,则将 vv 染成白色;
  • 否则,将 vv 染成黑色。

在征服一片圣诞树林的征途中,悠木遇到了一棵具有装饰性染色的树 TT,该树共有 nn 个顶点,编号为 11 至 nn,且以顶点 11 为根。

悠木认为该树已被“征服”,当且仅当满足以下任一条件:

  • 树中不存在白色顶点;或
  • 存在某个顶点 vv,使得所有白色顶点均位于从根 11 到 vv 的简单路径上。

为征服该树,悠木可对 TT 任意次(包括零次)执行如下操作:

  • 首先,选择一个染成白色且非根的顶点 ww;记 pwp_w 为 ww 的父节点;
  • 然后,删去连接 pwp_w 与 ww 的边,并在任意两个顶点之间添加一条新边,使得 TT 仍为一棵树;
  • 最后,对 TT 的所有顶点重新进行装饰性染色(注意:树始终以顶点 11 为根)。

第一个测试用例中一次操作的可能示例。所得树已被征服,因为所有白色顶点均位于顶点 11 与 33 之间的路径上。

请计算:通过对 TT 执行上述操作任意次数(包括零次),悠木所能构造出的互不相同‡^{\text{‡}} 的已被征服的树的总数。由于答案可能很大,请输出其对 998 244 353998\,244\,353 取模的结果。

注意:悠木不能在一次操作中途停止(特别地,他必须在检查树是否已被征服前完成重染色)。此外,即使当前树已处于被征服状态,悠木仍可继续执行操作。

∗^{\text{∗}} 树是无环的连通图。
†^{\text{†}} 顶点 vv 的子树是指由 vv、其所有后代以及它们之间的所有边构成的子图。
‡^{\text{‡}} 当且仅当存在一对顶点,使得其中一棵树包含该两点间的边而另一棵树不包含时,这两棵树被视为互不相同。

输入格式

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

The first line of each test case contains a single integer nn (2≤n≤2⋅1052\le n\le 2\cdot 10^5) — the number of vertices in TT.

Then n−1n-1 lines follow, the ii-th line containing two integers uiu_i and viv_i (1≤ui<vi≤n1\le u_i \lt v_i\le n) — the two vertices that the ii-th edge connects.

It is guaranteed that the given edges form a tree.

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

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052\le n\le 2\cdot 10^5)——树 TT 中的顶点数量。

接下来是 n−1n-1 行,其中第 ii 行包含两个整数 uiu_i 和 viv_i(1≤ui<vi≤n1\le u_i \lt v_i\le n)——表示第 ii 条边所连接的两个顶点。

保证所给的边构成一棵树。

保证所有测试用例中 nn 的总和不超过 2⋅1052\cdot 10^5。

输出格式

For each test case, output a single integer — the number of distinct conquered trees that can be constructed from TT, modulo 998 244 353998\,244\,353.

对于每个测试用例,输出一个整数——即可以从 TT 构造出的不同被征服树(conquered trees)的数量,对 998 244 353998\,244\,353 取模。

输入输出样例

  • 输入#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 11 and 44.

  • The first and only operation selects ww to be vertex 33 (pwp_w is vertex 11) and draws an edge between vertices 22 and 44.
  • The resulting tree is conquered since all white vertices lie on the simple path between vertices 11 and 33.

  • The first and only operation selects ww to be vertex 33 (pwp_w is vertex 11) and draws an edge between vertices 22 and 33.
  • The resulting tree is conquered since all white vertices lie on the simple path between vertices 11 and 33.

  • The first operation selects ww to be vertex 33 (pwp_w is vertex 11) and draws an edge between vertices 22 and 44.
  • The second operation selects ww to be vertex 44 (pwp_w is vertex 22) and draws an edge between vertices 11 and 44.
  • The resulting tree is conquered since all white vertices lie on the simple path between vertices 11 and 44.

In the second test case, Yuuki cannot apply the operation on TT since there are no white vertices. Additionally, TT is already conquered since there are no white vertices. Thus, the answer is 11.

在第一个测试用例中,以下是可构造的四棵被征服的树,以及构造每棵树的一系列操作。

说明

图示

  • 操作应用了零次。
  • 给定的树本身已是被征服的,因为所有白色顶点均位于顶点 11 与顶点 44 之间的简单路径上。

  • 第一次(也是唯一一次)操作选择 ww 为顶点 33(其父节点 pwp_w 为顶点 11),并在顶点 22 与顶点 44 之间添加一条边。
  • 所得树是被征服的,因为所有白色顶点均位于顶点 11 与顶点 33 之间的简单路径上。

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

  • 第一次操作选择 ww 为顶点 33(其父节点 pwp_w 为顶点 11),并在顶点 22 与顶点 44 之间添加一条边。
  • 第二次操作选择 ww 为顶点 44(其父节点 pwp_w 为顶点 22),并在顶点 11 与顶点 44 之间添加一条边。
  • 所得树是被征服的,因为所有白色顶点均位于顶点 11 与顶点 44 之间的简单路径上。

在第二个测试用例中,由于树 TT 中不存在白色顶点,柚木无法对其执行该操作;此外,TT 本身已是被征服的(因其中无白色顶点)。因此,答案为 11。

输入解题思路,AI测评打分。不知道怎么写?

首页