PAT甲级 1122 - Hamiltonian Cycle

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Mr0cheng/article/details/79472642

注意题目的k的取值范围是不定的,所以在第二阶段的输入顶点序列不要用数组去存,直接用变量存取相应需要保存的值。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 200+10;
bool edge[maxn][maxn];
bool visited[maxn];
int main(){
    int n,m;
    scanf("%d %d",&n,&m);
    int u,v;
    for(int i=0;i<m;++i){
        scanf("%d %d",&u,&v);
        edge[u][v]=edge[v][u]=true;
    }
    int k,p,cnt,pre,first,last,temp;
    bool flag;
    scanf("%d",&k);
    while(k--){
        scanf("%d",&p);
        fill(visited+1,visited+min(p,maxn-2)+1,false);
        cnt=0;
        flag=true;
        for(int i=0;i<p;++i){
            scanf("%d",&temp);
            if(i==0)first=temp;
            if(i==p-1)last = temp;
            if(temp > n){
                flag = false;
                pre=temp;
                continue;
            }
            if(!visited[temp])++cnt;
            visited[temp]=true;
            if(i!=0){
                if(!edge[temp][pre])flag=false;
            }
            pre=temp;
        }
        if(p!=n+1 || first!=last || cnt!=n || !flag)printf("NO\n");
        else printf("YES\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Mr0cheng/article/details/79472642