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. c# dictionary,list排序

    Dictionary Key排序 Dictionary<string, string> dct= new Dictionary<string, string>(); Dicti ...

  2. C#字符串要点(复习专用)

    一.字符串 通过string定义一个字符串,或者通过String类来创建对象. 通过new String() 创建有一下几种构造函数(从元数据),以此顺序创建string: // // 摘要: // ...

  3. Gazebo学习随记3 图形界面的使用

    直接写模型的SDF文件实在是太反人类啦! 可以在gazebo图形界面中设置好模型的链接(碰撞外观惯性),关节等等参数-然后生成SDF文件

  4. ubuntu - 14.04,安装Go语言(谷歌公司开发的一种语言)

    Go语言下载地址:https://storage.googleapis.com/golang/go1.5.1.linux-amd64.tar.gz 安装: 1,以root身份在shell里执行: ta ...

  5. NSString 字符串

    0.字符串常用操作 自动补充方法:当字符串长度不够需要自动补充到一定的位数 OC字符串与C语言字符串之间的相互转换 1.不可变字符串的创建 // 直接创建不可变字符串 /* 在 OC 中,使用 @&q ...

  6. 自定义Mybatis框架

    项目结构:      https://files-cdn.cnblogs.com/files/mkl7/ownMybatis.zip 1. 创建maven工程并引入坐标: <?xml versi ...

  7. 深入浅出git

    图文 http://www.cnblogs.com/syp172654682/p/7689328.html 廖雪峰 https://www.liaoxuefeng.com/wiki/001373951 ...

  8. 8、OpenCV Python 图像直方图

    __author__ = "WSX" import cv2 as cv import numpy as np from matplotlib import pyplot as pl ...

  9. [AH2017/HNOI2017]礼物(FFT)

    [Luogu3723] [DarkBZOJ4827] 题解 首先,有一个结论:两个手环增加非负整数亮度,等于其中一个增加一个整数亮度(可以为负) 设增加亮度为x.求\(\sum_{i=1}^{n}(a ...

  10. sharepoint_study_12

    描述:SharePoint新建Web应用程序时提示如下错误: 解决: 1. Go to IIS 2. Select the DefaultAppPool and Go to the Advanced ...