#abc232c. C - Graph Isomorphism

C - Graph Isomorphism

Score : 300300 points

问题描述

高桥和青木各自拥有一个由 MM 根绳索连接 NN 个球的玩具。

在高桥的玩具中,球被编号为 1,,N1, \dots, N,第 ii 根绳索将球 AiA_iBiB_i 连接在一起。

类似地,在青木的玩具中,球也被编号为 1,,N1, \dots, N,第 ii 根绳索将球 CiC_iDiD_i 连接在一起。

在每个玩具中,没有绳索会将一个球与自身相连,也没有两个球会被两条或更多的不同绳索相连。

Snuke 正在思考这两个玩具是否具有相同的形状。
这里,若满足以下条件,则认为它们具有相同的形状:

  • 序列 PP(1,,N)(1, \dots, N) 的一个排列。
  • 对于每一对整数 i,ji, j11NN(包括两端点)之间,满足以下条件:
    • 高桥玩具中的球 iijj 若由一根绳索相连,则青木玩具中的球 PiP_iPjP_j 也必须由一根绳索相连。

如果两个玩具具有相同的形状,则输出 Yes;否则,输出 No

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

Problem Statement

Takahashi and Aoki each have a toy made by attaching MM cords to NN balls.

In Takahashi's toy, the balls are numbered 1,,N1, \dots, N, and the ii-th cord ties Ball AiA_i and BiB_i.

Similarly, in Aoki's toy, the balls are numbered 1,,N1, \dots, N, and the ii-th cord ties Ball CiC_i and DiD_i.

In each toy, no cord ties a ball to itself, and no two balls are tied by two or more different cords.

Snuke is wondering whether the two toys have the same shape.
Here, they are said to have the same shape when there is a sequence PP that satisfies the conditions below.

  • PP is a permutation of (1,,N)(1, \dots, N).
  • For every pair of integers i,ji, j between 11 and NN (inclusive), the following holds.
    • Balls ii and jj in Takahashi's toy are tied by a cord if and only if Balls PiP_i and PjP_j in Aoki's toy are tied by a cord.

If the two toys have the same shape, print Yes; otherwise, print No.

Constraints

  • 1N81 \leq N \leq 8
  • 0MN(N1)20 \leq M \leq \frac{N(N - 1)}{2}
  • 1Ai<BiN(1iM)1 \leq A_i \lt B_i \leq N \, (1 \leq i \leq M)
  • (Ai,Bi)(Aj,Bj)(ij)(A_i, B_i) \neq (A_j, B_j) \, (i \neq j)
  • 1Ci<DiN(1iM)1 \leq C_i \lt D_i \leq N \, (1 \leq i \leq M)
  • (Ci,Di)(Cj,Dj)(ij)(C_i, D_i) \neq (C_j, D_j) \, (i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM

A1A_1 B1B_1

\vdots

AMA_M BMB_M

C1C_1 D1D_1

\vdots

CMC_M DMD_M

Output

If the two toys have the same shape, print Yes; otherwise, print No.

Sample Input 1

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

Sample Output 1

Yes

Takahashi's toy is illustrated on the left in the figure below, and Aoki's is illustrated on the right.

yes1

The following figure shows that the two toys have the same shape. The condition in the statement is satisfied when P=(3,2,1,4)P = (3, 2, 1, 4), for example.

yes2

Sample Input 2

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

Sample Output 2

No

The two toys do not have the same shape.

no

Sample Input 3

8 0

Sample Output 3

Yes

update @ 2024/3/10 10:06:31