-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuick_Union.cpp
More file actions
45 lines (45 loc) · 853 Bytes
/
Copy pathQuick_Union.cpp
File metadata and controls
45 lines (45 loc) · 853 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<iomanip>
#include<algorithm>
using namespace std;
#define N 1000000
int id[N];
int find(int p)
{
if(id[p]==p) return p;
return id[p]=find(id[p]);
}
int main ()
{
int t;
cin>>t;
int x=0;
while(t--){
for(int i=0;i<N;i++)id[i]=i;
int n;
cin>>n;
getchar();
char c;
int p,q;
int ans1=0,ans2=0;
while ((c=getchar())&&isalpha(c)){
cin>>p>>q;
getchar();
int pp=find(p);
int qq=find(q);
if (c=='c'){
id[pp]=qq;
} else {
if(pp==qq)ans1++;
else ans2++;
}
}
if (x==1)cout<<endl;
x=1;
cout<<ans1<<","<<ans2<<endl;
}
return 0;
}