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

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 100 testcases.

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.

 
Output
For each testcase, if there exist a Hamiltonian path print "YES" , otherwise print "NO".
 
Sample Input
4
1 1
1 2
2 3
2 4
3
1 2
2 3
3 1
 
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条边的图,如果要形成哈密顿路径,则必然要用掉n-1条边,形成一条链,起点和终点的度为1,其余点的度为2。剩下的一条边,可能有下面的情况:
 
情况1:形成自环,自环对于哈密顿路径没影响,可以忽略。
情况2:形成重边,重边对于哈密顿路径也没影响,可以忽略。
情况3:起点或终点跟非终点或非起点连一条边,这时候从终点或起点dfs。
情况4:起点跟终点连边,所有点的度都为2,从任意点dfs。
 
吐糟:自己写的时候vector清空的时候放在了最后,因为中间有continue,结果就呵呵了。。。一直超时,纳闷死了。再者就是没有考虑清楚,开始写的时候没有把所有路径都走完,会漏掉情况,不该呀~~~
 
#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——————【哈密顿路径】的更多相关文章

  1. 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 ...

  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. pch文件的创建与配置

    1.ios中pch文件的创建与配置 1.1 ios中pch文件的创建与配置 1.2 全局宏定义标志的配置 2.宏定义 这里放的主要是开发中常用的宏定义. /** 动态的字符串格式化宏 */ #defi ...

  2. 167. Two Sum II - Input array is sorted两数之和

    1. 原始题目 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明 ...

  3. git 命令总结(转)

    结构图: <1> Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remote:远程仓库 一.新建代码库   # 在当前目录新建 ...

  4. 【转】在Asp.net前台和后台弹出提示框

    源地址:http://blog.sina.com.cn/s/blog_5200dd680100mkk0.html

  5. 洛谷 P3806 【模板】点分治1

    P3806 [模板]点分治1 题目背景 感谢hzwer的点分治互测. 题目描述 给定一棵有n个点的树 询问树上距离为k的点对是否存在. 输入输出格式 输入格式: n,m 接下来n-1条边a,b,c描述 ...

  6. 10.8 wtx模拟题题解

    填坑 orz w_x_c_q w_x_c_q的模拟赛(150pts,炸了) money 题目背景: 王小呆又陷入自己的梦里.(活在梦里...) 题目描述: 王小呆是一个有梦想的小菜鸡,那就是赚好多好多 ...

  7. asp.net mvc 中的 controller和asp.net web api 的apicontroller有什么区别?(转)

    本质上区别不大,一个返回html/text类型的response,一个返回json/text或者xml/text类型的response,对于api环境而言,apicontroller更智能一点,他可以 ...

  8. python依赖文件的生成requirement.txt

    移到你的环境中 pip freeze > requirement.txt

  9. nginx的配置文件(反向代理和主配置)

    配置文件保存,是为了工作方便.特别是优化好的配置.我们基本可以直接复制粘贴.这样做可以快速的完成手上的工作!! vim nginx.conf user nginx; worker_processes ...

  10. mpvue 小程序应用拖动排序

    <template> <div> <div style="width:90%;height:40px;line-height:40px;margin:auto; ...