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

题目位置

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

#include 
#include 
#include 
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;i1;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;i1;i++){
    if(computer[i]<0)counter++;
    }
    if(counter==1)printf("The network is connected.");
    else printf("There are %d components.",counter);
}

你可能感兴趣的:(c语言)