#abc306g. G - Return to 1

G - Return to 1

Score : 600600 points

问题描述

我们有一个有向图,包含 NN 个顶点和 MM 条边。顶点从 11NN 编号,第 ii 条边从顶点 UiU_i 指向顶点 ViV_i

你现在位于顶点 11。判断你是否能够执行以下操作 101010010^{10^{100}} 次,并最终回到顶点 11

  • 从当前所在的顶点选择一条出发的边,并沿着这条边移动到它所指向的顶点。

给定 TT 组测试用例,请分别解决每组问题。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

We have a directed graph with NN vertices and MM edges. The vertices are numbered from 11 through NN, and the ii-th edge goes from vertex UiU_i to vertex ViV_i.

You are currently at vertex 11. Determine if you can make the following move 101010010^{10^{100}} times to end up at vertex 11:

  • choose an edge going from the vertex you are currently at, and move to the vertex that the edge points at.

Given TT test cases, solve each of them.

Constraints

  • All input values are integers.
  • 1T2×1051\leq T \leq 2\times 10^5
  • 2N2×1052\leq N \leq 2\times 10^5
  • 1M2×1051\leq M \leq 2\times 10^5
  • The sum of NN over all test cases is at most 2×1052 \times 10^5.
  • The sum of MM over all test cases is at most 2×1052 \times 10^5.
  • 1Ui,ViN1 \leq U_i, V_i \leq N
  • UiViU_i \neq V_i
  • If iji\neq j, then (Ui,Vi)(Uj,Vj)(U_i,V_i) \neq (U_j,V_j).

Input

The input is given from Standard Input in the following format. Here, testi\text{test}_i denotes the ii-th test case.

TT

test1\text{test}_1

test2\text{test}_2

\vdots

testT\text{test}_T

Each test case is given in the following format.

NN MM

U1U_1 V1V_1

U2U_2 V2V_2

\vdots

UMU_M VMV_M

Output

Print TT lines.

The ii-th (1iT)(1\leq i \leq T) line should contain Yes if you can make the move described in the problem statement 101010010^{10^{100}} times to end up at vertex 11, and No otherwise.

Sample Input 1

4
2 2
1 2
2 1
3 3
1 2
2 3
3 1
7 10
1 6
6 3
1 4
5 1
7 1
4 5
2 1
4 7
2 7
4 3
7 11
1 6
6 3
1 4
5 1
7 1
4 5
2 1
4 7
2 7
4 3
3 7

Sample Output 1

Yes
No
No
Yes

For the 11-st test case,

  • You inevitably repeat visiting vertices 1211 \rightarrow 2 \rightarrow 1 \rightarrow \dots. Thus, you will end up at vertex 11 after making the move 101010010^{10^{100}} times, so the answer is Yes.

For the 22-nd test case,

  • You inevitably repeat visiting vertices $1 \rightarrow 2 \rightarrow 3 \rightarrow 1 \rightarrow \dots$. Thus, you will end up at vertex 22 after making the move 101010010^{10^{100}} times, so the answer is No.

update @ 2024/3/10 08:39:51