ZOJ1740(Message System)用并查集

Message System

Time Limit: 1 Second      Memory Limit: 32768 KB

Little Kawaii has built a simple message system to process the Academy Cute Message(ACM). The message system consists of many endpoints, which are connected by message channels. The messages can only pass across the channels, and will be originated at any endpoint. When receiving or originating a message, each endpoint will process the message then send the message to the channels that it connects to, except the one from which the message comes. In case the endpoint is the originator, the message will be send to all the channels it connects to. Academy Cute Message is very important, so if any message is generated, it requires that all endpoints will receive it properly. However, processing the message requires tremendous resources, so Little Kawaii hopes that the endpoints will not receive duplicated messages. Given the description of the message system, Little Kawaii asks you to write a program to determine whether the system could fulfill the requirements.

Input

The input consists of several test cases, separated by a blank line. Each case starts with 2 integer N and M, separated by white spaces. N represents the number of endpoints in the system, 1 <= N <= 1000; 0 <= M <= N * (N - 1) / 2. Then M lines of input follows, each consists of 2 integer A and B, separated by white spaces, 1 <= A, B <= N, meaning there is a message channel between endpoint A and B. There will be at most one channel between two endpoints, and there is no channel connects an endpoint to itself. A test case starting with two 0(zero) signals the end of the input, you should not process it.

Output

For each test case, output "Yes" in a single line, if the message system fulfills Little Kawaii's requirements, otherwise output "No" in a single line.

Sample Input
4 3
1 2
2 3
3 4
3 1
2 3
0 0
Sample Output
Yes
No
用并查集做的时候需判断二个问题:
1.是否连通
2.是否有回路
第一个问题好解决,因为并查集本来就有“查”这一特性,
第二个问题的解决之道在于并得时候查找合并的二个集合是否属于同一个集合,若是,则说明有回路。
这是我用第一种并查集做的,其效率“并”是,“查”是
源代码:
 
 
ExpandedBlockStart.gif ContractedBlock.gif /**/ /*Message is very important, so if any message is generated, 
it requires that all endpoints will receive it properly. However, 
processing the message requires tremendous resources, 
so Little Kawaii hopes that the endpoints will not receive duplicated messages.
*/

#include 
<iostream>
using namespace std;

const int MAX = 1003;

int cnt[MAX];
int n, m;

int findRoot(int x)
ExpandedBlockStart.gifContractedBlock.gif
{
    
return cnt[x];
}


void Merge(int a, int b)
ExpandedBlockStart.gifContractedBlock.gif
{
    
int i, j, k;
    i 
= cnt[a] < cnt[b]? cnt[a] : cnt[b];
    j 
= cnt[a] > cnt[b]? cnt[a] : cnt[b];
    
for(k = 1; k <= n; k++)
        
if(cnt[k] == j)
            cnt[k] 
= i;
}


int main()
ExpandedBlockStart.gifContractedBlock.gif
{

    
int i, a, b, ra, rb;
    
bool flag ;//判断有无回路
    while(cin>>n>>m)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if(!&& !m)
            
break;
        
for(i = 1; i <= n; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            cnt[i] 
= i;
        }

        flag 
= true;
        
for(i = 1; i <= m; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            scanf(
"%d%d",&a,&b);
            
if(flag)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                ra 
= findRoot(a);
                rb 
= findRoot(b);
                
if(ra == rb)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    flag 
= false;
                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    Merge(a, b);
                }

            }

        }

        
if(flag)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
for(i = 2; i <= n; i++)//判断是否连通
                if(cnt[i] != cnt[1])
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    flag 
= false;
                    
break;
                }

        }

        
if(flag)
            cout
<<"Yes"<<endl;
        
else
            cout
<<"No"<<endl;
    }

    
return 0;
}
然后从学长那边找到一种效率更高的并查集实现方法,代码如下:
 
 
#include<stdio.h>
int un(int a[],int l)
ExpandedBlockStart.gifContractedBlock.gif
{
    
if(a[l]==l)return l;
    
else return un(a,a[l]);
}

int main()
ExpandedBlockStart.gifContractedBlock.gif
{
    
int n,m,i,x,y,k,p,a[1005],flag;
    
while(scanf("%d %d",&n,&m)!=EOF)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if(n==0&&m==0)break;
        
for(i=0;i<=n;i++)a[i]=i;
        flag
=1;
        
for(i=1;i<=m;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            scanf(
"%d %d",&x,&y);
            k
=un(a,x);
            p
=un(a,y);
            
if(flag)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if(k==p)flag=0;
                
else if(p==y&&k!=x)a[p]=x;
                
else if(k==x&&p!=y)a[x]=y;
                
else if(k!=x&&p!=y)a[k]=y;
                
else if(x==k&&p==y)a[y]=x;
            }

        }

        
if(flag)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            x
=un(a,1);
            
for(i=2;i<=n;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if(un(a,i)!=x){flag=0;break;}
        }

        
if(flag)printf("Yes\n");
        
else printf("No\n");
    }

    
return 0;
}

当然并查集的第二种实现方法是树形结构,其“并”效率为,“查”效率为
 
 
 

转载于:https://www.cnblogs.com/Xredman/archive/2009/03/30/1424772.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值