-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVA793.cpp
executable file
·47 lines (40 loc) · 977 Bytes
/
UVA793.cpp
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
46
47
#include<bits/stdc++.h>
using namespace std;
int parent[900];
void makeSet(int n){
for(int i = 0; i < n; i++) parent[i] = i;
}
int find(int i){
if(parent[i] == i) return i;
return find(parent[i]);
}
bool sameSet(int i, int j){
return find(i) == find(j);
}
void unionSet(int i, int j){
int iRoot = find(i), jRoot = find(j);
parent[iRoot] = jRoot;
}
int main(){
int tc, n, u, v, yes, no;
char request;
scanf("%d", &tc);
while(tc--){
scanf("%d", &n);
getchar();
yes = no = 0;
makeSet(n);
while((request = getchar()) && isalpha(request)){
scanf("%d %d", &u, &v);
getchar();
if(request == 'c') unionSet(u-1, v-1);
else {
if(sameSet(u-1, v-1)) yes++;
else no++;
}
}
printf("%d,%d\n", yes, no);
if(tc) printf("\n");
}
return 0;
}