hdu5424 Rikka with Graph II(n个点n条边的图判哈密顿通路)

时间:2023-02-02 00:10:59


Link:http://acm.hdu.edu.cn/showproblem.php?pid=5424


Rikka with Graph II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 547    Accepted Submission(s): 134


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(1n1000) .

Then  n  lines follow. Each line contains two numbers  u,v(1u,vn)  , 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 BestCoder Round #53 (div.2)  
中文题意:

Rikka with Graph II

  Accepts: 44  Submissions: 586 Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/65536 K (Java/Others)
问题描述
众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的:

勇太有一张nn个点nn条边的无向图,现在他想要知道这张图是否存在一条哈密顿路径。

当然,这个问题对于萌萌哒六花来说实在是太难了,你可以帮帮她吗?
输入描述
数据组数不超过100组。每组数据的第一行一个整数n(1 \leq n \leq 1000)n(1n1000)

接下来nn行。每行两个整数u,v(1 \leq u,v \leq n)u,v(1u,vn),代表给定图上的一条边。
输出描述
对于每一组数据,如果图中存在一条哈密顿路径,输出"YES"否则输出"NO"。
输入样例
4
1 1
1 2
2 3
2 4
3
1 2
2 3
3 1
输出样例
NO
YES
Hint
第二组数据的一条哈密顿路径是1->2->3

如果你不知道哈密顿路径是什么,戳这里(https://en.wikipedia.org/wiki/Hamiltonian_path).

官方题解:

Rikka with Graph II

如果图是联通的,可以发现如果存在哈密顿路径,一定有一条哈密顿路径的一端是度数最小的点,从哪个点开始直接DFS搜索哈密顿路径复杂度是 O(n)O(n)的。要注意先判掉图不连通的情况。


编程思想(参考自:http://blog.csdn.net/u014679804/article/details/48092393):

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


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

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

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

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




AC code:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int n;
bool vis[1010];
int deg[1010];
int Map[1010][1010];
int fg;
void dfs(int u,int cnt)
{
vis[u]=true;
if(cnt==n)
{
fg=1;
return;
}
for(int i=1;i<=n&&!fg;i++)
{
if(!vis[i]&&Map[u][i])
{
dfs(i,cnt+1);
vis[i]=false;
}
}
}
int main(){
//freopen("in.txt","r",stdin);
int i,u,v;
while(scanf("%d",&n)!=EOF)
{
memset(deg,0,sizeof(deg));
memset(vis,0,sizeof(vis));
memset(Map,0,sizeof(Map));
for(i=1;i<=n;i++)
{
scanf("%d%d",&u,&v);
if(u!=v&&!Map[u][v])//排除自环和重边
{
deg[u]++;
deg[v]++;
Map[u][v]=Map[v][u]=1;
}
}
int tot=0;
int st=1;
for(i=1;i<=n;i++)
{
if(deg[i]==1)
{
tot++;
st=i;
}
}
if(tot>2)
puts("NO");
else
{
fg=0;
dfs(st,1);//从度数为1的点开始深搜
if(fg)
{
puts("YES");
}
else
{
puts("NO");
}
}
}
return 0;
}