#P9584. 「MXOI Round 1」城市
「MXOI Round 1」城市
Problem Description
Little C is the president of Country F. Although this country exists only in an online game, he is indeed the president of this country.
Country F consists of cities. These cities are connected by bidirectional roads. It is guaranteed that starting from any city, you can reach any other city through these bidirectional roads.
Of course, using these bidirectional roads costs money. Passing through the -th bidirectional road costs yuan. We call the cost of the -th bidirectional road.
We define as the sum of the costs of all bidirectional roads on the simple path from city to city . In particular, when , .
To promote the development of Country F, Little C built a new city . Now he needs to build one more bidirectional road so that city can also reach any city through these bidirectional roads.
He has a total of road-building plans. Each plan provides two parameters . For each plan, you need to compute, after building a bidirectional road connecting city and city with cost , the sum of all , i.e. $\sum\limits_{i=1}^{n+1} \sum\limits_{j=1}^{n+1} cost(i,j)$.
Since the answer may be very large, you only need to output the result modulo .
The plans are independent of each other, meaning none of the plans will affect the existing roads, and these plans will not actually be carried out.
Input Format
The first line contains two integers .
The next lines: the -th line contains three integers , indicating that there is a bidirectional road connecting city and city with cost .
The next lines: the -th line contains two integers , indicating a plan for building a new road.
Output Format
Output lines, each containing one integer. The integer on the -th line denotes, after building a bidirectional road connecting city and city with cost , the sum of all modulo , i.e. $\sum\limits_{i=1}^{n+1} \sum\limits_{j=1}^{n+1} cost(i,j) \bmod 998244353$.
4 2
2 1 3
3 2 2
4 2 4
1 2
2 2
100
88
9 5
2 3 6
6 1 4
5 2 10
2 4 1
9 1 9
2 8 3
1 2 3
7 4 8
4 9
7 3
6 1
9 7
2 1
1050
1054
970
1148
896
Hint
Sample Explanation #1
After building a bidirectional road connecting city and city with cost , the roads of Country F are as shown in the figure below:

For example, in this case, and .
It is easy to obtain that $\sum\limits_{i=1}^{n+1} \sum\limits_{j=1}^{n+1} cost(i,j)=100$.
Sample #3
See city/city3.in and city/city3.ans in the additional files.
This sample satisfies the constraints of test point .
Sample #4
See city/city4.in and city/city4.ans in the additional files.
This sample satisfies the constraints of test point .
Sample #5
See city/city5.in and city/city5.ans in the additional files.
This sample satisfies the constraints of test point .
Sample #6
See city/city6.in and city/city6.ans in the additional files.
This sample satisfies the constraints of test point .
Constraints
For of the data, , , , . It is guaranteed that starting from any city, you can reach any other city through the original bidirectional roads.
| Test Point ID | Special Property | ||
|---|---|---|---|
| None | |||
| A | |||
| B | |||
| None | |||
Special Property A: It is guaranteed that for all , .
Special Property B: It is guaranteed that for all , .
Translated by ChatGPT 5