HDOJ 5424 Rikka with Graph II 暴力dfs
来源:互联网 发布:华为云智软件 编辑:程序博客网 时间:2024/05/18 02:10
哈密顿路径本来是NPC的问题,不过这题可以特判后直接暴力dfs
Rikka with Graph II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1066 Accepted Submission(s): 271
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)
/* ***********************************************Author :CKbossCreated Time :2015年09月03日 星期四 08时33分46秒File Name :HDOJ5424.cpp************************************************ */#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>using namespace std;const int maxn=1100;int fa[maxn];int Find(int x){return (x==fa[x])?x:fa[x]=Find(fa[x]);}void Union(int u,int v){u=Find(u); v=Find(v);if(u==v) return ;fa[u]=v;}int n,du[maxn];bool vis[maxn];vector<int> G[maxn];bool flag;void dfs(int u,int c){if(flag) return ;if(c==n){flag=true; return ;}for(int i=0,sz=G[u].size();i<sz;i++){int v=G[u][i];if(vis[v]==true) continue;vis[v]=true;dfs(v,c+1);vis[v]=false;}}int main(){//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);while(scanf("%d",&n)!=EOF){memset(du,0,sizeof(du));memset(vis,0,sizeof(vis));for(int i=1;i<=n;i++) {G[i].clear(); fa[i]=i;}for(int i=0;i<n;i++){int u,v;scanf("%d%d",&u,&v);G[u].push_back(v); G[v].push_back(u);du[u]++; du[v]++;Union(u,v);}int st=1,g=Find(st);bool ok=true;for(int i=2;i<=n;i++){if(du[i]<du[st]) st=i;if(Find(i)!=g) ok=false;}if(ok==false) {puts("NO"); continue;}flag=false;vis[st]=true;dfs(st,1);if(flag) puts("YES");else puts("NO");} return 0;}
0 0
- HDOJ 5424 Rikka with Graph II 暴力dfs
- hdu 5424 Rikka with Graph II
- hdu 5424 Rikka with Graph II 搜索
- HDU 5424 Rikka with Graph II 哈密顿路径+dfs找环
- HDOJ 5631 Rikka with Graph
- HDOJ-5631 Rikka with Graph
- HDOJ 5631 Rikka with Graph
- hdu 5424 Rikka with Graph II (哈密顿路判定)
- hdu 5424 Rikka with Graph II 哈密顿通路
- hdu 5424 Rikka with Graph II(哈密顿路判定)
- hdu 5424 Rikka with Graph II(欧拉通路)
- HDOJ 5631Rikka with Graph(图论)
- BestCoder Round #53 (div.2)1003 Rikka with Graph II 哈密顿路径 dfs
- BestCoder Round #53 (div.2) HDOJ5424 Rikka with Graph II(dfs & 哈密顿通路判断)
- BestCoder Round #53 (div.2) C.Rikka with Graph II(dfs)
- HDOJ 5425 Rikka with Tree II
- HDOJ 5631 Rikka with Graph (删边判断连通性)
- HDOJ 5631 Rikka with Graph(并查集)
- Android的内存机制和常见泄漏情形
- Unable to execute dex: Multiple dex files define
- Kafka设计解析(三)- Kafka High Availability (下)
- 【OpenCV】SURF的使用
- 监控数据备份恢复完成进度(EXPDP/IMPDP/RMAN)
- HDOJ 5424 Rikka with Graph II 暴力dfs
- android 内存管理
- rman备份和expdp备份的区别
- sqlite3使用
- expdp\impdp及exp\imp
- 7.1Android服务绑定
- NOIP 2010 乌龟棋 DP (COGS 519)
- Kafka深度解析
- hdu 3480 Division 斜率优化