#abc340g. G - Leaf Color

G - Leaf Color

Score: 600600 points

问题陈述

存在一棵具有 NN 个顶点的树 TT,顶点编号从 11NN。第 ii 条边连接顶点 uiu_iviv_i。此外,顶点 ii 被涂上了颜色 AiA_i

求满足以下条件的顶点集合 SS(非空子集)的数量,对 998244353998244353 取模:

  • SS 确定的 TT 的子图 GG 满足所有以下条件:
    • GG 是一棵树。
    • 所有度数为 11 的顶点颜色相同。

什么是导出子图?设 SS 是图 GG 中顶点的一个子集。由 SS 导出的子图是一个图,其顶点集合为 SS,边集合包含 GG 中两端点均在 SS 内的所有边。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There is a tree TT with NN vertices numbered from 11 to NN. The ii-th edge connects vertices uiu_i and viv_i. Additionally, vertex ii is painted with color AiA_i.
Find the number, modulo 998244353998244353, of (non-empty) subsets SS of the vertex set of TT that satisfy the following condition:

  • The induced subgraph GG of TT by SS satisfies all of the following conditions:
    • GG is a tree.
    • All vertices with degree 11 have the same color.

What is an induced subgraph? Let SS be a subset of the vertices of a graph GG. The induced subgraph of GG by SS is a graph whose vertex set is SS and whose edge set consists of all edges in GG that have both endpoints in SS.

Constraints

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 1AiN1 \leq A_i \leq N
  • 1ui<viN1 \leq u_i \lt v_i \leq N
  • 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:

NN

A1A_1 A2A_2 \dots ANA_N

u1u_1 v1v_1

u2u_2 v2v_2

\vdots

uN1u_{N-1} vN1v_{N-1}

Output

Print the number, modulo 998244353998244353, of (non-empty) subsets SS of the vertex set of TT 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.

  • {1}\lbrace 1 \rbrace
  • {1,2,3}\lbrace 1, 2, 3 \rbrace
  • {2}\lbrace 2 \rbrace
  • {3}\lbrace 3 \rbrace

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