luogu#P4842. 城市旅行

城市旅行

Problem Description

Country W is vast and rich. It consists of nn cities, connected by n1n-1 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 ii as aia_i. For a trip from city xx to city yy, the happiness index gained is the sum of the beauties of all cities on the path from city xx to city yy (including xx and yy). We define this value as H(x,y)H(x,y).

Now, Xiao A is in city XX and Sharon is in city YY. They want to know: if we choose any two cities xx and yy (where xx may equal yy) among all cities on the path from city XX to city YY, what is the expected value of H(x,y)H(x,y)? We denote this expected value as E(x,y)E(x,y).

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 n,mn,m, representing the number of cities and the number of operations.

The next line contains nn integers, where the ii-th integer represents aia_i.
The next n1n-1 lines each contain two integers u,vu,v, indicating there is a road between uu and vv.
The next mm lines describe the following operations:

  • 1 u v If there is no road directly connecting cities uu and vv, ignore this operation; otherwise, delete the road between uu and vv.
  • 2 u v If cities uu and vv are connected, ignore this operation; otherwise, add a road between uu and vv.
  • 3 u v d If cities uu and vv are not connected, ignore this operation; otherwise, increase the beauty of all cities on the path from city uu to city vv (including uu and vv) by dd.
  • 4 u v Query the value of E(u,v)E(u,v).

Output Format

For operation 4, output the answer as a reduced fraction pq\frac{p}{q}. If uu and vv 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