我们活在这个世界上,没有一件事是虚空而生的,你站在光里背后就会有阴影,站在黑暗里心中也可能有光明,所以我们必须得承认,有一部分人或许丧失了初心,但也有一部分人用生命来践行诺言。
The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a graph. Such a cycle is called a "Hamiltonian cycle".
In this problem, you are supposed to tell if a given cycle is a Hamiltonian cycle.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive integers N (2<N≤200), the number of vertices, and M, the number of edges in an undirected graph. Then Mlines follow, each describes an edge in the format Vertex1 Vertex2
, where the vertices are numbered from 1 to N. The next line gives a positive integer K which is the number of queries, followed by K lines of queries, each in the format:
n V1 V2 ... Vn
where n is the number of vertices in the list, and Vi's are the vertices on a path.
Output Specification:
For each query, print in a line YES
if the path does form a Hamiltonian cycle, or NO
if not.
Sample Input:
6 10
6 2
3 4
1 5
2 5
3 1
4 1
1 6
6 3
1 2
4 5
6
7 5 1 4 3 6 2 5
6 5 1 4 3 6 2
9 6 2 1 6 3 4 5 2 6
4 1 2 5 1
7 6 1 3 4 5 2 6
7 6 1 2 5 4 3 1
Sample Output:
YES
NO
NO
NO
YES
NO
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#include <numeric>
#include<unordered_set>
#include <climits>//INT_maxn
#define PP pair<ll,int>
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fll
#define dinf 1000000000000.0
#define PI 3.1415926
//#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=5e4+10;
const int maxn=210;
int n,m,k,mp[210][210];
set<int> s;
int main(){
cin>>n>>m;
for (int i=0; i<m; i++){
int t1, t2;
scanf("%d%d", &t1, &t2);
mp[t1][t2]=mp[t2][t1]=1;
}
cin>>k;
for (int i=0; i<k; i++) {
int ls,st;
bool fg=0;
scanf("%d", &ls);
s.clear();
for (int j=0; j<ls; j++) {
int v[1010],t3;
scanf("%d", &t3);
v[j]=t3;
if (j==0)
st=t3;
else{
if(j!=ls-1){
if(s.find(t3)!=s.end()&&fg==0) {
fg=1;
printf("NO\n");
}
}
else{
if (t3!=st&&fg==0){
fg=1;
printf("NO\n");
}
}
if(mp[v[j-1]][v[j]]==0&&fg==0) {
fg=1;
printf("NO\n");
}
}
s.insert(t3);
}
if(ls!=n+1&&fg==0)
printf("NO\n");
else if(fg==0&&ls==n+1)
printf("YES\n");
}
return 0;
}