题目收集
题目列表题单
返回列表

Cover the Paths

CodeforcesP0064· 1000ms· 256 MB· ZJU Summer 2026 Contest 6原题链接
贪心数据结构优化

创建于 2026/07/18 · 更新于 2026/07/18

题目描述

You are given a rooted tree with nnn vertices. The root is vertex 111.

You are also given mmm paths. The iii-th path is described by two vertices aia_iai​ and bib_ibi​ and an integer cic_ici​; it consists of all edges on the simple path between aia_iai​ and bib_ibi​. It is guaranteed that aia_iai​ is an ancestor of bib_ibi​, or bib_ibi​ is an ancestor of aia_iai​.

You may place markers on the edges of the tree. Each edge can hold any number of markers. For each iii, at least cic_ici​ of the placed markers must lie on the edges of the iii-th path.

Find the minimum total number of markers you need to place.

输入格式

The first line contains nnn and mmm.

Each of the next n−1n-1n−1 lines contains uuu and vvv (1≤u,v≤n1 \le u, v \le n1≤u,v≤n, u≠vu \ne vu=v) — an edge of the tree.

Each of the next mmm lines contains aia_iai​, bib_ibi​, cic_ici​ (1≤ai,bi≤n1 \le a_i, b_i \le n1≤ai​,bi​≤n, ai≠bia_i \ne b_iai​=bi​, 1≤ci≤1091 \le c_i \le 10^91≤ci​≤109); it is guaranteed that aia_iai​ is an ancestor of bib_ibi​, or bib_ibi​ is an ancestor of aia_iai​.

2≤n≤1052 \le n \le 10^52≤n≤105, 1≤m≤1051 \le m \le 10^51≤m≤105.

输出格式

Print a single integer — the minimum total number of markers.

样例输入 #1

2 1
1 2
1 2 6

样例输出 #1

6

样例输入 #2

5 3
3 1
4 3
5 2
4 5
2 4 2
2 3 3
5 3 2

样例输出 #2

3