Skip to content

Instantly share code, notes, and snippets.

@nafis00141
Created October 10, 2016 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nafis00141/c0fb4c68e28844482a80632cc6691030 to your computer and use it in GitHub Desktop.
Save nafis00141/c0fb4c68e28844482a80632cc6691030 to your computer and use it in GitHub Desktop.
#include<bits/stdc++.h>
using namespace std;
int par[100000];
int fin(int x){
if(par[x]==x) return x;
return par[x]=fin(par[x]);
}
int main(){
int t;
char o[1000];
char c;
int a ,b;
cin>>t;
getchar();
while(t--){
int n;
int s=0,us=0;
scanf("%d\n\n", &n);
for(int i=0;i<=n;i++){
par[i]=i;
}
while(1){
gets(o);
if (strcmp(o, "") == 0 || feof(stdin)) {
break;
}
sscanf(o, "%c %d %d", &c, &a, &b);
a = fin(a);
b = fin(b);
//cout<<a<<b<<"\n";
if(c=='c'){
if(a!=b){
par[a]=b;
}
}
if(c=='q'){
if(a!=b){
us++;
}
else s++;
}
}
cout<<s<<","<<us;
if(t!=0) cout<<"\n\n";
else cout<<"\n";
}
}
/*
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
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment