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

#BF0000011. Nghịch lý Farnsworth (The Farnsworth Parabox)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Nghịch lý Farnsworth (The Farnsworth Parabox)

The Farnsworth Parabox

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Each box i,j,t moves forward by t from i to j, or backwards by t from j to i. Detect a time paradox reachable from universe 1.

There are nn parallel universes, with universe 11 being the original universe. A box (i,j,t)(i,j,t) permits forward travel i→ji\to j advancing time by tt units, and reverse travel j→ij\to i moving back in time by tt units. There is at most one box per unordered universe pair, but boxes may be reused.

The scientist starts in universe 11 and wants to know whether a closed trip can return to that universe earlier than departure. Individual boxes are not enough to decide this: a combination of boxes may form a negative-total-time cycle. If such a cycle is reachable from the original universe, a zero-time round trip to the cycle can be added to obtain a valid paradoxical itinerary.

Input

First line n,mn,m; then mm boxes i,j,ti,j,t, each unordered pair appearing at most once.

Output

Print Y if the paradox is possible, otherwise N.

Subtasks

  • Subtask 1 (20 points): 2≤n≤202\le n\le20; 0≤m≤500\le m\le50; 0≤t≤1000\le t\le100; 1≤i,j≤n1\le i,j\le n; no repeated unordered pairs.
  • Subtask 2 (30 points): 2≤n≤1002\le n\le100; 0≤m≤5000\le m\le500; 0≤t≤1000\le t\le100; 1≤i,j≤n1\le i,j\le n; no repeated unordered pairs.
  • Subtask 3 (50 points): 2≤n≤1002\le n\le100; 0≤m≤49500\le m\le4950; 0≤t≤1000\le t\le100; 1≤i,j≤n1\le i,j\le n; no repeated unordered pairs.

Examples

Input

3 3
1 2 3
1 3 2
2 3 4

Output

Y

Explanation

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