PAT 甲级 1122. Hamiltonian Cycle (25)
2017-10-20 04:47
441 查看
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 … 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
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 … 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
#include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; int g[10010][10010]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { g[i][j] = 0; } } for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); g[a][b] = 1; g[b][a] = 1; } int k; scanf("%d", &k); for (int i = 0; i < k; i++) { int vn; scanf("%d", &vn); int flag = 1; if (vn-1 != n) flag = 0; vector<int> v(vn); for (int j = 0; j < vn; j++) cin >> v[j]; if (v[0] != v[vn - 1]) { flag = 0; } else { for (int j = 0; j < vn - 1; j++) { if (!g[v[j]][v[j+1]]) { flag = 0; break; } } sort(v.begin(), v.end()-1); for (int j = 0; j < vn - 2; j++) { if (v[j] == v[j + 1]) { flag = 0; break; } } } if (flag)printf("YES\n"); else printf("NO\n"); } cin >> n; return 0; }
相关文章推荐
- PAT甲级 1122. Hamiltonian Cycle (25)
- 1122. Hamiltonian Cycle (25)-PAT甲级真题
- PAT 甲级 Highest Price in Supply Chain (25)
- PAT(甲级)1020. Tree Traversals (25)
- PAT甲级1036. Boys vs Girls (25)
- PAT甲级-1010. Radix (25)进制
- PAT 甲级 1006. Sign In and Sign Out (25)
- PAT 甲级 1016. Phone Bills (25)
- PAT甲级1009. Product of Polynomials (25)
- PAT 甲级 1039. Course List for Student (25)
- PAT 甲级 1066. Root of AVL Tree (25)
- PAT - 甲级 - 1007. Maximum Subsequence Sum (25)
- 【PAT甲级】【C++】1006. Sign In and Sign Out (25)
- 【PAT甲级】1040. Longest Symmetric String (25)
- PAT 甲级 1121. Damn Single (25)
- PAT - 甲级 - 1101. Quick Sort (25)(快速排序)
- pat甲级1003. Emergency (25)
- PAT - 甲级 - 1021. Deepest Root (25)(DFS图的连通分量,最大深度)
- pat甲级1013. Battle Over Cities (25)
- 1102. Invert a Binary Tree (25)-PAT甲级真题