#BF0000010. Tìm chu trình âm (Cycle Finding)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 vertices and edges of integer weight, possibly negative. A cycle is a vertex sequence 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 , then directed edges .
Output
Print NO if absent; otherwise print YES and one negative cycle with matching first and last vertices.
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; .
- Subtask 3 (50 points): ; ; .
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 has total weight $1+1-3=-1`, so it is a valid answer.