/*
* UVA_793.cpp
*
* Created on: 2013年11月6日
* Author: Administrator
*/
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 50010;
int father[maxn];
int find(int x){//并查集的查询
if(father[x] == x){
return x;
}
return (father[x] = find(father[x]));
}
void join(int x,int y){//并查集的合并
x = find(x);
y = find(y);
if(x != y){
father[x] = y;
}
}
int main(){
int t;
scanf("%d\n",&t);//测试用例数后带一个空行的处理方法
while(t--){
int n;
scanf("%d\n",&n);
int i;
for(i = 1 ; i <= n ; ++i){
father[i] = i;
}
char str[10];
int yes = 0;
int no = 0;
while(str[0] = getchar()){//用空行来分割测试用例的方法
if(str[0] == '\n' || str[0] == EOF){
break;
}
int x,y;
scanf("%d%d",&x,&y);
getchar();
if(str[0] == 'c'){
join(x,y);
}else{
if(find(x) == find(y)){//计算查询中yes和no出现的次数
yes++;
}else{
no++;
}
}
}
printf("%d,%d\n",yes,no);
if(t){//每个测试用例后输出一个空行的处理方法
printf("\n");
}
}
return 0;
}
(是用树结构来支持并查集的操作8.4.5)UVA 793 Network Connections(输入输出处理的的经典题&&并查集操作之记录查询中yes和no出现的次数)
最新推荐文章于 2022-12-18 23:15:08 发布