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 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?
Input
There are no more than  testcases.
For each testcase, the first line contains a number n(≤n≤).
Then n lines follow. Each line contains two numbers u,v(≤u,v≤n) , which means there is an edge between u and v.
Output
For each testcase, if there exist a Hamiltonian path print "YES" , otherwise print "NO".
 
Sample Input

 
Sample Output
NO 
YES
Hint 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).
 
Source
 
 

给一个n条边,n个顶点的图,判定是否存在哈密顿路。

 

如果存在哈密顿路,此时路径中含有n-1条边,剩下的那一条要么是自环(这里不予考虑,因为哈密顿路必然不经过),要么连接任意两个点。不考虑自环,此时图中的点度数为1的个数必然不超过2个,有如下三种情况:

1、剩下的那条边连接起点和终点,此时所有点度数都是2,可以从任意一个顶点开始进行DFS,看能否找到哈密顿路

2、剩下的那条边连接除起点和终点外的任意两个点,此时起点和终点度数为1,任选1个开始进行DFS。

3、剩下的那条边连接起点和除终点的任意一个点,或者连接终点与除起点外的任意一个点,此时图中仅有1个点度数为1,从该点开始进行DFS即可。

 
 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int map[][];
int dgree[];
int vis[];
int n;
int f;
void dfs(int x)
{
vis[x]=;
for(int i=;i<=n;i++)
{
if(!vis[i] && map[x][i])
{
dfs(i);
vis[i]=;
}
}
}
void dfs1(int u,int num){
if(num==n){
f=;
return;
}
for(int i=;i<=n;i++){
if(!vis[i] && map[u][i]){
vis[i]=;
dfs1(i,num+);
if(f)
return;
vis[i]=;
}
}
}
int main()
{
int t; int x,y;
while(scanf("%d",&n)==)
{
memset(vis,,sizeof(vis));
memset(map,,sizeof(map));
memset(dgree,,sizeof(dgree));
for(int i=;i<n;i++)
{
scanf("%d%d",&x,&y);
map[x][y]=map[y][x]=;
++dgree[x];
++dgree[y];
}
dfs();
int flag=;
int count=;
for(int i=;i<=n;i++)
{
if(!vis[i])
{
flag=;
break;
}
if(dgree[i]==)
{
count++;
}
}
if(flag== || count>)
{
printf("NO\n");
continue;
}
if(count==)
{
printf("YES\n");
}
else {
f=;
for(int i=;i<=n;i++){
if(dgree[i]==){
memset(vis,,sizeof(vis));
vis[i]=;
dfs1(i,);
if(f)
break;
}
}
if(f) puts("YES");
else puts("NO");
} }
return ;
}
 

hdu 5424 Rikka with Graph II(dfs+哈密顿路径)的更多相关文章

  1. HDU 5424——Rikka with Graph II——————【哈密顿路径】

    Rikka with Graph II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  2. 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条边,最后一条边的 ...

  3. HDU 5424 Rikka with Graph II

    题目大意: 在 N 个点 N 条边组成的图中判断是否存在汉密尔顿路径. 思路:忽略重边与自回路,先判断是否连通,否则输出"NO",DFS搜索是否存在汉密尔顿路径. #include ...

  4. HDU 5831 Rikka with Parenthesis II(六花与括号II)

    31 Rikka with Parenthesis II (六花与括号II) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  5. HDU 5831 Rikka with Parenthesis II (栈+模拟)

    Rikka with Parenthesis II 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...

  6. hdu 5831 Rikka with Parenthesis II 线段树

    Rikka with Parenthesis II 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...

  7. HDU 5631 Rikka with Graph 暴力 并查集

    Rikka with Graph 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5631 Description As we know, Rikka ...

  8. HDU 5422 Rikka with Graph

    Rikka with Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. HDU 6090 Rikka with Graph

    Rikka with Graph 思路: 官方题解: 代码: #include<bits/stdc++.h> using namespace std; #define ll long lo ...

随机推荐

  1. TTTAttributedLabel使用介绍(转)

    TTTAttributedLabel 库地址 https://github.com/TTTAttributedLabel/TTTAttributedLabel 可以实现电话  地址  链接自动查找显示 ...

  2. 最长回文字符串(manacher算法)

    偶然看见了人家的博客发现这么一个问题,研究了一下午, 才发现其中的奥妙.Stupid. 题目描述:      回文串就是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串. ...

  3. PHP页面之间跳转方法总结

    编程中,在页面之间进行跳转是必须的.这里列出了三种办法,供参考. 一.用HTTP头信息 也就是用PHP的HEADER函数.PHP里的HEADER函数的作用就是向浏览器发出由HTTP协议规定的本来应该通 ...

  4. SUN-LDAP6.3_RHEL 5.0-卸载LDAP

    卸载LDAP 1.注销服务器 到目录/ldap/ldapinstance/dscc6/bin下 # ./dsccreg remove-server -h 主机名 /ldap/ldapinstance/ ...

  5. 多线程下载 HttpURLConnection

    Activity /**实际开发涉及文件上传.下载都不会自己写这些代码,一般会使用第三方库(如xUtils)或Android提供的DownloadManager下载*/ public class Ht ...

  6. 关于winform主题IrisSkin2的编写

    第一步:首先引用IrisSkin2.dll. 第二步自定义类: /// <summary> /// 窗体主题边界类 /// </summary> public class Fo ...

  7. iOS调节系统音量

    目录[-] 使用MPVolumeView 编程实现系统音量调节2 通过MPVolumeSlider的实例来操作系统音量 有问题!我不喜欢系统弹出音量提示 还有问题,我修改了系统音量但是不是通过我的UI ...

  8. Sql省市三级联动一张表

    CREATE TABLE [dbo].[region]( [region_id] [int] NULL, [region_name] [varchar](50) COLLATE Chinese_PRC ...

  9. uva 10929 - You can say 11

    #include <cstdio> using namespace std; ]; int main() { while(gets(in)) { ] == ] == ) break; ; ...

  10. Intellij idea配置springMvc4.2.6

    Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面. 环境: Intellij iead 2016.1 java version " ...