Sequence Reconstruction
Time limit3sMemory limit256 MB
Given M equations of the form a_i + a_j = k, reconstruct the integer sequence a_1..a_N or report ERROR when more than one solution exists.
- Level
Hard8 of 10
- Topics
- Graph, Union-find, DFS, Math
- Solved
- No attempts yet
Problem
Wookje was playing with addition on a sequence of integers of length , but he lost the paper with the sequence written on it. Fortunately, the records he was playing with survive. Each record consists of three integers: the indices of two numbers and their sum . Using this information, you must reconstruct the original sequence.
Input
The first line gives the length of the sequence and the number of records.
Each of the next lines contains three integers separated by spaces. This means .
No input lacks a solution.
Output
Print the elements of the sequence Wookje was playing with, separated by spaces.
The elements of the sequence are guaranteed to be integers.
If more than one solution exists, print "ERROR" (without the quotes).