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

25702번 - DFS 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
8 초 (추가 시간 없음) 1024 MB411100.000%

문제

You are given a rooted tree of $n$ vertices, and $r$ is the root of the tree. Each vertex $x$ has value $a_x$.

Let us define the DFS procedure starting from $x$ to find $y$:

  1. Push $x$ on the stack.
  2. Check $w,ドル the top element of the stack. If $w = y,ドル the procedure ends. Otherwise, if there is at least one son of $w$ which is not visited, choose one such son with equal probability and push it on the stack.
  3. Repeat step 2 until there is no unvisited son.
  4. Pop the top element from the stack.
  5. Repeat step 2 until the stack is empty.

The procedure is legal if and only if $y$ is in the subtree of $x$.

Define $f(x,y)$ as the expectation of the minimum value of all vertices which were pushed on the stack during the DFS procedure starting from $x$ to find $y$.

Now we want to calculate $\sum f(x,y)$ for all legal pairs $(x,y)$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{x}{y},ドル where $x$ and $y$ are integers and $y\not \equiv 0\pmod {998,244円,353円}$. Output the integer equal to $x\cdot y^{-1}\pmod {998,244円,353円}$. In other words, output an integer $a$ such that 0ドル\leq a < 998,244円,353円$ and $a\cdot y\equiv x\pmod {998,244円,353円}$.

입력

The first line contains an integer $T$ (1ドル \leq T \leq 100$), denoting the number of test cases.

For each test case, the first line contains two integers $n$ and $r$ (1ドル \leq n \leq 4 \cdot 10^5,ドル 1ドル \leq r \leq n$), denoting the number of vertices in the tree and the root.

The following line contains $n$ integers, the $i$-th integer of them is $a_i $(1ドル\leq a_i\leq 10^9$) denoting the value of vertex $i$.

Each of the next $n-1$ lines contains two integers $u$ and $v$ (1ドル \leq u,v \leq n$), denoting an edge of the tree.

It is guaranteed that $\sum n \leq 8 \cdot 10^5$. It is also guaranteed that the given graph is indeed a tree.

출력

Output $T$ lines. Each line must contain one integer: the answer to the respective test case.

제한

예제 입력 1

4
1 1
1
3 3
3 3 4
3 1
3 2
6 1
5 2 4 1 3 6
1 2
1 6
2 3
2 4
4 5
5 1
5 4 3 2 1
1 2
1 3
3 4
3 5

예제 출력 1

1
16
34
499122202

힌트

출처

Camp > Petrozavodsk Programming Camp > Summer 2022 > Day 5: ZJU Contest 2 K번

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

출처

대학교 대회

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

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