#include<iostream>
#include<algorithm>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<sstream>
#include<set>
using namespace std;
int f[999999];
int findfa(int x)
{
if(x==f[x])
return x;
int y=findfa(f[x]);
return f[x]=y;
}
void myunion(int x,int y)
{
x=findfa(x);
y=findfa(y);
f[x]=y;
}
int main()
{
int casei;
char ch[3];
char s[10];
int n;
scanf("%d",&casei);
while(casei--)
{
int yes=0,no=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
f[i]=i;
getchar();
while(gets(s))
{
int a,b;
if(s[0]=='\0')
break;
stringstream sin(s);
sin>>ch>>a>>b;
if(ch[0]=='c')
{
myunion(a,b);
}
else
{
if(findfa(a)==findfa(b))
yes++;
else
no++;
}
}
printf("%d,%d\n",yes,no);
if(casei)
puts("");
}
return 0;
}
/*
2
10
c 1 5
c 2 7
q 7 1
c 3 9
q 9 6
c 2 5
q 7 5
1
q 1 1
c 1 1
q 1 1
*/
UVA 793 并查集
最新推荐文章于 2019-06-20 22:29:24 发布