luogu#P10706. 悲哀(Sorrow)
悲哀(Sorrow)
Background
Problem Description
Given a tree with nodes rooted at node . Each node has two weights (). The values are given, and all are initially .
Now, for every pair of nodes (), let . If , then set ; otherwise, do nothing.
For each , output .
Input Format
The first line contains an integer , meaning there are nodes.
The second line contains positive integers, representing .
The next lines each contain two positive integers , indicating there is an edge between node and node .
Output Format
Output lines, each containing one integer.
The -th line should be the value of modulo .
8
3 7 2 2 2 3 72 24
1 2
1 3
1 4
4 5
2 6
5 7
4 8
785
0
0
1972
144
0
0
0
15
73 83 31 9514 1189 43 79 2 2 1798 5063 2 5 2573 53
1 2
2 3
1 4
4 5
1 6
3 7
5 8
5 9
6 10
7 11
10 12
9 13
7 14
13 15
23952214
633788
79763
38056
4
0
13027099
0
0
3596
0
0
0
0
0
Hint
Sample Explanation

The constructed tree is shown in the figure.
The following contributions exist:
- For node :
contributes .
contributes .
contributes .
contributes .
contributes .
contributes .
contributes .
contributes .
contributes .
Total: .
- For node :
contributes .
contributes .
contributes .
contributes .
contributes .
Total: .
- For node :
contributes .
Total: .
All other nodes are clearly .
Constraints
| Subtask ID | Special Property | Score | ||
|---|---|---|---|---|
Special property : it is guaranteed that all are generated randomly.
Special property : it is guaranteed that the tree is a complete binary tree.
For of the testdata: , .
Special note: This problem uses bundled subtask testing. You will only get the score for a subtask if you pass all test points in that subtask.
Translated by ChatGPT 5