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

如果所给的图是联通的话,那么其度为1的点最多有2个,否则该图不连通

以度最小的点为起点进行DFS判断是否为哈密顿通路

哈密顿通路介绍:

https://en.wikipedia.org/wiki/Hamiltonian_path

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#define N 1010
#define INF 0x3f3f3f3f

using namespace std;

int n, G[N][N], du[N], f;
bool vis[N];

void Init()
{
    memset(G, 0, sizeof(G));
    memset(du, 0, sizeof(du));//du[i]记录点i的入度
    memset(vis, false, sizeof(vis));
}

void DFS(int u, int cnt)
{
    int i;
    vis[u] = true;
    if(cnt == n)//访问次数与点的个数相等,则每个点都访问到了,且每个点只访问了一次
    {
        f = 1;
        return ;
    }
    for(i = 1 ; i <= n && !f ; i++)
    {
        if(!vis[i] && G[u][i])
        {
            DFS(i, cnt + 1);
            vis[i] = false;
        }
    }
}

int main()
{
    int u, v, i;
    while(~scanf("%d", &n))
    {
        Init();
        for(i = 1 ; i <= n ; i++)
        {
            scanf("%d%d", &u, &v);
            if(u != v && !G[u][v])//去除自环和重边的情况
            {
                G[u][v] = G[v][u] = 1;
                ++du[u];
                ++du[v];
            }
        }
        int s = 1, num = 0;//s记录度最小的点
        for(i = 1 ; i <= n ; i++)
        {
            if(du[i] == 1)
            {
                num++;//统计度为1的点的个数
                s = i;
            }
        }
        if(num > 2)
        {
            printf("NO\n");//判断其不连通,则不是哈密顿通路
            continue;
        }
        f = 0;
        DFS(s, 1);//从度最小的开始搜
        if(f == 1)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

时间: 2024-10-22 14:07:10

hdu 5424 Rikka with Graph II (BestCoder Round #53 (div.2))(哈密顿通路判断)的相关文章

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——————【哈密顿路径】

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 situati

HDU 5651 xiaoxin juju needs help(BestCoder Round #77 (div.1)1001)

传送门 xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 861    Accepted Submission(s): 243 Problem Description As we all known, xiaoxin is a brilliant coder. He knew **palin

哈密顿图 BestCoder Round #53 (div.2) 1003 Rikka with Graph II

题目传送门 题意:判断是否为哈密顿图 分析:首先一种情况是不合法的:也就是度数为1的点超过2个:合法的有:,那么从度数为1的点开始深搜,如果存在一种走法能够走完n个点那么存在哈密顿路 收获:学习资料 代码: /************************************************ * Author :Running_Time * Created Time :2015-8-29 20:37:34 * File Name :C.cpp *******************

hdu 5204 Rikka with sequence(BestCoder Round #37)

Rikka with sequence                                                        Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 378    Accepted Submission(s): 75 Problem Description As we know, Rikk

HDU 5432 Rikka with Tree (BestCoder Round #53 (div.2))

题目大意:给你一个树 判断这棵树是否是独特的 一颗树是独特的条件:不存在一颗和它本身不同但相似的树 两颗树相似条件:两颗树中点的数量相等且相对应的点的深度相同 如第2个样例 4 1 2 2 3 1 4 与 4 1 2 1 4 3 4 如图:这两棵树的点的数量相等且相应的点的深度deep相同,所以这两棵树相似,所以样例2存在一颗树与它不同但相似,即不特殊 运用广搜统计每个点的深度 要想一颗树特殊,只有保证他的分支下面不再有子节点 #include<stdio.h> #include<str

BestCoder Round #53 (div.1)

Problem A: 题目大意: 给出以节点1为根的一棵树A,判断它是否是特殊的.一棵树是特殊的当且仅当不存在和它不完全相同的一棵树B,使得A中点i到点1的距离和B中相等. 题解: 假设一个点x的深度是d,它的父亲是y,如果存在一个深度为d-1的点z,那么把x从y下面移到z下面就可以得到树B了.所以求出每个深度的点的个数,只有当所有深度的点的个数都为1,最大深度的点的个数任意的时候 树是特殊的. Problem B: 题目大意: 给出N个点N条边的无向图判断是否存在哈密顿路. N<=1000.

BestCoder Round #53

现在博客更新比较少了,就当我还活着吧 Rikka with Graph 题目传送:HDU - 5422 - Rikka with Graph AC代码: #include <map> #include <set> #include <list> #include <cmath> #include <deque> #include <queue> #include <stack> #include <bitset>

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.