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 ...
随机推荐
- 解决Eclipse 启动后总是Building WorkSpace(sleeping) Java报错和处理
发布者:Lynn.. 时间:2016-12-20 13:13:55 今天打开eclipse后eclipse总是在Building WorkSpace(sleeping),我的解决方案是 ...
- findControl 可以获取前台页面的控件
findControl 可以获取前台页面的控件
- PAT天梯赛L2-005 集合相似度
题目链接:点击打开链接 给定两个整数集合,它们的相似度定义为:Nc/Nt*100%.其中Nc是两个集合都有的不相等整数的个数,Nt是两个集合一共有的不相等整数的个数.你的任务就是计算任意一对给定集合的 ...
- Android点击事件(click button)的四种写法
在学习android开发和测试的时候发现不同的人对于click事件的写法是不一样的,上网查了一下,发现有四种写法,于是想比较一下四种方法的不同 第一种方法:匿名内部类 代码: package com. ...
- redis数据库介绍(NoSql数据库)
- 2015苏州大学ACM-ICPC集训队选拔赛(2)1004
草爷要的数 Problem Description 今天校队队员们准备放松一下,我们队就准备选一些数字玩,然而每个人喜欢的数字是不同的,刻盘喜欢x(1<=x<=1^9),凯凯喜欢y(1&l ...
- Vue全家桶了解一下(待补充)
vue全家桶了解一下 一.vue+vue-router+vuex+axios1.vue:使用vue-cli,生成最基本的vue项目2.vue-router:vue项目中的路由管理插件3.vuex:vu ...
- LINUX下用PHPIZE安装PHP GD扩展
环境:LNMP in centOS 6.4. linux下PHP的扩展可以用phpize的方法,比较简单地进行启用. 以下以PHP-GD2 库安装为例子. sudo yum install php-g ...
- 求用1g、2g、3g的砝码(每种砝码有无穷多个)称出10g的方案有几种
#include <iostream> using namespace std; // ; // sup是保存多项式的数组,sup[n]中的值代表指数为i的系数 ,下标i是x的指数 // ...
- zabbix监控nginx mysql 服务添加
[root@test2 ~]# rpm -ivh nginx-1.8.0-1.el7.ngx.x86_64.rpm [root@test2 ~]# cd /etc/nginx/ [root@test2 ...