麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > 綜合 > 正文

PAT-1122. Hamiltonian Cycle (25)

2024-07-21 02:52:17
字體:
來源:轉載
供稿:網友
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 ... Vnwhere 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:YESNONONOYES

NO

題意就是輸入一個圖 然后在輸入多行數據 每一行表示一個軌跡 需要分析這個軌跡是否構成哈密頓回路

哈密頓回路就是一條能夠串聯起圖中所有點的回路  如何判斷呢 一開始想復雜了 以為這個行序列里有多條起點和終點  需要把每一段起點終點相同的點有可能構成回路的線段都存到向量里 最后交了一發發現其實并沒有這么復雜  只需判斷這個行序列是不是第一個數等于最后一個數 并且這之間遍歷了所有的點 那么就符合YES條件 否則輸出NO

另外 一定要判斷這條軌跡中彼此連邊是否存在 不然還讓你輸入那么多邊干嘛?

AC code:

#include<set>#include<map>#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;struct node{	int s,e;	};int a[210][210];int tre[210];int last[210];int main(){	vector<node>v;	int n,m;	scanf("%d%d",&n,&m);	for(int i=1;i<=m;i++)	{		int s,e;		scanf("%d%d",&s,&e);		a[s][e]=1;		a[e][s]=1;	}		int k;	scanf("%d",&k);	while(k--)	{		int t;		scanf("%d",&t);		for(int i=1;i<=t;i++)scanf("%d",&tre[i]);			bool ver[210]={0};		int check=n;		bool flag=0;		for(int i=1;i<=t;i++)		{			if(i>1&&a[tre[i-1]][tre[i]]!=1)break;			if(!ver[tre[i]])			{				check--;				ver[tre[i]]=1;			}			else if(ver[tre[i]]&&check)break;			else if(check==0&&ver[tre[i]]&&tre[1]==tre[t]&&i==t)			{				flag=1;				break;			}		}		if(flag)printf("YES/n");		else printf("NO/n");	}	return 0;}


上一篇:sql語法 學習了解

下一篇:存儲過程簡介

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美精品欧美极品欧美激情 | 久久人体 | 日韩黄色av| 欧产日产国产精品v | 噜噜噜影院 | 国产精品视频六区 | 免费观看视频在线 | 国产亚洲欧美日韩在线观看不卡 | 久久久久久中文字幕 | 成人毛片在线免费观看 | 国产精品剧情一区二区在线观看 | 最新中文字幕第一页视频 | 亚洲精品久久久久久久久久 | 播色网| 中文字幕h | 91精品国产777在线观看 | 九草视频 | 亚洲成人福利网站 | 最新中文字幕第一页视频 | 中国女警察一级毛片视频 | 欧美成人免费电影 | 久久精品国产亚洲7777 | 国产成人精品免高潮在线观看 | 欧美成人黄色小视频 | 中文字幕在线永久 | 国产欧美亚洲精品a | 艹男人的日日夜夜 | 黄色av片在线观看 | 国产成人精品免费视频大全最热 | 欧美一级黄色免费 | 成人免费av在线播放 | 国产亚洲欧美日韩在线观看不卡 | 午夜精品久久久久久毛片 | 97中文 | 日本高清在线免费 | 国产精品久久999 | 黑人日比视频 | 国产精品成人久久久久a级 欧美特黄一级高清免费的香蕉 | 日本欧美一区二区 | 天堂亚洲一区 | 欧美成年人视频在线观看 |