并查集练习4:HDOJ1272

并查集的应用。

遇到了两个问题。

开始时用简单的并查集,判断是否有重复的线,即有相同父节点的点不可以再次连接。

这种想法未考虑全连接的条件。即出现(1,2,3)(4,5)虽无圈但是未全连接。

加上全连接的条件,即在满足无循环的情况下,点的个数等于连接+1。

并且要加上没有输入点的情况也输出yes。

几经周折,AC的比较不容易。

#include <iostream>

using namespace std;

int city[100005] ;
int weight[100005] ;
int used[100005] ;
int findroot(int a) ;
void unionroot(int a, int b) ;


int main()
{
    bool ans1 = true ;
    bool ans2 = true ;
    int a, b, roota, rootb, numcity, numlink ;
    while(true)
    {
        ans1 = true ;
        ans2 = true ;
        numcity = 0 ;
        numlink = 0 ;
        for(int i = 1 ; i<100005 ; i++)
        {
            city[i] = i ;
            weight[i] = 1 ;
            used[i] = 0 ;
        }
        while(cin >> a >> b)
        {
            if(a==0&&b==0)
                break ;
            if(a==-1&&b==-1)
                return 0 ;
            numlink++;
            used[a] = 1 ;
            used[b] = 1 ;
            roota = findroot(a) ;
            rootb = findroot(b) ;
            if(roota==rootb)
                ans1 = false ;
            else
                unionroot(roota, rootb) ;
        }
        for(int i = 1 ; i < 100005 ; i++)
            numcity+=used[i] ;
        if(numcity!=numlink+1)
            ans2 = false ;
        if((ans1==true&&ans2==true)||numcity==0)
            cout << "Yes" << endl ;
        else
            cout << "No" << endl ;
    }
    return 0;
}

int findroot(int a)
{
    while(a!=city[a])
        a = city[a] ;
    return a ;
}

void unionroot(int a, int b)
{
    if(weight[a]>weight[b])
    {
        city[b] = a ;
        weight[a] += weight[b] ;
    }
    else
    {
        city[a] = b ;
        weight[b] += weight[a] ;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值