目的:判断是否是Hamiltonian Cycle
输入:
N 节点数,1~N
M 边数
K 查询
#include<iostream>
#include<stdio.h>
#include<set>
#include<unordered_map>
#include<vector>
using namespace std;
const int maxn = 1000;
unordered_map<int,bool> hash1;
int N,M,K,n;
int main()
{
scanf("%d%d",&N,&M);
for(int i=0;i<M;i++)
{
int u,v;
scanf("%d%d",&u,&v);
hash1[u*maxn+v] = true;
hash1[v*maxn+u] = true;
}
scanf("%d",&K);
for(int i=0;i<K;i++)
{
scanf("%d",&n);
int first,pre,now;
scanf("%d",&first);
now = first;
set<int> temp;
temp.clear();
temp.insert(first);
bool flag = true;
for(int j=1;j<n;j++)
{
pre = now;
scanf("%d",&now);
temp.insert(now);
if(hash1.find(pre*maxn+now)==hash1.end())
{
flag = false;
}
}
if(flag==true&&(n==N+1)&&(temp.size()==N)&&first==now)
{
printf("YES\n");
}else
{
printf("NO\n");
}
}
return 0;
}
反思:也许你提前知道了答案,但是不能提前交卷。就是说,也是结果早就出来了,但是你也得等它输入完全才可以。因为这里是多个查询。每个查询的数据不全部接收的话。后面的数据位置就错乱了。