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

#BF0000010. Tìm chu trình âm (Cycle Finding)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tìm chu trình âm (Cycle Finding)

Cycle Finding

Source: CSES

Version: Phuoc Hung OJ Extended

Problem Statement

Find any negative-weight directed cycle in any connected component and output its vertices in order.

A directed graph contains nn vertices and mm edges of integer weight, possibly negative. A cycle is a vertex sequence v1,v2,…,vk,v1v_1,v_2,\ldots,v_k,v_1 following directed edges in order and ending where it started.

Find any negative-weight cycle anywhere in the graph. Print NO if none exists. Otherwise, print YES followed by a valid vertex sequence forming a cycle of negative total weight. Multiple different cycles may all be accepted; it is not necessary to minimize the number of edges or total cycle weight.

Input

First line n,mn,m, then mm directed edges u,v,wu,v,w.

Output

Print NO if absent; otherwise print YES and one negative cycle with matching first and last vertices.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le20; 1≤m≤501\le m\le50; −1000000000≤w≤1000000000-1000000000\le w\le1000000000.
  • Subtask 2 (30 points): 1≤n≤1501\le n\le150; 1≤m≤5001\le m\le500; −1000000000≤w≤1000000000-1000000000\le w\le1000000000.
  • Subtask 3 (50 points): 1≤n≤25001\le n\le2500; 1≤m≤50001\le m\le5000; −1000000000≤w≤1000000000-1000000000\le w\le1000000000.

Examples

Input

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

Output

YES
1 2 4 1

Explanation

Cycle 1→2→4→11\to2\to4\to1 has total weight $1+1-3=-1`, so it is a valid answer.