4道 Uva 并查集

上周做的=.=

没发上来.导致文件夹内大量cpp...

今天在学活从6点切到了9:30

uva313AC排名1396

呵呵=.=

明天下午校赛,上午就不做题了,把实验选了,开心就好

以上

并查集 以及通过并查集实现的最小生成树

459 - Graph Connectivity

#include<stdio.h>
int ft[30];
void in_ft(int n){
	int i;
	for(i=0;i<=n;i++)ft[i]=i;
	}
int find(int x){
	int temp,t=x;
	while(ft[x]!=x)x=ft[x];
	while(t!=x){
		temp=ft[t];
		ft[t]=x;
		t=temp;
		}
	return x;
	}
void merge(int a,int b){
	int fa=find(a);
	int fb=find(b);
	ft[fa]=fb;
	}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		char s[3];
		scanf("%s%*c",s);
		in_ft(s[0]-'A');
		char x,y;
		int i,res=0;
		while(scanf("%c",&x)!=EOF){
			if(x=='\n')break;
			scanf("%c%*c",&y);
			merge(x-'A',y-'A');
			}
	//	for(i=0;i<=s[0]-'A';i++)printf("%d ",ft[i]);printf("\n");
		for(i=0;i<=s[0]-'A';i++)if(ft[i]==i)res++;
		printf("%d\n",res);
		if(t)printf("\n");
		}
	return 0;
	}


793 - Network Connections

#include<stdio.h>
int ft[10030];
void in_ft(int n){
	int i;
	for(i=1;i<=n;i++)ft[i]=i;
	}
int find(int x){
	int temp,t=x;
	while(ft[x]!=x)x=ft[x];
	while(t!=x){
		temp=ft[t];
		ft[t]=x;
		t=temp;
		}
	return x;
	}
void merge(int a,int b){
	int fa=find(a);
	int fb=find(b);
	ft[fa]=fb;
	}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n,i;
		scanf("%d%*c",&n);
		in_ft(n);
		int n1=0,n2=0;
		char s[1000];
		while(gets(s)!=NULL){
			if(s[0]=='\0')break;
			char q;
			int x,y;
			sscanf(s,"%c %d %d",&q,&x,&y);
			if(q=='c')merge(x,y);
			else {
				if(find(x)==find(y))n1++;
				else n2++;
				}
			}
		printf("%d,%d\n",n1,n2);
		if(t)printf("\n");
		}
	return 0; 
	}


1160 - X-Plosives

#include<stdio.h>
int ft[100010];
void in_ft(){
	int i;
	for(i=1;i<=100000;i++)ft[i]=i;
	}
int find(int x){
	int t=x,temp;
	while(x!=ft[x])x=ft[x];
	while(t!=x){
		temp=ft[t];
		ft[t]=x;
		t=temp;
		}
	return x;
	}
void merge(int a,int b){
	int fa=find(a);
	int fb=find(b);
	ft[fb]=fa;
	}
int main(){
	int x,y;
	int sum=0;
	in_ft();
	while(scanf("%d",&x)!=EOF){
		if(x==-1){
			printf("%d\n",sum);
			in_ft();
			sum=0;
			continue;
			}
		scanf("%d",&y);
		if(find(x)!=find(y))merge(x,y);
		else sum++;
		}
	return 0;
	}


1329 - Corporative Network

#include<stdio.h>
const int maxn=20000+10;
int ft[maxn],d[maxn];
int dis(int a,int b){
	int z;
	if(a-b>0)z=a-b;
	else z=b-a;
	return z%1000;
	}
void in_ft(int n){
	int i;
	for(i=1;i<=n;i++)ft[i]=i,d[i]=0;
	}
int find(int x){
	if(ft[x]!=x){
		int root=find(ft[x]);
		d[x]+=d[ ft[x] ];
		return ft[x]=root;
		}
	return x;
	}
int main(){
	char c,s[1000];
	int t;
	scanf("%d",&t);
	while(t--){
		int n,x,y;
		scanf("%d%*c",&n);
		in_ft(n);
		while(scanf("%s",s)!=EOF&&s[0]!='O'){
			if(s[0]=='I'){
				scanf("%d%d",&x,&y);
				ft[x]=y;
				d[x]=dis(x,y);
				}
			else {
				scanf("%d",&x);
				find(x);
				printf("%d\n",d[x]);
				}
			}
		}
	return 0;
	}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值