-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfusões.cpp
More file actions
43 lines (35 loc) · 724 Bytes
/
fusões.cpp
File metadata and controls
43 lines (35 loc) · 724 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
#include <bits/stdc++.h>
using namespace std;
int pai[100005];
int peso[100005];
void inicia(int n){
for(int i = 1; i <= n;i++) pai[i]=i;
}
int find(int x) {
if(pai[x]==x)return x;
return pai[x]=find(pai[x]);
}
void join(int x, int y){
x = find(x); y = find(y);
if(x==y)return;
if(peso[x]==peso[y]){pai[x]=y; peso[y]++;}
if(peso[x]>peso[y])pai[y]=x;
if(peso[x]<peso[y])pai[x]=y;
}
int main()
{
int n, k, x, y;
char c;
cin >> n >> k;
inicia(n);
while(k--){
cin>>c>>x>>y;
if(c=='C'){
if(find(x)==find(y))cout<<"S"<<endl;
else cout<<"N"<<endl;
}else{
join(x,y);
}
}
return 0;
}