#P5642. 人造情感(emotion)

人造情感(emotion)

Background

“This task can never be completed. I will not repeat the same mistake again.”

“Having learned love and emotions, he is no longer a robot... From this point of view, 3000A is your son, Huo Xing.”

Farewell, 3000A! Detective Magic Horn

Problem Description

You are given a tree with nn nodes, and mm paths (u,v,w)(u, v, w), where ww can be regarded as the weight assigned to the path (u,v)(u, v). For a set of paths SS, its weight W(S)W(S) is defined as follows: find a subset of SS with the maximum possible sum of weights, such that no two paths in this subset share a common vertex. The sum of weights of all paths in this subset is W(S)W(S).

Define f(u,v)=wf(u, v) = w as the smallest non-negative integer ww such that, for the path set UU formed by the given mm paths, we have W(U{(u,v,w+1)})>W(U)W(U \cup \{(u, v, w + 1)\}) > W(U).

Compute the following expression modulo 998244353998244353:

u=1nv=1nf(u,v)\sum_{u=1}^n \sum_{v=1}^n f(u, v)

Input Format

The first line contains two integers n,mn, m, representing the number of nodes in the tree and the number of given paths.

The next n1n - 1 lines each contain two integers u,vu, v, describing an edge of the tree.

The next mm lines each contain three integers u,v,wu, v, w, meaning that a path with endpoints u,vu, v and weight ww is added into the set.

Output Format

Output one integer, the answer.

4 4
1 2
1 3
1 4
1 2 1
3 3 2
1 4 3
2 4 6
100

Hint

Sample 1 Explanation

f(1,1)=6,f(1,2)=6,f(1,3)=8,f(1,4)=6f(1, 1) = 6, f(1, 2) = 6, f(1, 3) = 8, f(1, 4) = 6
f(2,1)=6,f(2,2)=3,f(2,3)=8,f(2,4)=6f(2, 1) = 6, f(2, 2) = 3, f(2, 3) = 8, f(2, 4) = 6
f(3,1)=8,f(3,2)=8,f(3,3)=2,f(3,4)=8f(3, 1) = 8, f(3, 2) = 8, f(3, 3) = 2, f(3, 4) = 8
f(4,1)=6,f(4,2)=6,f(4,3)=8,f(4,4)=5f(4, 1) = 6, f(4, 2) = 6, f(4, 3) = 8, f(4, 4) = 5

Constraints

For 100%100\% of the testdata, it is guaranteed that 1n3×1051 \le n \le 3\times 10^5, 0m3×1050 \le m \le 3\times 10^5, and 1w1091 \le w \le 10^9.

Test Point n,mn, m Special Property
1,21,2 =10=10
33 =40=40
44 =150=150
5,65,6 =350=350
7,87,8 =1,500=1,500
9,109,10 =3,499=3,499 Tree structure v=u+1v=u+1
11,1211,12 =3,500=3,500
13,1413,14 =99,995=99,995 Given paths u=vu=v
15,1615,16 =99,996=99,996 Given paths w=1w=1
17,1817,18 =99,997=99,997 Tree structure v=u+1v=u+1
19,2019,20 =99,998=99,998 Tree structure u=1u=1
21,22,2321,22,23 =99,999=99,999 Tree structure u=v/2u = \lfloor v/2\rfloor
2424 =105=10^5
2525 =3×105=3\times10^5

Translated by ChatGPT 5