1122. Hamiltonian Cycle (25)
来源:互联网 发布:wifi杀手软件下载 编辑:程序博客网 时间:2024/05/01 04:20
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 ... 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 106 23 41 52 53 14 11 66 31 24 567 5 1 4 3 6 2 56 5 1 4 3 6 29 6 2 1 6 3 4 5 2 64 1 2 5 17 6 1 3 4 5 2 67 6 1 2 5 4 3 1Sample Output:
YESNONONOYESNO
#include <stdio.h> #include <stdlib.h> #include <limits.h>#include <memory.h>#define MAX 210int table[MAX];int map[MAX][MAX];int main(){int N, M,n,i,j,flag;int start;char c;int x, y,k;//freopen("d:\\input.txt", "r", stdin);scanf("%d%d", &N, &M);for (i = 1; i <= N; i++){map[i][i] = 1;}for (i = 0; i < M; i++){scanf("%d%d", &x, &y);map[x][y] = 1;map[y][x] = 1;}scanf("%d", &k);for (i = 0; i < k; i++){flag = 0;memset(table, 0, sizeof(table));scanf("%d", &n);if (n < N){flag = 1;}scanf("%d", &x);start = x;for (j = 1; j < n; j++){scanf("%d", &y);table[y]++;if (map[x][y] != 1){flag = 1;}x = y;}if (x != start){flag = 1;}if (flag){puts("NO");continue;}for (j = 1; j <= N; j++){if (table[j] != 1){flag = 1;break;}}if (flag){puts("NO");continue;}puts("YES");}return 0;}
- 1122. Hamiltonian Cycle (25)
- 1122. Hamiltonian Cycle (25)
- 1122. Hamiltonian Cycle (25)
- 1122. Hamiltonian Cycle (25)
- 1122. Hamiltonian Cycle (25)
- 1122. Hamiltonian Cycle (25)
- 1122. Hamiltonian Cycle (25)
- 1122. Hamiltonian Cycle (25)
- 1122. Hamiltonian Cycle (25)
- 1122. Hamiltonian Cycle (25)
- PAT--1122. Hamiltonian Cycle (25)
- pat 1122. Hamiltonian Cycle (25)
- PAT-1122. Hamiltonian Cycle (25)
- PAT_A 1122. Hamiltonian Cycle (25)
- 1122. Hamiltonian Cycle (25)解题报告
- pat甲级 1122. Hamiltonian Cycle (25)
- 1122. Hamiltonian Cycle (25)[图的遍历]
- 1122. Hamiltonian Cycle (25) PAT 甲级
- 机器学习入门(11)--多特征量梯度下降算法
- POJ 3258 River Hopscotch 笔记
- Linux firmware
- Eclipse——提示
- 导入Maven pom.xml首行报错以及缺少api类jar包
- 1122. Hamiltonian Cycle (25)
- 机器学习入门(12)--线性回归之正规方程
- 第十六周5
- 第十六周OJ(3)学生信息排序
- poj 2002_Squares_hash
- 第十六周 项目1-(4)堆排序
- Codeforces Round #384 (Div. 2)(数学)
- 【读书笔记】循环引用(reference cycles)的问题
- 同步/异步 & 阻塞/非阻塞