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

#BF0000009. Mẫu kiểm tra chu trình âm (Negative Cycle Template)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Mẫu kiểm tra chu trình âm (Negative Cycle Template)

Negative Cycle Template

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

Nonnegative edge records add two opposite arcs; negative records add one arc. Detect a negative cycle reachable from 1.

You are given nn vertices and mm edge records. For a record (u,v,w)(u,v,w) with w≥0w\ge0, create two directed arcs u→vu\to v and v→uv\to u, each of weight ww. For a negative ww, create only the directed arc u→vu\to v.

Starting from vertex 11, decide whether there is a reachable negative-weight cycle. Disconnected negative cycles do not affect the answer. Note that the number of edge records is not necessarily equal to the number of directed arcs after expansion.

Input

First line n,mn,m and mm records u,v,wu,v,w. Nonnegative records create two directed arcs; negative records create only u→vu\to v.

Output

Print YES for a reachable negative cycle from 1, else NO.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le20; 1≤m≤501\le m\le50; −10000≤w≤10000-10000\le w\le10000.
  • Subtask 2 (30 points): 1≤n≤1501\le n\le150; 1≤m≤5001\le m\le500; −10000≤w≤10000-10000\le w\le10000.
  • Subtask 3 (50 points): 1≤n≤20001\le n\le2000; 1≤m≤30001\le m\le3000; −10000≤w≤10000-10000\le w\le10000.

Examples

Input

3 3
1 2 3
2 3 4
3 1 -8

Output

YES

Explanation

Relaxation finds eligible paths, or a negative cycle in the required scope. The sample uses the exact output tokens specified by the problem.