• PAT 1122 Hamiltonian Cycle


    1122 Hamiltonian Cycle

    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 (2Vertex1 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:

    1. 6 10
    2. 6 2
    3. 3 4
    4. 1 5
    5. 2 5
    6. 3 1
    7. 4 1
    8. 1 6
    9. 6 3
    10. 1 2
    11. 4 5
    12. 6
    13. 7 5 1 4 3 6 2 5
    14. 6 5 1 4 3 6 2
    15. 9 6 2 1 6 3 4 5 2 6
    16. 4 1 2 5 1
    17. 7 6 1 3 4 5 2 6
    18. 7 6 1 2 5 4 3 1

    Sample Output:

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

    总结:这道题目不难,对满足条件的路径输出YES,不满足的输出NO

    满足条件:

    ①:首尾两点相同

    ②:不能重复访问点,且该访问的点有通路

    ③:所有的点都要别访问过

    代码(不知道哪里错了,扣了一分):

    1. #include
    2. #include
    3. using namespace std;
    4. const int N=210;
    5. int g[N][N];
    6. int b[2*N];
    7. int t;
    8. bool st[N];//用于判断当前点是否被访问过
    9. void dfs(){
    10. if(b[0]!=b[t-1]){//首尾两点是否是相同的点
    11. puts("NO");
    12. return;
    13. }
    14. int pre=b[0],cur;
    15. st[0]=true;//因为不会遍历第0个节点,所以这里需要先将它赋值为true,
    16. //第一个节点会在遍历最后一个节点的时候赋值true
    17. for(int i=1;i
    18. cur=b[i];
    19. if(g[pre][cur]==1 && !st[cur]){//判断是否通路 和该点是否被访问过
    20. st[cur]=true;
    21. pre=cur;
    22. }
    23. else{
    24. puts("NO");
    25. return;
    26. }
    27. }
    28. bool sign=true;
    29. for(int i=0;iif(!st[i]) sign=false;//是否全部的点都已经访问过了
    30. if(sign) puts("YES");
    31. else puts("NO");
    32. }
    33. int main(){
    34. int n,m,k;
    35. scanf("%d%d",&n,&m);
    36. memset(g,-1,sizeof g);
    37. for(int i=0;i
    38. int a,b;
    39. scanf("%d%d",&a,&b);
    40. g[a][b]=g[b][a]=1;
    41. }
    42. scanf("%d",&k);
    43. for(int i=0;i
    44. scanf("%d",&t);
    45. memset(b,0,sizeof b);
    46. memset(st,false,sizeof st);
    47. for(int j=0;j
    48. scanf("%d",&b[j]);
    49. dfs();
    50. }
    51. return 0;
    52. }

     柳神代码:

    满足条件:集合中是否有点重复了,是否是k个点,首尾两点是否相同,路径是否有通路

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. int main() {
    6. int n, m, cnt, k, a[210][210] = {0};
    7. cin >> n >> m;
    8. for(int i = 0; i < m; i++) {
    9. int t1, t2;
    10. scanf("%d%d", &t1, &t2);
    11. a[t1][t2] = a[t2][t1] = 1;
    12. }
    13. cin >> cnt;
    14. while(cnt--) {
    15. cin >> k;
    16. vector<int> v(k);
    17. set<int> s;
    18. int flag1 = 1, flag2 = 1;
    19. for(int i = 0; i < k; i++) {
    20. scanf("%d", &v[i]);
    21. s.insert(v[i]);
    22. }
    23. if(s.size() != n || k - 1 != n || v[0] != v[k-1]) flag1 = 0;
    24. for(int i = 0; i < k - 1; i++)
    25. if(a[v[i]][v[i+1]] == 0) flag2 = 0;
    26. printf("%s",flag1 && flag2 ? "YES\n" : "NO\n");
    27. }
    28. return 0;
    29. }

    好好学习,天天向上!

    我要考研

  • 相关阅读:
    ElasticSearch 之 搜索辅助功能
    Protocol Buffers基本数据类型
    共谋韬略、共巢未来,电巢与韬略“战略合作签约仪式”圆满举办!
    外部 prometheus监控k8s集群资源
    基于SSM的乡镇篮球队管理系统设计与实现
    VMware Explore 大会发布重磅云上技术之外,VMware 有哪些前沿探索?
    唐老师讲电赛
    使用image-map编写校区平面示意图
    神经网络的英文缩写是啥,神经网络的英文是什么
    SpringSecurity 授权详解
  • 原文地址:https://blog.csdn.net/weixin_50679551/article/details/127451273