#abc340g. G - Leaf Color
G - Leaf Color
Score: points
问题陈述
存在一棵具有 个顶点的树 ,顶点编号从 到 。第 条边连接顶点 和 。此外,顶点 被涂上了颜色 。
求满足以下条件的顶点集合 (非空子集)的数量,对 取模:
- 由 确定的 的子图 满足所有以下条件:
- 是一棵树。
- 所有度数为 的顶点颜色相同。
什么是导出子图?设 是图 中顶点的一个子集。由 导出的子图是一个图,其顶点集合为 ,边集合包含 中两端点均在 内的所有边。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
There is a tree with vertices numbered from to . The -th edge connects vertices and . Additionally, vertex is painted with color .
Find the number, modulo , of (non-empty) subsets of the vertex set of that satisfy the following condition:
- The induced subgraph of by satisfies all of the following conditions:
- is a tree.
- All vertices with degree have the same color.
What is an induced subgraph? Let be a subset of the vertices of a graph . The induced subgraph of by is a graph whose vertex set is and whose edge set consists of all edges in that have both endpoints in .
Constraints
- The graph given in the input is a tree.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the number, modulo , of (non-empty) subsets of the vertex set of that satisfy the condition in the problem statement.
Sample Input 1
3
1 2 1
1 2
2 3
Sample Output 1
4
The following four sets of vertices satisfy the condition.
Sample Input 2
5
2 2 1 1 1
2 5
3 4
1 3
1 5
Sample Output 2
9
Sample Input 3
15
5 3 5 1 1 4 4 4 2 5 5 4 4 2 5
3 13
4 10
7 11
8 9
2 10
2 14
5 11
5 6
6 13
12 13
9 14
9 13
1 13
1 15
Sample Output 3
48
update @ 2024/3/10 01:33:43