#P13597. 『GTOI - 1D』回归空白
『GTOI - 1D』回归空白
Background
Even if I myself will sink into boundless sorrow.
I still have to return to blank, to a blank future……
Problem Description
Lingluo has an unrooted tree with nodes. The -th edge connects node and node . Node has a positive integer on it.
Sometimes, the numbers on the tree will change. The numbers on the two endpoints of the -th edge will be swapped, i.e., and will be exchanged.
Sometimes Lingluo will ask you: if at the beginning only the nodes on the simple path from node to node are white and all other nodes are blue, then if you repeatedly perform the following step until all nodes become white, what is the expected number of steps?
Randomly choose one blue node and one white node . The probability of choosing each node is proportional to the number on that node. Then paint all nodes on the simple path from node to node to white.
Specifically, if node is white, the probability of choosing it is
If node is blue, the probability of choosing it is
where denotes the color of node : means white, and means blue.
Due to the problem setter’s deliberate design, you need to output the result of the answer .
Can you correctly answer every question from Lingluo?
Input Format
The first line contains two positive integers , representing the number of nodes in the tree and the total number of operations.
The second line contains positive integers. The -th integer is , representing the number on node .
The next lines describe the edges of the tree. Line contains two positive integers , describing an edge .
Then follow lines, each representing one modification or one query. First read a positive integer indicating the operation type:
- If , read a positive integer , meaning to swap the numbers and on the two endpoints of the -th edge.
- If , read two positive integers , meaning one query from Lingluo.
Output Format
For each query, output one line with one integer, representing the answer.
5 5
1 2 3 4 5
1 2
1 3
2 4
2 5
2 1 5
2 3 3
1 1
2 4 5
2 1 3
2
719696977
800000007
700000007
Hint
Sample Explanation
In the sample, the answers to the last three queries, written as fractions, are , , and .
Constraints
This problem uses bundled testdata.
| Special Property | Score | ||
|---|---|---|---|
| None | |||
| , | |||
| It is guaranteed that in all queries | |||
| None | |||
For all testdata, it is guaranteed that: , , the input graph is a tree, , , , in queries , and in modifications .
Note
Please pay attention to the impact of constant factors on program efficiency.
Translated by ChatGPT 5