luogu#P4842. 城市旅行
城市旅行
Problem Description
Country W is vast and rich. It consists of cities, connected by bidirectional roads, each road connecting two cities. Any two cities can reach each other.
The beautiful scenery of Country W attracts many tourists. Based on the tourists' ratings for each city, we define the beauty of city as . For a trip from city to city , the happiness index gained is the sum of the beauties of all cities on the path from city to city (including and ). We define this value as .
Now, Xiao A is in city and Sharon is in city . They want to know: if we choose any two cities and (where may equal ) among all cities on the path from city to city , what is the expected value of ? We denote this expected value as .
Of course, the traffic conditions between cities are unpredictable, so we cannot rule out that at some moments some roads become impassable. At some moments, new roads may be added. Also, as tourists' tastes change, the beauty of some cities may change as well. As Mr. T, who is responsible for the tourism industry in Country W, requires you to write a program to simulate all the processes above.
Input Format
The first line contains two integers , representing the number of cities and the number of operations.
The next line contains integers, where the -th integer represents .
The next lines each contain two integers , indicating there is a road between and .
The next lines describe the following operations:
1 u vIf there is no road directly connecting cities and , ignore this operation; otherwise, delete the road between and .2 u vIf cities and are connected, ignore this operation; otherwise, add a road between and .3 u v dIf cities and are not connected, ignore this operation; otherwise, increase the beauty of all cities on the path from city to city (including and ) by .4 u vQuery the value of .
Output Format
For operation 4, output the answer as a reduced fraction . If and are not connected, output -1.
4 5
1 3 2 5
1 2
1 3
2 4
4 2 4
1 2 4
2 3 4
3 1 4 1
4 1 4
16/3
6/1
Hint
For all testdata, $1\le N\le 50000,1\le M\le 50000,1\le a_i\le 10^6,1\le D\le 100,1\le u,v\le N$.
Translated by ChatGPT 5