Centrifuge

시간 제한1.5초메모리 제한2048 MB

요약
각 노드에 유체량이 주어진 트리에서 루트를 무작위로 고르고 바깥 방향으로 흐르며 각 분기에서 균등하게 나뉠 때 각 노드에 도달하는 유체량의 기댓값을 구한다.
난이도

어려움10점 중 8점

유형
트리, DFS, 수학, 조합론
정답자
아직 제출이 없습니다

문제

On your latest intergalactic scavenger trip, you discovered an abandoned futuristic robot. Mysteriously, the robot consists of nn ball-shaped joints with some kind of fluid inside. These joints are all connected together via n−1n-1 flexible pipes that allow the fluid to flow bidirectionally from one joint to another. To analyze this strange fluid inside the robot, you decide to use a centrifuge.

Since you have no clue how to actually use a centrifuge, you secure one of the joints to the center of it and turn the machine on. The machine then rapidly spins around the center, and all the fluids are pushed away from the center towards the outside. Whenever the fluid has more than one pipe to flow through, the fluid splits evenly between all of these pipes.

You wonder how much fluid will be in the outermost joints at the end of this process. After thinking so much, you forgot which joint you secured to the center. Therefore, you have to calculate the expected amount of fluid in each joint as if you chose a joint at random.

입력

The first line contains one integer nn (1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5) --- the number of joints of the robot.

The next line contains nn integers a_ia\_i (0≤a_i≤1090 \leq a\_i \leq 10^9) --- the amount of fluid in the ii-th joint.

The next n−1n-1 lines contain two integers uu and vv (1≤u,v≤n1 \leq u, v \leq n) --- indicating a pipe between joint uu and vv. These pipes form a tree.

출력

Output nn lines with one integer per line, the ii-th representing the expected amount of fluid in the ii-th joint after the process modulo 998,244,353998\\,244\\,353.

Formally, let M=998,244,353M = 998\\,244\\,353. It can be shown that the answer can be expressed as an irreducible fraction pq\frac{p}{q}, where pp and qq are integers and q≢0(modM)q \not \equiv 0 \pmod{M}. Output the integer equal to p⋅q−1 mod Mp \cdot q^{-1} \bmod M. In other words, output such an integer xx that 0≤x<M0 \le x < M and x⋅q≡p(modM)x \cdot q \equiv p \pmod{M}.

힌트

In the first sample, if the first joint is fixed at the center, all fluid will flow from joint 11 to 22, and the combined fluid will flow to joint 33. In total, joint 11 and 22 will be empty, and all 77 units of fluid will be at joint 33.

If the second joint is fixed to the center, half of its fluid will flow to 11 and half will flow to 33. There will be 22, 00, and 55 units of fluid at each joint respectively. If joint 33 is fixed, all 77 units of fluid will be at joint 11.

In total, the expected amount of fluid in each joint is

13⋅(0+2+7)=3\frac{1}{3} \cdot (0 + 2 + 7) = 3

13⋅(0+0+0)=0\frac{1}{3} \cdot (0 + 0 + 0) = 0

13⋅(7+5+0)=4\frac{1}{3} \cdot (7 + 5 + 0) = 4

예제2

  1. 예제 1

    입력
    3
    1 2 4
    1 2
    2 3
    
    예상 출력
    3
    0
    4
    
  2. 예제 2

    입력
    6
    4 1 3 11 7 2
    1 4
    2 3
    6 2
    2 4
    4 5
    
    예상 출력
    928921837
    0
    818005794
    0
    679360751
    568444705