国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

PAT甲級1122

2019-11-08 18:51:09
字體:
供稿:網(wǎng)友

1122. Hamiltonian Cycle (25)

時間限制300 ms內(nèi)存限制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 1Sample Output:
YESNONONOYESNO
#include<cstdio>#include<set>#include<algorithm>using namespace std;const int maxn = 210;const int INF = 1000000000;int a[maxn];int G[maxn][maxn];int main(){	fill(G[0], G[0] + maxn*maxn, INF);	int N, M;	scanf("%d %d", &N, &M);	int v1, v2;	for (int i = 0; i < M; i++)	{		scanf("%d %d", &v1, &v2);		G[v1][v2] = 1;		G[v2][v1] = 1;	}	int K;	scanf("%d", &K);	int n;	set<int> s; bool flag = false;	for (int i = 0; i < K; i++)	{		scanf("%d", &n);				for (int j = 0; j < n; j++)		{			scanf("%d", &a[j]);			s.insert(a[j]);		}		if (n != N + 1 || s.size() != N)		{			printf("NO/n");		}		else		{			int j = 0;			if (n == N + 1 && s.size() == N)			{				for (j = 0; j < n - 1; j++)				{					if (G[a[j]][a[j + 1]] == 1 && a[0] == a[n - 1])						continue;					else					{						printf("NO/n");						break;					}				}				if (j == n - 1)				{					printf("YES/n");				}			}		}		s.clear();	}	return 0;}
上一篇:gcc詳解

下一篇:微信上傳下載臨時素材

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 新竹市| 浠水县| 本溪| 延津县| 灵石县| 漳州市| 锦州市| 肥西县| 吴忠市| 都昌县| 筠连县| 田东县| 津南区| 靖宇县| 资溪县| 鄂尔多斯市| 北流市| 华坪县| 渑池县| 庆安县| 日喀则市| 双江| 资兴市| 云林县| 民勤县| 光山县| 宽城| 哈密市| 弋阳县| 隆安县| 自贡市| 尼勒克县| 祁阳县| 文成县| 巴林右旗| 鲁山县| 钟祥市| 望江县| 武穴市| 永新县| 宜州市|