Link Cut Tree

View as PDF

Submit solution

Points: 50
Time limit: 4.0s
Memory limit: 256M

Problem types

Larry was playing with a link cut tree last night. He has made Q LCA queries to his link cut tree. Each time, the link cut tree responded with the integer representing the lowest common ancestor. He cut one too many edges and now he can't restore his tree. However, he will be satisfied with any tree that will give him the same results when asked the LCA queries again. Your task is to help him restore his tree. You will be given the Q queries, and the result of those queries.

Input Specification

On the first line, two integers, N and Q (1N,Q105) where N is the number of nodes.

On the next Q lines, three integers ui vi and wi, which indicates that the LCA of ui and vi is wi. (1ui,vi,wiN).

Output Specification

N integers, indicating that the parent of i is pi. (e.g. if the first integer is 2, then the parent of 1 is 2, if the second integer is 5, then the parent of 2 is 5). Node 1 is always the root, and has special value 0 as the parent. In other words, p1=0.

Sample Input

Copy
4 3
1 2 1
2 3 2
3 4 3

Sample Output

Copy
0 1 2 3

Comments

There are no comments at the moment.