HDU 5424——Rikka with Graph II——————【哈密顿路径】
Rikka with Graph II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1051 Accepted Submission(s): 266
Yuta has a non-direct graph with n 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?
For each testcase, the first line contains a number n(1≤n≤1000).
Then n lines follow. Each line contains two numbers u,v(1≤u,v≤n) , which means there is an edge between u and v.
For the second testcase, One of the path is 1->2->3
If you doesn't know what is Hamiltonian path, click here (https://en.wikipedia.org/wiki/Hamiltonian_path).
#include<bits/stdc++.h>
using namespace std;
const int maxn=2100;
const int INF=0x3f3f3f3f;
int degree[maxn];
int vis[maxn],gra[maxn][maxn];
vector<int>G[maxn];
int n;
bool dfs(int u,int fa,int cn){
if(cn==n){
return true;
}
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(vis[v]||v==fa){
continue;
}
vis[v]=1;
if(dfs(v,u,cn+1))
return true;
vis[v]=0; //如果没有这句,会过不了这个样例。5 2 3 2 4 4 1 1 2 5 4
}
return false;
}
void init(){//以后尽量放在前面情况,不装B
for(int i=0;i<=n+2;i++)
G[i].clear();
memset(degree,0,sizeof(degree));
memset(vis,0,sizeof(vis));
memset(gra,0,sizeof(gra));
}
int main(){
int a,b;
while(scanf("%d",&n)!=EOF){
init();
for(int i=0;i<n;i++){
scanf("%d%d",&a,&b);
if(gra[a][b]==1||a==b)
continue;
gra[a][b]=gra[b][a]=1;
G[a].push_back(b);
G[b].push_back(a);
degree[a]++,degree[b]++;
}
int deg1=0,idx=1;
for(int i=1;i<=n;i++){
if(degree[i]==1){
deg1++;
idx=i;
}
}
if(deg1>2){ //度为1的大于2个,必然不行
puts("NO");
continue;
}
vis[idx]=1;
if(dfs(idx,0,1))
puts("YES");
else puts("NO");
}
return 0;
}
HDU 5424——Rikka with Graph II——————【哈密顿路径】的更多相关文章
- hdu 5424 Rikka with Graph II(dfs+哈密顿路径)
Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so h ...
- hdu 5424 Rikka with Graph II (BestCoder Round #53 (div.2))(哈密顿通路判断)
http://acm.hdu.edu.cn/showproblem.php?pid=5424 哈密顿通路:联通的图,访问每个顶点的路径且只访问一次 n个点n条边 n个顶点有n - 1条边,最后一条边的 ...
- HDU 5424 Rikka with Graph II
题目大意: 在 N 个点 N 条边组成的图中判断是否存在汉密尔顿路径. 思路:忽略重边与自回路,先判断是否连通,否则输出"NO",DFS搜索是否存在汉密尔顿路径. #include ...
- HDU 5831 Rikka with Parenthesis II(六花与括号II)
31 Rikka with Parenthesis II (六花与括号II) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- HDU 5831 Rikka with Parenthesis II (栈+模拟)
Rikka with Parenthesis II 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...
- hdu 5831 Rikka with Parenthesis II 线段树
Rikka with Parenthesis II 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...
- HDU 5631 Rikka with Graph 暴力 并查集
Rikka with Graph 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5631 Description As we know, Rikka ...
- HDU 5422 Rikka with Graph
Rikka with Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 6090 Rikka with Graph
Rikka with Graph 思路: 官方题解: 代码: #include<bits/stdc++.h> using namespace std; #define ll long lo ...
随机推荐
- iOS Programming GitHub
我把学习<iOS编程(第4版)>的相关代码放在了GitHub上: https://github.com/palanceli/iOSProgramming 学了一段时间之后,当要用到某个知识 ...
- nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx//conf/nginx.conf:117
SSL相关的配置加到了nginx的配置文件中后,nginx竟然启动不起来了 于是用如下命令测试问题所在: /usr/local/nginx/sbin/nginx -c /usr/local/nginx ...
- day-15递归与函数
生成器send方法 send的工作原理 1.send发生信息给当前停止的yield 2.再去调用__next__()方法,生成器接着往下指向,返回下一个yield值并停止 # 案例: persons ...
- vm安装centos后unknown host问题和yum install安装不成功问题
网上差了很多说要在vi /etc/sysconfig/network新增GATEWAY=192.168.0.1 还有vi /etc/sysconfig/network-scripts/ifcfg-et ...
- MVC与三层的区别
闲来无事,想了想MVC与三层的区别,根据自己的经验,上图 由此来看,其实这两种框架(结构)的分层方式其实没什么联系,也没有什么可比性,但他们的目的都一样的:解耦 --Ones
- CF796C Bank Hacking 思维
Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search f ...
- html表单笔记
1.下面是 <form> 属性的列表: accept-charset 规定在被提交表单中使用的字符集(默认:页面字符集). action 规定向何处提交表单的地址(URL)(提交页面). ...
- clojure with postgres
主要关注访问pg.不关心其他db 1 clojure.java.jdbc https://github.com/clojure/java.jdbc http://clojure-doc.org/art ...
- visio 使用技巧汇总
1.visio中图形旋转任意角度的方法 视图----任务窗格----大小与位置----角度 2.箭头形状 更多形状---流程图----箭头形状 3.汇制虚线框 从基本形状中拖出一个矩形,右击矩形,选择 ...
- IE浏览器提示对象不支持“append”属性或方法
如下代码在IE浏览器中无法执行,提示对象不支持“append”属性或方法 var tImg = document.createElement("img"); tImg.setAtt ...