P25407 [AtCoder] Graph Manipulation
提高-
题目描述

Problem Description

You are given a directed graph with $ N $ vertices and $ M $ edges. The vertices are numbered $ 1, 2, ..., N $, and the $ i $-th edge goes from vertex $ a_i $ to vertex $ b_i $ with a length of $ c_i $. This graph is strongly connected, meaning there is a path from vertex $ i $ to vertex $ j $ for all $ i, j $ ( $1 \leq i, j \leq N$ ).

You can choose one edge in this graph and change its length to any value you like. Note that you can also keep the original length.

Determine if it is possible to make the graph such that the length of any cycle is $ 0 $.

When selecting two or more edges from the graph (let's say $ M $ edges, $ d_1, d_2, ..., d_M $), the selected edges form a cycle if they satisfy the following conditions:

  • For $ i = 1, 2, ..., M-1 $, $ b_{d_i} = a_{d_{i+1}} $
  • $ a_{d_1} = b_{d_M} $ (corrected at 11:26)
  • If $i \neq j$, then $a_{d_i} \neq a_{d_j}$ (corrected at 11:22)

The length of a cycle is defined as the sum of the lengths of the selected edges.

Input Format

The input is given from the standard input in the following format:

$ N $ $ M $ $ a_1 $ $ b_1 $ $ c_1 $ $ a_2 $ $ b_2 $ $ c_2 $ $ : $ $ a_M $ $ b_M $ $ c_M $

Output Format

Output Yes if it is possible to make the graph such that the length of any cycle is $ 0 $, otherwise output No.

Sample Input and Output

Sample Input #1

3 3
1 2 1
2 3 2
3 1 3

Sample Output #1

Yes

Sample Input #2

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

Sample Output #2

No

Sample Input #3

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

Sample Output #3

Yes

Notes/Hints

Constraints

  • All inputs are integers.
  • $2 \leq N \leq 300$
  • $1 \leq M \leq N^2 - N$
  • $1 \leq a_i, b_i \leq N$
  • $|c_i| \leq 10^9$
  • $a_i \neq b_i$
  • For all $1 \leq i, j \leq M$, if $i \neq j$, then $a_i \neq a_j$ or $b_i \neq b_j$
  • The given graph is strongly connected, meaning there is a path from vertex $ i $ to vertex $ j $ for all $1 \leq i, j \leq N$.

Sample Explanation 1

You can reduce the length of any edge by $ 1 + 2 + 3 = 6 $.

Sample Explanation 3

You can set the length of the 1st edge to $ 0 $.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
3 3 1 2 1 2 3 2 3 1 3
样例输出
Yes