Zju 2740 Message System解题分析

http://acm.zju.edu.cn/show_problem.php?pid=2740 想来大家这题的困惑在于题意读不清楚,实际上是判断m条边能否连通n个顶点而且没有回路。判断是否有回路可能也要想上好久。怎么办?实际上可以简单处理。思考一下n个顶点的连通图的最小生成树有几条边。这样就可以通过判断m与n的关系来判断是否有回路了。还不明白?那么再看看题目吧。 作为图有关算法的练习,大家可以用dfs,bfs,Prim,Dijkstra算法都去做一遍的。
阅读更多

Message System

03-24

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.nnInputnThe 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.nnOutputnFor 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.nnSample Inputn4 3n1 2n2 3n3 4nn3 1n2 3nn0 0nnSample OutputnYesnNonn

Message System 并查集

01-13

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. rnrnInputrnThe 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.rnrnOutputrnFor 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.rnrnSample Inputrn4 31 22 33 43 12 30 0rnSample OutputrnYesNorn解决方案:rn采用并查集判断整个图是否是一棵生成树。处理输入的每一条边的时,判断边的两个端点是否已经在同一个连通子集rn中。如果是,则不能满足要求。在处理完所有边后,还需要判断整个图是否连通。具体判断方法参考了别人的程序,rn设置一个标记数组,每个元素表示对应点是否为某条输入边的端点,是则记录为1。最后统计标记过的点的个数,看是否rn等于整个图的点的个数,同时等于边的条数加1。代码如下:rnrnCode:rn#include rn#include rnrn#define MAXN 1001rnrnclass DisjointSetrnrnpublic:rn DisjointSet()rn rn rnrn void Make_Set(int s)rn rn size = s;rn for(int i = 0;i < s; i++)rn rn parent[i] = -1;rn rn rnrn int Find_Set(int i)rn rn rn int j, k, t;rnrn for(j = k = i; parent[j] >= 0; j = parent[j]);rnrn while(k != j)rn rn t = parent[k];rn parent[k] = j;rn k = t;rn rnrn return j;rn rn /*rn if(parent[i] != i)rn parent[i] = Find_Set(parent[i]);rn return parent[i];rn */rn rnrn void Union(int x, int y)rn rn if(x == y)rn return;rnrn int root_x = Find_Set(x);rn int root_y = Find_Set(y);rnrn if(root_x == root_y)rn return;rnrn if(parent[root_x] < parent[root_y])rn rn parent[root_x] += parent[root_y];rn parent[root_y] = root_x;rn rn elsern rn parent[root_y] += parent[root_x];rn parent[root_x] = root_y;rn rnrn /*rn if(x == y)rn return;rnrn int root_x = Find_Set(x);rn int root_y = Find_Set(y);rnrn if(root_x == root_y)rn return;rnrn if(rank[x] > rank[y])rn rn parent[y] = x;rn elsern rn parent[x] = y;rn if(rank[x] == rank[y])rn rank[y]++;rn rn */rn rnrnprotected:rn int parent[MAXN];rn //int rank[MAXN];rn int size;rn;rnrnint main(int argc,char **argv)rnrn int N,M,i,a,b,x,y;rn bool duplicated = false;rn int flag[MAXN];rnrn DisjointSet ds;rn while(true)rn rn scanf("%d %d",&N, &M);rn rn if(N==0 && M ==0)rn break;rnrn ds.Make_Set(N);rn //memset(flag, 0 ,N);rn duplicated = false;rn rn for(i = 0; i < M; i++)rn rn scanf("%d %d",&a, &b);rn x = ds.Find_Set(a-1);rn y = ds.Find_Set(b-1);rn rn if(x == y)rn duplicated = true;rnrn ds.Union(x,y);rn flag[a-1] = flag[b-1] = 1;rn rnrn int v = 0;rn for(i=0; i< N; i++)rn v += flag[i];rnrn if(v == N && v == M+1 && !duplicated)rn rn printf("Yes\n");rn rn elsern rn printf("No\n");rn rn rn rn return 0;rnrnrn但是本人对题目意思不是很明白。题目要求是每个点收到消息不会重复,那么第2个测试用例应该满足,为什么参考输出为No呢。这个程序中还有一个地方不是很清楚。如果不注释memset(flag, 0 ,N)这句,怎么也过不了。一旦注释就过了,这又到底是怎么回事呢。望指点!rn

没有更多推荐了,返回首页