hdu 5424 Rikka with Graph II 搜索
来源:互联网 发布:db2删除数据库实例 编辑:程序博客网 时间:2024/06/06 13:06
Rikka with Graph II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 911 Accepted Submission(s): 224
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)
删除自环,如果自环个数>1无解,=1,判定是否联通即可。
=0,枚举每条边,删除,然后判定联通。
1号点用于判定联通,因为1可能不是路径的起点,所以要从两个方向搜索。
#include<iostream>#include<cstring>#include<algorithm>#include<cstdio>#include<vector>using namespace std;#define maxn 3001int size[maxn];int nv[maxn],nextt[maxn],id[maxn];int head[maxn];int check[maxn];void dfs(int u,int f,int xid){ if(check[u]) return ; //cout<<u<<"->"; check[u] = 1; size[u] = 1; int t= 0; for(int i = head[u];i != -1; i = nextt[i]){ if(id[i] == xid) continue; int v = nv[i]; if(check[v]) continue; dfs(v,u,xid); size[u]+=size[v]; if(u == 1 && t == 0){ t++; continue; } return ; }}int in[maxn];int main(){ int n,u,v; while(scanf("%d",&n)!=EOF){ memset(head,-1,sizeof(head)); memset(in,0,sizeof(in)); int cnt = 0,cycle=0; for(int i = 0;i < n; i++){ scanf("%d%d",&u,&v); if(u == v){ cycle++; continue; } nv[cnt] = u; nextt[cnt] = head[v]; id[cnt] = i; head[v] = cnt++; nv[cnt] = v; nextt[cnt] = head[u]; id[cnt] = i; head[u] = cnt++; if(u != v) in[u]++,in[v]++; } int flag = 0; if(cycle == 1){ memset(check,0,sizeof(check)); dfs(1,0,-1); if(size[1] == n) flag = 1; } else if(cycle == 0) for(int i = 0;i < n && flag==0; i++){ memset(check,0,sizeof(check)); dfs(1,0,i); //cout<<endl; if(size[1] == n) flag = 1; } if(flag)cout<<"YES"<<endl; else cout<<"NO"<<endl; } 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 哈密顿通路
- hdu 5424 Rikka with Graph II(哈密顿路判定)
- hdu 5424 Rikka with Graph II(欧拉通路)
- Rikka with Graph HDU
- HDU 5631 Rikka with Graph(无向图去边搜索)
- HDU 5424 Rikka with Graph II 哈密顿路径+dfs找环
- hdu 5422 Rikka with Graph
- HDU 5422:Rikka with Graph
- hdu-5422 Rikka with Graph
- hdu 5422 Rikka with Graph
- HDU 5422 Rikka with Graph
- HDU 5631 Rikka with Graph
- HDU 5631 Rikka with Graph
- HDU 5631 Rikka with Graph
- HDU 5631 Rikka with Graph
- IOS block编程指南 6 Block的使用
- 改变frame,放在哪里比较合适
- rust泛型过度使用思考
- Framework Design Guidelines(4.5)
- Android Activity生命周期以及onSaveInstanceState、onRestoreInstanceState要点备忘
- hdu 5424 Rikka with Graph II 搜索
- 《天龙八部3D》中Unity使用介绍
- GCD 概要
- 史上最详细的kettle初步小应用(猪都看得懂哦!!!)
- 浅谈 final, finally, finalize的区别
- iPad开发2
- Bootstrap学习:按钮
- oracle列转行
- SSH 整合报sessionfactory的错误以及解决方案