-
Notifications
You must be signed in to change notification settings - Fork 0
/
793.cc
49 lines (40 loc) · 1009 Bytes
/
793.cc
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
48
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int fnd(vector<int>& parent,int key){
if(parent[key-1] == key)
return key;
return parent[key-1] = fnd(parent,parent[key-1]);}
void mrg(vector<int>& parent,int key1,int key2){
int id1 = fnd(parent,key1);
int id2 = fnd(parent,key2);
if(id1 != id2)
parent[id1-1] = id2;}
int main(){
int tries;
cin >> tries;
for(int i = 0;i < tries;i++){
vector<int> ids;
int nc,p1,p2,na = 0,nu = 0;
char type;
string line;
cin >> nc;
getline(cin,line);
for(int j = 1;j <= nc;j++)
ids.push_back(j);
while(getline(cin,line) && line != ""){
istringstream strin(line);
strin >> type >> p1 >> p2;
if(type == 'c')
mrg(ids,p1,p2);
else if(type == 'q'){
if(fnd(ids,p1) == fnd(ids,p2))
na++;
else
nu++;}}
if(i != 0)
cout << endl;
cout << na << ',' << nu << endl;}
return 0;}