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 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 K which 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

第一,遍历点K一定等于N+1,因为既要遍历且一次所有顶点,而且回到起点第二,遍历的最后一个点一定是起点使用visit记录顶点是否遍历过了,graph记录路径是否能行
 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 int main()
 5 {
 6     int n, m, k, a, b, start, graph[205][205];
 7     bool visit[205];//记录每个顶点遍历一次
 8     fill(graph[0], graph[0] + 25 * 205, -1);
 9     cin >> n >> m;
10     while (m--)
11     {
12         cin >> a >> b;
13         graph[a][b] = graph[b][a] = 1;
14     }
15     cin >> m;
16     while (m--)
17     {
18         cin >> k;
19         bool flag = true;
20         fill(visit, visit + 205, false);
21         for (int i = 0; i < k; ++i)
22         {
23             cin >> b;
24             if (flag == false || k != n + 1)//遍历所有的顶点并回到起点,则一定走过n+1个点
25             {
26                 flag = false;
27                 continue;
28             }
29             if (i == 0)
30                 start = b;//记录起点
31             else if (graph[a][b] != 1)//此路不通
32                 flag = false;
33             else if (i == k - 1 && b != start)//最后一个点不是起点
34                 flag = false;
35             else if (i != k - 1 && visit[b] != false)//除了最后一次重复遍历起点,出现了其他点重复遍历,
36                 flag = false;
37             else
38                 visit[b] = true;//遍历过
39             a = b;//记录前一个点
40         }
41         if (flag)
42         {
43             for (int i = 1; i <= n && flag == true; ++i)
44                 if (visit[i] == false)//存在没有遍历的顶点
45                     flag = false;
46             if (flag)
47                 cout << "YES" << endl;
48         }
49         if (flag == false)
50             cout << "NO" << endl;
51     }
52     return 0;
53 }
 

原文地址:https://www.cnblogs.com/zzw1024/p/11470328.html

时间: 2024-07-31 18:54:37

PAT甲级——A1122 Hamiltonian Cycle【25】的相关文章

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 H

【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全部出手的利润.输出最大利润. trick: 测试点2可能包含M不为整数的数据.(尽管题面说明M是正整数,可是根据从前PAT甲级题目的经验,有可能不是整数.....) 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using names

PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)

1016 Phone Bills (25 分)   A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connec

PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)

1024 Palindromic Number (25 分) A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers. Non-palindromic n

PAT 甲级 1028 List Sorting (25 分)(排序,简单题)

1028 List Sorting (25 分)   Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers N (≤) and C, wh

PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)

1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if

PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)

1063 Set Similarity (25 分)   Given two sets of integers, the similarity of the sets is defined to be /, where N?c?? is the number of distinct common numbers shared by the two sets, and N?t?? is the total number of distinct numbers in the two sets. Yo

PAT甲级——A1145 HashingAverageSearchTime【25】

The task of this problem is simple: insert a sequence of distinct positive integers into a hash table first. Then try to find another sequence of integer keys from the table and output the average search time (the number of comparisons made to find w

PAT甲级——A1146 TopologicalOrder【25】

This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topological order obtained from the given directed graph? Now you are supposed to write a program to test each of the options. Input Specification: Each in