zoukankan      html  css  js  c++  java
  • 1122 Hamiltonian Cycle (25 分)

    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<N≤200), 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:

    n V1 V2 … V**n

    where n is the number of vertices in the list, and V**i‘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
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    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:

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

    作者: CHEN, Yue

    单位: 浙江大学

    时间限制: 300 ms

    内存限制: 64 MB

    代码长度限制: 16 KB

    题目大意

    给出一个图,要求判断一个环是否是Hamiltonian cycle。题目对这个定义说得挺不清楚的。其实就是必须是简单环(除起点终点不能重复)而且图中所有点都要访问一次。

    代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    大专栏  1122 Hamiltonian Cycle (25 分)>54
    55
    56
    57
    58
    59
    60

    #include <vector>
    #include <stack>
    #include <algorithm>
    #include <deque>
    #include <queue>
    #include <map>
    #include <set>
    #include <cstring>
    #include <cmath>
    #include <sstream>

    using namespace std;
    int n,m,k;
    int visited[202];
    int g[202][202];

    int () {
    cin>>n>>m;
    int v1,v2;
    for(int i=0;i<m;i++){
    cin>>v1>>v2;
    g[v1][v2]=1;
    g[v2][v1]=1;
    }
    cin>>k;
    int c;
    for(int i=0;i<k;i++){
    cin>>c;
    int first,cur,pre,t;
    bool iscycle=true;
    fill(visited,visited+202,0);
    for(int j=0;j<c;j++){
    cin>>t;
    visited[t]++;
    if(j==0){
    first=t;
    pre=t;
    }
    else{
    cur=t;
    if(g[pre][cur]==0)
    iscycle=false;
    pre=cur;
    }
    }
    for(int i=1;i<=n;i++){
    if(i!=first&&visited[i]!=1)
    iscycle=false;
    if(i==first&&visited[i]!=2)
    iscycle=false;
    }

    if(cur!=first)
    iscycle=false;
    iscycle?cout<<"YES":cout<<"NO";
    cout<<endl;
    }
    return 0;
    }
  • 相关阅读:
    Selenium-Xpath使用方法
    HTML基础之js
    HTML基础—DOM操作
    CSS基础知识总结二
    CSS基础知识总结之css样式引用的三种方式
    BZOJ 2329/2209 [HNOI2011]括号修复 (splay)
    BZOJ 1576 [USACO]安全路经Travel (树剖+线段树)
    BZOJ 2402 陶陶的难题II (01分数规划+树剖+线段树+凸包+二分)
    BZOJ 4044 Virus synthesis (回文自动机+dp)
    BZOJ 2342 [SHOI2011]双倍回文 (回文自动机)
  • 原文地址:https://www.cnblogs.com/lijianming180/p/12041482.html
Copyright © 2011-2022 走看看