您的位置:首页 > Web前端

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

2017-10-31 14:46 549 查看
题目位置

看了老师的 各种骚操作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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: