Centrifuge
InterviewTime limit1.5sMemory limit2048 MB
Given a tree with fluid in each node, find each node's expected final fluid when a random node is the center and fluid flows outward, mod 998244353.
Problem
On your latest intergalactic scavenger trip, you discovered an abandoned futuristic robot. The robot consists of ball-shaped joints with fluid inside. These joints are connected by flexible pipes that let the fluid flow in both directions. To analyze the fluid, you decide to use a centrifuge.
You have no idea how to operate a centrifuge, so you fix one joint to its center and turn the machine on. The machine spins quickly, and all the fluid is pushed from the center toward the outside. Whenever fluid has more than one pipe to flow through, it splits evenly among those pipes.
You want to know how much fluid remains in the outermost joints when the process ends. You forgot which joint you fixed to the center, so assume the joint was chosen uniformly at random. Compute the expected amount of fluid in each joint.
Input
The first line contains one integer (), the number of joints.
The next line contains integers (), the amount of fluid in the -th joint.
The next lines each contain two integers and (), meaning there is a pipe between joint and joint . These pipes form a tree.
Output
Output lines with one integer each. The -th line is the expected amount of fluid in joint after the process, modulo .
Formally, let . The answer can be written as an irreducible fraction with . Output the integer with and .
Hint
In the first sample, if joint 1 is fixed at the center, all fluid flows from joint 1 to joint 2, and the combined fluid flows to joint 3. In total, joints 1 and 2 are empty, and all 7 units of fluid are at joint 3.
If joint 2 is fixed at the center, half of its fluid flows to joint 1 and half to joint 3. The joints then hold 2, 0, and 5 units. If joint 3 is fixed, all 7 units end up at joint 1.
The expected amounts are: