1122. Hamiltonian Cycle (25)

来源:互联网 发布:wifi杀手软件下载 编辑:程序博客网 时间:2024/05/01 04:20

1122. Hamiltonian Cycle (25)

时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 1
Sample 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;}


0 0
原创粉丝点击