PAT a1122

 目的:判断是否是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;
}

反思:也许你提前知道了答案,但是不能提前交卷。就是说,也是结果早就出来了,但是你也得等它输入完全才可以。因为这里是多个查询。每个查询的数据不全部接收的话。后面的数据位置就错乱了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值