zoukankan      html  css  js  c++  java
  • PAT 1122 Hamiltonian Cycle[比较一般]

    1122 Hamiltonian Cycle (25 分)

    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<N200), the number of vertices, and M, the number of edges in an undirected graph. Then M lines 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:

    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

     题目大意:判断给出的路径是否是哈密顿回路,哈密顿回路是一个简单回路,包含图中的每一个点,

     我的AC:

    #include <iostream>
    #include <vector>
    #include<cstdio>
    #include <map>
    using namespace std;
    #define inf 9999
    
    int g[205][205];
    int vis[205];
    int main() {
        int n,m,f,t;
        cin>>n>>m;
        fill(g[0],g[0]+205*205,inf);
        for(int i=0;i<m;i++){
            cin>>f>>t;
            g[f][t]=1;
            g[t][f]=1;
        }
        int k,ct;
        cin>>k;
        while(k--){
            fill(vis,vis+205,0);
            cin>>ct;
            vector<int> path(ct);
            for(int i=0;i<ct;i++){
                cin>>path[i];
            }
            if(path[0]!=path[ct-1]){//首先需要保证两者是相同的。
                cout<<"NO
    ";continue;
            }
            bool flag=false;
            for(int i=0;i<ct-1;i++){
               if(g[path[i]][path[i+1]]==inf){//如果两点之间,没有路径。
                    cout<<"NO
    ";
                    flag=true;
                    break;
               }
               if(vis[path[i+1]]==1){//如果重复访问那么就不是简单路径,
                    cout<<"NO
    ";
                    flag=true;break;
               }
               vis[path[i+1]]=1;
              // cout<<path[i+1]<<'
    ';
            }
            if(!flag){//这里还需要判断是否是所有的点都已经访问过。
                bool fg=false;
                for(int i=1;i<=n;i++){//这里是从1开始判断啊喂!!!
                    if(vis[i]==0){
                        cout<<"NO
    ";
                        fg=true;break;
                    }
                }
                if(!fg)cout<<"YES
    ";
            }
        }
        return 0;
    }

    //本来很简单的一道题,两个周没打算法代码了,生疏了。

    1.点标号是从1开始的所以 最后判断所有的点是否被遍历过,是从1开始循环的,

     2.比较简单,就是几个判断情况,使用邻接矩阵存储图,不是邻接表。

  • 相关阅读:
    在CentOS7上搭建MySQL主从复制与读写分离
    数据库 引擎
    数据库 事务
    数据库 索引
    MYSQL
    基于mysqld_multi实现MySQL 5.7.24多实例多进程配置
    09 引导过程与故障修复
    chapter 8.3
    作业 8.1
    Chapter 04 作业
  • 原文地址:https://www.cnblogs.com/BlueBlueSea/p/9689706.html
Copyright © 2011-2022 走看看