#P3209. [HNOI2010] 平面图判定

[HNOI2010] 平面图判定

Description

If an undirected graph G=(V,E)G=(V, E) can be drawn in the plane such that any two edges that do not share an endpoint do not intersect, then GG is called a planar graph. Deciding whether a graph is planar is an important problem in graph theory. Now suppose you are to decide this for a special class of graphs that contain a cycle passing through all vertices, i.e., there exists a Hamiltonian cycle.

Input Format

The first line of the input file contains a positive integer TT, indicating the number of test cases (each test case describes one graph to be decided).

Starting from the second line, there are TT test cases. For each test case, the first line contains two positive integers NN and MM separated by a space, representing the number of vertices and the number of edges of the corresponding graph, respectively.

The next MM lines each contain two positive integers uu and vv (1u,vN)\left(1\leq u,v\leq N\right) separated by a space, representing an edge (u,v)\left(u,v\right) of the graph. The input guarantees that each edge appears exactly once.

The last line of each test case contains NN integers, from left to right representing a Hamiltonian cycle of the corresponding graph: V1,V2,,VNV_1,V_2,\dots,V_N, i.e., for any iji\not=j we have ViVjV_i\not=V_j, and for any 1iN11\leq i\leq N-1 we have (Vi,Vi+1)E\left(V_i,V_{i+1}\right)\in E and (V1,VN)E\left(V_1,V_N\right)\in E.

The input guarantees that 100%100\% of the testdata satisfy T100T\leq 100, 3N2003\leq N\leq 200, M10000M\leq 10000.

Output Format

Output TT lines. If the graph in the ii-th test case is planar, print YES on the ii-th line; otherwise, print NO. Note that they must be uppercase letters.

2
6 9
1 4
1 5
1 6
2 4
2 5
2 6
3 4
3 5
3 6
1 4 2 5 3 6
5 5
1 2
2 3
3 4
4 5
5 1
1 2 3 4 5
NO
YES

Hint

Thanks to @hibiki for correcting the problem.

Thanks to @Anguei for providing the LaTeX problem statement.

Translated by ChatGPT 5