Queries and Trees 2
Time limit3sMemory limit1024 MB
Given M pairs of vertices and the LCA of each pair in an unknown rooted tree (root 1), print the parent of every vertex.
- Level
Medium7 of 10
- Topics
- Tree, Union-find, Graph
- Solved
- No attempts yet
Problem
A tree with N () vertices is given. Each vertex is numbered from 1 to N, and vertex 1 is the root.
You, who like trees and queries, are given M () pairs of vertices, along with the lowest common ancestor of each pair.
However, you do not actually know what the tree is.
Input
The first line contains two integers N and M.
The next M lines each contain u, v, and w, meaning the lowest common ancestor of vertices u and v is vertex w. ()
Output
Print N integers. The i-th number is the parent of vertex i. The parent of the root (vertex 1) must be printed as 0. The output must represent a valid rooted tree.
Only inputs where an answer exists are given.