并查集 Message System -- ZJU 2740

本文介绍了一个使用并查集算法来判断消息系统是否能够确保每个节点接收消息不重复且完整的问题。通过构建图模型并利用并查集判断生成树的方式,验证了系统的可行性。
Message System

Time limit: 1 Seconds   Memory limit: 32768K  
Total Submit: 1839   Accepted Submit: 512  

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。最后统计标记过的点的个数,看是否
等于整个图的点的个数,同时等于边的条数加1。代码如下:
Code:

#include

<stdio.h>

#include

<string.h>

#define

MAXN 1001

class

{

DisjointSet

public

DisjointSet()

{

}

 

{

size = s;

 

{

parent[i] = -1;

}

}

 

{

 

 

 

 

{

t = parent[k];

parent[k] = j;

k = t;

}

 

 

 

if(parent[i] != i)

parent[i] = Find_Set(parent[i]);

return parent[i];

*/

:void Make_Set(int s)for(int i = 0;i < s; i++)int Find_Set(int i)int j, k, t;for(j = k = i; parent[j] >= 0; j = parent[j]);while(k != j)return j;/*

}

 

{

 

 

 

 

 

 

 

{

parent[root_x] += parent[root_y];

parent[root_y] = root_x;

}

 

void Union(int x, int y)if(x == y)return;int root_x = Find_Set(x);int root_y = Find_Set(y);if(root_x == root_y)return;if(parent[root_x] < parent[root_y])else

{

parent[root_y] += parent[root_x];

parent[root_x] = root_y;

}

 

if(x == y)

return;

int root_x = Find_Set(x);

int root_y = Find_Set(y);

if(root_x == root_y)

return;

if(rank[x] > rank[y])

{

parent[y] = x;

}else

{

parent[x] = y;

if(rank[x] == rank[y])

rank[y]++;

}

*/

/*

}

protected

 

 

:int parent[MAXN];//int rank[MAXN];

 

};

int size;

int

{

 

 

 

DisjointSet ds;

 

{

scanf(

 

 

 

ds.Make_Set(N);

 

main(int argc,char **argv)int N,M,i,a,b,x,y;bool duplicated = false;int flag[MAXN];while(true)"%d %d",&N, &M);if(N==0 && M ==0)break;//memset(flag, 0 ,N);

duplicated =

 

 

{

scanf(

x = ds.Find_Set(a-1);

y = ds.Find_Set(b-1);

 

 

duplicated =

ds.Union(x,y);

flag[a-1] = flag[b-1] = 1;

}

 

 

v += flag[i];

 

{

printf(

}

 

false;for(i = 0; i < M; i++)"%d %d",&a, &b);if(x == y)true;int v = 0;for(i=0; i< N; i++)if(v == N && v == M+1 && !duplicated)"Yes/n");else

{

printf(

}

}

 

 

}

 

但是本人对题目意思不是很明白。题目要求是每个点收到消息不会重复,那么第2个测试用例应该

满足,为什么参考输出为No呢。这个程序中还有一个地方不是很清楚。如果不注释

memset(flag, 0 ,N)这句,怎么也过不了。一旦注释就过了,这又到底是怎么回事呢。

望指点!

msn:king821221@live.com

"No/n");return 0;
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值