QOJ.ac

QOJ

実行時間制限: 2 s メモリ制限: 512 MB 満点: 100

#1339. 一致性交易

統計

贵公司正在开发一款电子游戏。在这个游戏中,玩家可以交换物品。交易遵循开发者设定的规则。规则定义格式如下:“玩家可以用 1 个物品 $A_i$ 交换 $x_i$ 个物品 $B_i$”。请注意,交易可以在两个方向上进行。物品在玩家和游戏系统之间进行交换。因此,玩家可以进行任意次数的交易。

有时,测试人员会发现一些漏洞,即重复执行特定的交易序列会导致物品数量无限增加。例如,以下规则集可能会导致此漏洞:

  • 玩家可以用 1 个物品 1 交换 2 个物品 2。
  • 玩家可以用 1 个物品 2 交换 2 个物品 3。
  • 玩家可以用 1 个物品 1 交换 3 个物品 3。

在这个规则集中,玩家可以无限增加物品数量。例如,玩家以 1 个物品 1 开始交易。使用规则 1 和 2,他们可以用它交换 4 个物品 3。然后,使用规则 3,他们可以得到 1 个物品 1 和 1 个物品 3。通过重复这个序列,物品 3 的数量会无限增加。

这些漏洞会破坏游戏平衡,因此开发者决定引入一个系统来防止不一致的交易。你的任务是编写一个程序,检测规则集是否包含此类漏洞。

输入格式

输入包含单个测试用例,格式如下:

第一行包含两个整数 $N$ 和 $M$,分别表示物品的种类数和规则数($1 \le N \le 10^5$,$1 \le M \le 10^5$)。接下来的 $M$ 行中,每一行给出一个交易规则,即 1 个物品 $A_i$ 和 $x_i$ 个物品 $B_i$ 可以双向交换($1 \le A_i, B_i \le N$,$1 \le x_i \le 10^9$)。不同物品之间不存在交换,即 $A_i \neq B_i$。

输出格式

如果没有漏洞(即重复任何交易序列都不会导致物品数量无限增加),输出“Yes”。否则,输出“No”。

样例

输入格式 1

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

输出格式 1

No

输入格式 2

4 3
1 2 7
2 3 5
4 1 2

输出格式 2

Yes

输入格式 3

4 4
1 2 101
2 3 99
1 4 100
4 3 100

输出格式 3

No

输入格式 4

5 6
3 1 4
2 3 4
5 4 15
2 1 16
2 4 20
5 3 3

输出格式 4

Yes

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.