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

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

BestCoder Round #53 (div.2)

题目大意:就给你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;
}

  

时间: 2024-10-12 11:27:25

HDU 5424——Rikka with Graph II——————【哈密顿路径】的相关文章

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): 367    Accepted Submission(s): 90 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situatio

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条边,最后一条边的连接情况: (1)自环(这里不需要考虑): (2)最后一条边将首和尾连接,这样每个点的度都为2: (3)最后一条边将首和除尾之外的点连接或将尾和出尾之外的点连接,这样相应的首或尾的度最小,度为1: (4)最后一条边将首和尾除外的两个点连接,这样就有两个点的度最小,度都为1 如果所给的图是联通的话,那

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

HDU 5831 Rikka with Parenthesis II (六花与括号II) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Description 题目描述 As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math ta

hdu 5425 Rikka with Tree II(暴力)

题目链接:hdu 5425 Rikka with Tree II 直接枚举就好了,当概率极小时贡献值可以忽略. #include <cstdio> #include <cstring> #include <cmath> #include <vector> #include <queue> #include <algorithm> using namespace std; const int maxn = 1e5 + 5; int N,

HDU 5422:Rikka with Graph

Rikka with Graph Accepts: 353 Submissions: 1174 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的: 勇太有一张nn个点mm条边的无向图,每一条边的长度都是1.现在他想再在这张图上连上一条连接两个不同顶点边,使得1号点到nn号点的最短路尽可能的短

HDU 6090 Rikka with Graph —— 2017 Multi-University Training 5

Rikka with Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) 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

hdu 6090 Rikka with Graph

Rikka with Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 425    Accepted Submission(s): 270 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation,

hdu5424 Rikka with Graph II

给一个n个节点n条边的无向图G,试判断图中是否存在哈密顿路径. 若G中存在哈密顿路径l,则路径端点度数不小于1,其余点度数不小于2. 则G存在哈密顿路径的必要条件: 1)G连通: 2)G中度数为1的点不超过两个. 考虑到简单连通图中边的数目m不超过n, 1)若 m = n - 1,则可从任一度数为1的点搜索即可: 2)若 m = n,多余的一条边连接哈密顿路径上的两点,从任一度数为1的点搜索即可. 3)若不存在度数为1的点,从任一点开始搜索. 复杂度O(n). http://acm.hdu.ed

hdu 5422 Rikka with Graph(简单题)

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 m edges. The length of each edge is 1.