Message System

Message System

Time limit: 1 Seconds   Memory limit: 32768K  
Total Submit: 681   Accepted Submit: 155  

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

C语言的求解和结果
感觉上好像是一个生成树就行,变成判断是否生成树了,但答案不对。。。。。
下面的算法可以得到一个连通集合,某个结点i对应的值array[i]为与结点i连通的所有结点的最小序号。应该可以用于其它算法吧
#include <stdio.h>
int main()
{
    int N, M, a, b;
    int i, j, k, min, max, temp;
    int array[1000];
    while(1){
        scanf("%d%d", &N, &M);
        if(N == 0 && M == 0) break;
        for(i = 0; i < N; i++) array[i] = i;
        for(i = 0; i < M; i++){
            scanf("%d%d", &a, &b);
            a--;    b--;
            min = array[a];
            max = array[b];
            if(min > max){
                temp = min; min = max;  max = temp;
            }
            for(j = 0; j < N; j++){
                if(array[j] == max) array[j] = min;
            }
        }
        for(i = 0; i < N; i++)
            if(array[i] != 0) break;
        if(i == N) puts("Yes");
        else puts("No");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值