#P11640. Graph

Graph

Background

Hack testdata has been added in Subtask #5 and is not scored.

Problem Description

There is a graph with nn vertices. Each vertex can be black or white.

There are mm constraints. The ii-th constraint gives ai,bi,cia_i, b_i, c_i, meaning that from aia_i to bib_i there must exist a path of length cic_i. The path may pass through an edge or a vertex multiple times.

Determine whether there exists a directed graph whose edges all have weight 11, such that:

  • All the above mm constraints are satisfied.
  • If the graph has kk edges, then for every 1ik\forall 1 \le i \le k, let the ii-th edge be directed from uiu_i to viv_i. Then the colors of uiu_i and viv_i are different.

Input Format

The first line contains an integer TT, the number of test cases.

For each test case, the first line contains two integers n,mn, m.

The next mm lines each contain three integers ai,bi,cia_i, b_i, c_i.

Output Format

Output TT lines. Each line contains a string s{Yes,No}s \in \{\tt{Yes}, \tt{No}\}.

The ii-th line is the answer to the ii-th test case.

1
5 4
1 3 4
4 2 7
4 4 0
5 2 1
Yes

Hint

[Sample Explanation]

You can construct

to satisfy the requirements.


[Constraints]

This problem uses bundled tests.

  • Subtask #1 (5pts5\text{pts}): m=0m = 0.
  • Subtask #2 (20pts20\text{pts}): n10n \le 10.
  • Subtask #3 (25pts25\text{pts}): n103n \le 10^3.
  • Subtask #4 (50pts50\text{pts}): no special constraints.

For 100%100\% of the data, 1T101 \le T \le 10, 1n1061 \le n \le 10^6, 0m1060 \le m \le 10^6, 1ai,bin1 \le a_i, b_i \le n, 0ci1090 \le c_i \le 10^9.

Translated by ChatGPT 5