05-树8 File Transfer(25 point(s))

题目位置

看了老师的 各种骚操作0 0
路径压缩 和 秩的运算

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int *computer;
int N;
void Connection(int position,int connection);
void Check(int position,int connection);
void Check_NextWork();
int find(int position);
int main(){
    int i,j;
    scanf("%d",&N);
    computer = (int*)malloc((N+1)*sizeof(int));
    for(i=0;i<N+1;i++){
        computer[i]=-1;
    }
    char s[2];
    int position,connection;
    do{
        scanf("%s",&s);
        switch(s[0]){
        case 'I':Connection(position,connection);break;
        case 'C':Check(position,connection);break;
        }
    }while(s[0]!='S');
    Check_NextWork(computer,N);
}
int find(int position){
    if(computer[position]<0)return position;
    else return computer[position] = find(computer[position]);
}
void Connection(int position,int connection){
    scanf("%d %d",&position,&connection);
    int i=find(position);
    int j=find(connection);
    if(computer[i]>computer[j])computer[i]=j,computer[j]-=1;
    else computer[j]=i,computer[i]-=1;
}
void Check(int position,int connection){
    scanf("%d %d",&position,&connection);
    int i=find(position);
    int j=find(connection);
    if(i==j)printf("yes\n");
    else printf("no\n");
}
void Check_NextWork(){
    int counter=0;
    int i;
    for(i=1;i<N+1;i++){
    if(computer[i]<0)counter++;
    }
    if(counter==1)printf("The network is connected.");
    else printf("There are %d components.",counter);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值