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

#BF0000027. Mẫu hệ bất đẳng thức hiệu (Difference Constraints Template)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Mẫu hệ bất đẳng thức hiệu (Difference Constraints Template)

Difference Constraints Template

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

Given nn integer variables and mm constraints xa−xb≤cx_a-x_b\le c, output any integer assignment satisfying all constraints, or NO if no such assignment exists.

An output assignment is valid if it satisfies all constraints.

Input

First line: n,mn,m. Each of the next mm lines contains a,b,ca,b,c, expressing xa−xb≤cx_a-x_b\le c.

Output

Print NO if infeasible; otherwise print nn integer values x1,…,xnx_1,\ldots,x_n.

Subtasks

  • Subtask 1 (20 points): $2\le n\le 12; 1\le m\le 30; 1\le a,b\le n; a\ne b; -10^4\le c\le 10^4$.
  • Subtask 2 (30 points): $2\le n\le 200; 1\le m\le 1200; 1\le a,b\le n; a\ne b; -10^4\le c\le 10^4$.
  • Subtask 3 (50 points): $2\le n\le 5000; 1\le m\le 5000; 1\le a,b\le n; a\ne b; -10^4\le c\le 10^4$.

Examples

Input

3 3
1 2 3
2 3 -2
1 3 1

Output

0 -2 0

Explanation

The inequalities are x1−x2≤3x_1-x_2\le3, x2−x3≤−2x_2-x_3\le-2, and x1−x3≤1x_1-x_3\le1. The assignment (−2,−2,0)(-2,-2,0) satisfies them: respectively 0≤30\le3, −2≤−2-2\le-2, and −2≤1-2\le1. The answer file may contain another feasible assignment.