PAT 甲级 1122. Hamiltonian Cycle (25)
来源:互联网 发布:淘宝发空包平台 编辑:程序博客网 时间:2024/04/30 16:18
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
#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甲级 1122. Hamiltonian Cycle (25)
- PAT 甲级 1122. Hamiltonian Cycle (25)
- 1122. Hamiltonian Cycle (25)-PAT甲级真题
- PAT--1122. Hamiltonian Cycle (25)
- pat 1122. Hamiltonian Cycle (25)
- PAT-1122. Hamiltonian Cycle (25)
- 【PAT】1122. Hamiltonian Cycle
- PAT (Advanced Level) 1122. Hamiltonian Cycle (25)
- 【PAT】【Advanced Level】1122. Hamiltonian Cycle (25)
- PAT A1122 hamiltonian cycle (25)
- PAT A1122. Hamiltonian Cycle (25)
- 1122. Hamiltonian Cycle (25)
- 1122. Hamiltonian Cycle (25)
- 1122. Hamiltonian Cycle (25)
- 1122. Hamiltonian Cycle (25)
- 1122. Hamiltonian Cycle (25)
- 让Nginx支持php的PHP_INFO
- 1004. Counting Leaves (30)
- 字体图标的使用
- CentOS6.7源码安装vim-8.0
- (超级治愈!)关于创建String对象过程的内存分配
- PAT 甲级 1122. Hamiltonian Cycle (25)
- 杀死占用某端口的进程
- 哥德尔不完全性定理探索数学边界
- haxe 第二期 入门篇 (转载)
- Markdown介绍
- sequence alignment
- 解决近期Excel导入无法正常操作的问题
- linux下源码安装elasticsearch-5.5.2
- 各种智能产品,我想要一款智能保姆