hdu 5424 Rikka with Graph II 哈密顿通路
来源:互联网 发布:阿里云电子发票 编辑:程序博客网 时间:2024/05/21 21:48
Rikka with Graph II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 367 Accepted Submission(s): 90
Problem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:
Yuta has a non-direct graph withn vertices and n edges. Now he wants you to tell him if there exist a Hamiltonian path.
It is too difficult for Rikka. Can you help her?
Yuta has a non-direct graph with
It is too difficult for Rikka. Can you help her?
Input
There are no more than 100 testcases.
For each testcase, the first line contains a numbern(1≤n≤1000) .
Thenn lines follow. Each line contains two numbers u,v(1≤u,v≤n) , which means there is an edge between u and v .
For each testcase, the first line contains a number
Then
Output
For each testcase, if there exist a Hamiltonian path print "YES" , otherwise print "NO".
Sample Input
41 11 22 32 431 22 33 1
Sample Output
NOYESHintFor the second testcase, One of the path is 1->2->3If you doesn't know what is Hamiltonian path, click here (https://en.wikipedia.org/wiki/Hamiltonian_path).
Source
BestCoder Round #53 (div.2)
Recommend
hujie | We have carefully selected several similar problems for you: 5426 5425 5422 5421 5420
题意,给出一个n点n边的图,要求是否存在哈密顿通路。哈密顿通路:经过每个点有且仅有一次的一条通路。
由于图,只有n个边,那么最小度的那个边,最大只有2,先判定,是否连通,如果连通,每次,都找那个度最小的作为入点,因为,最小度的点,一定会成为某条路的端点,这样就可以用o(n )的复杂度,找到一条哈密顿回路。
给个测试数据,
5
1 5 1 2 2 3 2 4 1 3
#define N 1005#define M 100005#define maxn 205#define MOD 1000000000000000007int n,a,b,d[N],minx,mi;vector<int> p[N];bool vis[N];void DFS(int x){ vis[x] = true; FI(p[x].size()){ int goal = p[x][i]; if(!vis[goal]){ DFS(goal); } }}int main(){ //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); while(S(n)!=EOF) { FI(n+1) p[i].clear(); fill(d,0); FI(n){ S2(a,b); if(a != b){ p[a].push_back(b); p[b].push_back(a); d[a]++;d[b]++; } } bool isConnect = true; fill(vis,false); DFS(1); For(i,1,n+1){ if(!vis[i]){ isConnect = false; break; } } if(!isConnect){ printf("NO\n"); continue; } minx = N;mi = 1; For(i,1,n+1){ if(d[i] < minx){ mi = i; minx = d[i]; } } fill(vis,false); vis[mi] = true; while(true){ bool isChange = false; minx = N; int mit = -1; FI(p[mi].size()){ int goal = p[mi][i]; if(!vis[goal]){ if(d[goal] < minx){ mit = goal; minx = d[goal]; } isChange = true; } } if(!isChange) break; mi = mit; vis[mi] = true; } isConnect = true; For(i,1,n+1){ if(!vis[i]){ isConnect = false; break; } } if(!isConnect) printf("NO\n"); else printf("YES\n"); } //fclose(stdin); //fclose(stdout); return 0;}
0 0
- hdu 5424 Rikka with Graph II 哈密顿通路
- hdu 5424 Rikka with Graph II (哈密顿路判定)
- hdu 5424 Rikka with Graph II(哈密顿路判定)
- hdu 5424 Rikka with Graph II 判n点n边的图是否为哈密顿通路
- BestCoder Round #53 (div.2) HDOJ5424 Rikka with Graph II(dfs & 哈密顿通路判断)
- HDU 5424 Rikka with Graph II 哈密顿路径+dfs找环
- hdu 5424 Rikka with Graph II(欧拉通路)
- hdu5424 Rikka with Graph II(n个点n条边的图判哈密顿通路)
- BestCoder Round #53 (div.2)1003 Rikka with Graph II 哈密顿路径 dfs
- hdu 5424 Rikka with Graph II
- hdu 5424 Rikka with Graph II 搜索
- HDU5424Rikka with Graph II(哈密顿图判断)
- hdu 4856 Tunnels 哈密顿通路
- Rikka with Graph HDU
- hdu 5422 Rikka with Graph
- HDU 5422:Rikka with Graph
- hdu-5422 Rikka with Graph
- hdu 5422 Rikka with Graph
- strlen 转义字符
- 软考记录集(一)
- 除法优化
- 第八章 线性时间排序
- leetcode_019Remove Nth Node From End of List
- hdu 5424 Rikka with Graph II 哈密顿通路
- C 语言中32个关键字中几个特殊关键字
- js生成随机编号
- 如何搭建自定义php环境
- [c] poj1012 约瑟夫环
- winform,C#,打开文件对话框的使用
- pat(B)1007. 素数对猜想(素数打表)
- Note For Linux By Jes(17)-软件安装: RPM, SRPM 与 YUM 功能
- vim安装MatchTagAlways插件