Werewolves
Time limit2sMemory limit1024 MB
Count connected subgraphs of a colored tree in which one color makes up strictly more than half of the nodes, modulo 998244353.
- Level
Hard8 of 10
- Topics
- Tree, Divide and conquer, Dynamic programming
- Solved
- No attempts yet
Problem
You are given a colored tree on nodes, and node has color . A tree on nodes is a connected graph with edges.
Count the connected subgraphs of this tree for which some color (the majority color) appears on strictly more than half of the subgraph's nodes. Since the answer can be large, print it modulo .
Input
The first line contains one integer (), the number of nodes. The second line contains integers (), the colors of the nodes. Each of the next lines contains two integers and (, ), an edge of the tree. The given graph is a tree.
Output
Print one integer, the number of connected subgraphs with a majority color, modulo .
Hint
In the figures, color 1 is blue, color 2 is red, and color 3 is yellow. In the first figure, the subgraph on nodes 1 and 2 has no majority color because red and yellow each appear once.

