#P10733. Constructing an Array from Min-Relation Constraints

    ID: 12767 Type: Default 1000ms 256MiB

Constructing an Array from Min-Relation Constraints

Constructing an Array from Min-Relation Constraints

You are given M relations of the form \( \min(X_{A_i}, X_{B_i}) = C_i \) where \(X\) is an array of length \(N\) and each \(X_i\) must lie in the range \( [1,10^9] \). Your task is to construct one such array \(X\) that satisfies all the given relations. It is guaranteed that a solution exists.

Constraint Explanation:
For each relation \(\min(X_{A_i}, X_{B_i}) = C_i\), both \(X_{A_i}\) and \(X_{B_i}\) must be at least \(C_i\), and at least one of them must be exactly \(C_i\). All indices are 1-indexed.

Input Format:
The first line contains two integers \(N\) and \(M\). Each of the next \(M\) lines contains three integers \(A_i\), \(B_i\) and \(C_i\) representing a relation \(\min(X_{A_i},X_{B_i})=C_i\).

Output Format:
Output the array \(X\) of \(N\) integers separated by spaces. If an index does not appear in any relation, you may assign it any value in the range \([1,10^9]\) (for example, \(10^9\)).

inputFormat

The input begins with a line containing two integers \(N\) and \(M\) (the array length and number of relations). Then \(M\) lines follow; each line contains three integers \(A_i\), \(B_i\), and \(C_i\) indicating a relation \(\min(X_{A_i},X_{B_i}) = C_i\). Indices are 1-indexed.

outputFormat

Output a single line containing \(N\) integers \(X_1, X_2, \ldots, X_N\) separated by spaces, which form a valid array satisfying all the relations.

sample

3 2
1 2 5
2 3 3
5 5 3