hdu 5424 Rikka with Graph II 搜索

文章探讨了如何通过自定义输入的数据结构来判断一个无向图中是否存在汉密尔顿路径,并提供了算法实现和实例解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Rikka with Graph II

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


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

删除自环,如果自环个数>1无解,=1,判定是否联通即可。

=0,枚举每条边,删除,然后判定联通。

1号点用于判定联通,因为1可能不是路径的起点,所以要从两个方向搜索。



#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
using namespace std;
#define maxn 3001
int size[maxn];
int nv[maxn],nextt[maxn],id[maxn];
int head[maxn];
int check[maxn];
void dfs(int u,int f,int xid){
    if(check[u]) return ;
    //cout<<u<<"->";
    check[u] = 1;
    size[u] = 1;
    int t= 0;
    for(int i = head[u];i != -1; i = nextt[i]){
        if(id[i] == xid) continue;
        int v = nv[i];
        if(check[v]) continue;
        dfs(v,u,xid);
        size[u]+=size[v];
        if(u == 1 && t == 0){
            t++;
            continue;
        }
            return ;
    }
}
int in[maxn];

int main(){
    int n,u,v;
    while(scanf("%d",&n)!=EOF){
        memset(head,-1,sizeof(head));
        memset(in,0,sizeof(in));
        int cnt = 0,cycle=0;
        for(int i = 0;i < n; i++){
            scanf("%d%d",&u,&v);
            if(u == v){
                cycle++;
                continue;
            }
            nv[cnt] = u;
            nextt[cnt] = head[v];
            id[cnt] = i;
            head[v] = cnt++;

            nv[cnt] = v;
            nextt[cnt] = head[u];
            id[cnt] = i;
            head[u] = cnt++;
            if(u != v)
                in[u]++,in[v]++;
        }
        int flag = 0;
        if(cycle == 1){
            memset(check,0,sizeof(check));
            dfs(1,0,-1);
            if(size[1] == n) flag = 1;
        }
        else if(cycle == 0)
            for(int i = 0;i < n && flag==0; i++){
                memset(check,0,sizeof(check));

                dfs(1,0,i);
                //cout<<endl;
                if(size[1] == n) flag = 1;
            }

        if(flag)cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值