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), 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 Kwhich is the number of queries, followed by K lines of queries, each in the format:

n V?1?? V?2?? ... 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:

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

就多判断几下了,感觉都不涉及图的知识
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int n, m, k, p;
 4 vector<int> vt;
 5 int v[500][500];
 6 int main(){
 7     cin >> n >> m;
 8     int x, y;
 9     for(int i = 0; i < m; i++){
10         cin >> x >> y;
11         v[x][y] = 1;
12         v[y][x] = 1;
13     }
14     cin >> k;
15     while(k--){
16         cin >> p;
17         vt.clear();
18         for(int i = 0; i < p; i++){
19             cin >> x;
20             vt.push_back(x);
21         }
22         set<int> s;
23         if(p == n+1){
24             if(vt[0] == vt[vt.size()-1]){
25                 bool flag = true;
26                 for(int i = 1; i < vt.size(); i++){
27                     if(v[vt[i-1]][vt[i]] == 0){
28                         flag = false;
29                         break;
30                     }
31                     s.insert(vt[i]);
32                 }
33                 if(flag && s.size() == n){
34                     cout << "YES" << endl;
35                 }else{
36                     cout <<"NO"<<endl;
37                 }
38             }else{
39                 cout << "NO" << endl;
40             }
41         }else{
42             cout << "NO" << endl;
43         }
44     }
45
46     return 0;
47 }


原文地址:https://www.cnblogs.com/zllwxm123/p/11326501.html

时间: 2024-10-07 11:12:47

1122 Hamiltonian Cycle (25 分)的相关文章

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 H

PAT 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 Specif

PAT甲级——A1122 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 Specif

hihoCoder #1087 Hamiltonian Cycle

Description Given a directed graph containing n vertice (numbered from 1 to n) and m edges. Can you tell us how many different Hamiltonian Cycles are there in this graph? A Hamiltonian Cycle is a cycle that starts from some vertex, visits each vertex

1150 Travelling Salesman Problem (25 分)

1150 Travelling Salesman Problem (25 分) The "travelling salesman problem" asks the following question: "Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city and retu

PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)

1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, y

【PAT甲级】1052 Linked List Sorting (25分)

1052 Linked List Sorting (25分) A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, yo

PAT 甲级 1106 Lowest Price in Supply Chain (25分) (bfs)

1106 Lowest Price in Supply Chain (25分)   A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer. Starting from one root supplier, everyone on the chain b

4-9 二叉树的遍历 (25分)

4-9 二叉树的遍历   (25分) 输出样例(对于图中给出的树): Inorder: D B E F A G H C I Preorder: A B D F E C G H I Postorder: D E F B H G I C A Levelorder: A B C D F G I E H 代码:(都是遍历的算法) 1 // 4-9 二叉树的遍历 2 // 3 // Created by Haoyu Guo on 04/02/2017. 4 // Copyright ? 2017 Haoy