CF1929D.Sasha and a Walk in the City
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Sasha wants to take a walk with his girlfriend in the city. The city consists of n intersections, numbered from 1 to n. Some of them are connected by roads, and from any intersection, there is exactly one simple path† to any other intersection. In other words, the intersections and the roads between them form a tree.
Some of the intersections are considered dangerous. Since it is unsafe to walk alone in the city, Sasha does not want to visit three or more dangerous intersections during the walk.
Sasha calls a set of intersections good if the following condition is satisfied:
- If in the city only the intersections contained in this set are dangerous, then any simple path in the city contains no more than two dangerous intersections.
However, Sasha does not know which intersections are dangerous, so he is interested in the number of different good sets of intersections in the city. Since this number can be very large, output it modulo 998244353.
†A simple path is a path that passes through each intersection at most once.
萨沙想和女友在城市中散步。该城市由 n 个路口组成,编号为 1 到 n。其中一些路口由道路连接,且任意两个路口之间都存在唯一一条简单路径†。换言之,这些路口及其之间的道路构成一棵树。
部分路口被视为危险路口。由于独自在城市中行走不安全,萨沙不希望在散步途中经过三个或更多危险路口。
萨沙将一个路口集合称为好集合,当且仅当满足以下条件:
- 若在整个城市中,仅该集合中的路口是危险的,则城市中任意一条简单路径上至多包含两个危险路口。
然而,萨沙并不知道哪些路口实际是危险的,因此他关心的是:该城市中不同的好集合的总数。由于该数目可能极大,请输出其对 998244353 取模的结果。
† 简单路径指每条路径上每个路口至多经过一次。
输入格式
Each test consists of multiple test cases. The first line contains a single integer t (1≤t≤104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer n (2≤n≤3⋅105) — the number of intersections in the city.
The next (n−1) lines describe the roads. The i-th line contains two integers ui and vi (1≤ui,vi≤n, ui=vi) — the numbers of the intersections connected by the i-th road.
It is guaranteed that these roads form a tree.
It is guaranteed that the sum of n over all test cases does not exceed 3⋅105.
每个测试包含多个测试用例。第一行包含一个整数 t(1≤t≤104),表示测试用例的数量。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(2≤n≤3⋅105),表示城市中交叉路口的数量。
接下来的 (n−1) 行描述道路。第 i 行包含两个整数 ui 和 vi(1≤ui,vi≤n,且 ui=vi),表示由第 i 条道路连接的两个交叉路口的编号。
保证这些道路构成一棵树。
保证所有测试用例的 n 值之和不超过 3⋅105。
输出格式
For each test case, output a single integer — the number of good sets of intersections modulo 998244353.
对于每个测试用例,输出一个整数——满足条件的交叉点集合的数量对 998244353 取模的结果。
输入输出样例
输入#1
4 3 1 3 3 2 4 3 4 2 3 3 1 5 1 2 3 4 5 1 2 3 4 1 2 2 3 3 4
输出#1
7 12 16 11
说明/提示
In the first test case, there are 23=8 sets of intersections. All of them are good, except for the set 1,2,3, because if intersections 1,2, and 3 are dangerous, then the simple path 1−2−3 contains 3 dangerous intersections. Thus, there are 7 good sets.
In the second test case, there are 24=16 sets of intersections. Among them, the sets 1,2,3,4, 1,2,3, 1,3,4, 2,3,4 are not good. Thus, there are a total of 12 good sets. The city layout is shown below:

在第一个测试用例中,共有 23=8 个路口集合。其中除集合 1,2,3 外,其余均为好集合,因为若路口 1、2 和 3 均为危险路口,则简单路径 1−2−3 将包含 3 个危险路口。因此,共有 7 个好集合。
在第二个测试用例中,共有 24=16 个路口集合。其中,集合 1,2,3,4、1,2,3、1,3,4 和 2,3,4 不是好集合。因此,好集合总数为 12 个。城市布局如下所示:

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