7-8 File Transfer (25 分)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_44720323/article/details/102666848

7-8 File Transfer (25 分)

这道题主要考查了,并查集的一些基本操作。
做这道题的时候应该注意要按大小union压缩路径

#include <stdio.h>
#include <stdlib.h>
#define INFO -100000
int Parenet[10001]; //储存父节点
int Find(int c1){
    for(;Parenet[c1] >= 0;c1 = Parenet[c1]);
    return c1;
}
void Union(int c1,int c2){
    int root1,root2;
    root1 = Find(c1);
    root2 = Find(c2);
    if(root1 == 0 && root2 == 0){
        Parenet[root2]--;
        Parenet[root1] = root2;
    } else if(Parenet[root1] >= Parenet[root2]){
        Parenet[root2] += Parenet[root1]; 
        Parenet[root1] = root2;
    } else if(Parenet[root2] > Parenet[root1]){
        Parenet[root1] += Parenet[root2];
        Parenet[root2] = root1;
    }
}
int main(){
    int n,c1,c2,root1,root2;
    char S;
    Parenet[0] = INFO;
    scanf("%d",&n);
    getchar();
    for(int i=1;i<=n;i++) Parenet[i] = -1;
    while(1){
        scanf("%c",&S);
        if(S == 'S') break;
        scanf("%d %d",&c1,&c2);
        getchar();
        if(S == 'I') Union(c1,c2);
        else{
            root1 = Find(c1);
            root2 = Find(c2);
            if(root1 == root2) printf("yes\n");
            else printf("no\n");
        }
    }
    int i = 1,count = 0,sum = 0;
    for(;i<=n;i++){
        if(Parenet[i] < 0){
            sum += Parenet[i];
            count++;
        }  
    }
    if(count == 1 && sum + n == 0) printf("The network is connected.\n");    
    else{
        printf("There are %d components.\n",sum+n+count);
    }
    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44720323/article/details/102666848