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.
InputThe 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.
OutputFor 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 Input4 3 1 2 2 3 3 4 3 1 2 3 0 0Sample 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;
}