Logo
(追記) (追記ここまで)

31153번 - Werewolves 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 1024 MB167563022.727%

문제

You are given a colored tree on $n$ nodes, node $i$ has color $c_i$. As a reminder, a tree on $n$ nodes is a connected graph with $n-1$ edges.

Compute the number of connected subgraphs of this tree, for which there exists some color (majority color), such that strictly more than half of the nodes of this subgraph have this color.

Since the answer can be quite large, compute it modulo 998ドル,244円,353円$.

입력

The first line of input contains one integer $n$ (1ドル \le n \le 3000$) --- the number of nodes in the tree.

The second line contains $n$ integers $c_1\ c_2\ \ldots\ c_n$ (1ドル \le c_i \le n$) --- the colors of the nodes.

The $i$-th of next $n-1$ lines contains 2ドル$ integers $u_i, v_i$ (1ドル \le u_i, v_i \le n,ドル $u_i \neq v_i$), representing the edge $(u_i, v_i)$ of the tree. It is guaranteed that the given graph is a tree.

출력

Print a single integer --- answer to the problem modulo 998ドル,244円,353円$.

제한

예제 입력 1

3
2 3 3
1 2
2 3

예제 출력 1

5

예제 입력 2

4
1 1 3 3
1 2
1 3
1 4

예제 출력 2

8

노트

In the following pictures, we use blue for color 1ドル,ドル red for color 2ドル,ドル and yellow for color 3ドル$. The first example looks as follows:

The tree has a total of 6ドル$ non-empty connected subgraphs: 3ドル$ of size 1ドル,ドル 2ドル$ of size 2ドル$ and 1ドル$ of size 3ドル,ドル the latter in fact being the whole tree. All such subgraphs of sizes 1ドル$ and 3ドル$ have a majority color. For those of size 2ドル$ only the subgraph induced by vertices 1ドル$ and 2ドル$ does not have a majority color (red and yellow both appear equally often in it). Therefore, there are 6ドル - 1 = 5$ connected subgraphs with a majority color.

The second example looks as follows, and it has 8ドル$ connected subgraphs with a majority color:

출처

Contest > Open Cup > 2021/2022 Season > Stage 7: Grand Prix of Southeastern Europe H번

ICPC > Regionals > Europe > Southeastern European Regional Contest > SEERC 2021 C번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /