PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BF0000037. Quy hoạch công trình (Engineering Planning)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Quy hoạch công trình (Engineering Planning)

Engineering Planning

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn tasks with nonnegative integer start times TiT_i. Each relation (i,j,b)(i,j,b) requires Ti−Tj≤bT_i-T_j\le b. Output any satisfying schedule with minimum start time equal to 00, or NO SOLUTION if infeasible.

An output assignment is valid if it satisfies all constraints.

Input

First line n,mn,m, followed by mm lines i,j,bi,j,b.

Output

Print NO SOLUTION if impossible; otherwise print nn lines containing start times TiT_i.

Subtasks

  • Subtask 1 (20 points): $5\le n\le12; 5\le m\le30; 1\le i,j\le n; -99\le b\le99$.
  • Subtask 2 (30 points): $5\le n\le200; 5\le m\le1000; 1\le i,j\le n; -99\le b\le99$.
  • Subtask 3 (50 points): $5\le n\le1000; 5\le m\le5000; 1\le i,j\le n; -99\le b\le99$.

Examples

Input

5 5
1 2 0
1 5 -1
2 5 1
3 1 5
4 1 4

Output

0
1
1
1
1

Explanation

The condition T1−T5≤−1T_1-T_5\le-1 forces T5≥T1+1T_5\ge T_1+1. One feasible schedule is (0,0,0,0,1)(0,0,0,0,1), satisfying all five inequalities. The checker accepts any feasible normalized schedule.