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.是否有回路
第一个问题好解决,因为并查集本来就有“查”这一特性,
第二个问题的解决之道在于并得时候查找合并的二个集合是否属于同一个集合,若是,则说明有回路。
这是我用第一种并查集做的,其效率“并”是Θ(N),“查”是Θ(1)
源代码:
/*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)
{
    
return cnt[x];
}


void Merge(int a, int b)
{
    
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()
{

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

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

                
else
                
{
                    Merge(a, b);
                }

            }

        }

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

        }

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

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

int main()
{
    
int n,m,i,x,y,k,p,a[1005],flag;
    
while(scanf("%d %d",&n,&m)!=EOF)
    
{
        
if(n==0&&m==0)break;
        
for(i=0;i<=n;i++)a[i]=i;
        flag
=1;
        
for(i=1;i<=m;i++)
        
{
            scanf(
"%d %d",&x,&y);
            k
=un(a,x);
            p
=un(a,y);
            
if(flag)
            
{
                
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)
        
{
            x
=un(a,1);
            
for(i=2;i<=n;i++)
                
if(un(a,i)!=x){flag=0;break;}
        }

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

    
return 0;
}

当然并查集的第二种实现方法是树形结构,其“并”效率为Θ(1),“查”效率为Θ(N)
具体代码可参考,下面博客下次自己也实现一下,O(∩_∩)O~
相应课件下载>>

posted on 2009-03-30 08:43  Xredman  阅读(422)  评论(0编辑  收藏  举报

导航