#P17267. [ICPC 2017 Urumqi R] A Possible Tree
[ICPC 2017 Urumqi R] A Possible Tree
Problem Description
Alice knows that Bob has a secret tree (in terms of graph theory) with nodes with weighted edges with integer values in . She knows its structure but does not know the specific information about edge weights.
Thanks to the awakening of Bob’s conscience, Alice gets conclusions related to his tree. Each conclusion provides three integers and saying that the exclusive sum of edge weights in the unique shortest path between and is equal to .
Some conclusions provided might be wrong and Alice wants to find the maximum number such that the first given conclusions are compatible. That is say that at least one allocation of edge weights satisfies the first conclusions all together but no way satisfies all the first conclusions (or there are only conclusions provided in total).
Help Alice find the exact value of .
Input Format
The input has several test cases and the first line contains an integer which is the number of test cases.
For each case, the first line contains two integers and which are the number of nodes in the tree and the number of conclusions provided. Each of the following lines contains two integers and indicating an edge in the tree between the -th node and the -th node. Each of the following lines provides a conclusion with three integers , and where , and .
Output Format
For each test case, output the integer in a single line.
2
7 5
1 2
2 3
3 4
4 5
5 6
6 7
1 3 1
3 5 0
5 7 1
1 7 1
2 3 2
7 5
1 2
1 3
1 4
3 5
3 6
3 7
2 6 6
4 7 7
6 7 3
5 4 5
2 5 6
3
4