#BF0000009. Mẫu kiểm tra chu trình âm (Negative Cycle Template)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 vertices and edge records. For a record with , create two directed arcs and , each of weight . For a negative , create only the directed arc .
Starting from vertex , 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 and records . Nonnegative records create two directed arcs; negative records create only .
Output
Print YES for a reachable negative cycle from 1, else NO.
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; .
- Subtask 3 (50 points): ; ; .
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.