-
Notifications
You must be signed in to change notification settings - Fork 0
/
并查集
37 lines (36 loc) · 753 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
#include <iostream>
#include <stdio.h>
#define MAX 10005
using namespace std;
int father[MAX];
void init(int n) {
for(int i=1;i<=n;i++) father[i] = i;
}
int FindFather(int x) {
while(x != father[x]) x = father[x];
return x;
}
void unionn(int x,int y) {
int a = FindFather(x);
int b = FindFather(y);
if(a != b) father[a] = b;
}
bool judge(int x,int y) {
int a = FindFather(x);
int b = FindFather(y);
if(a != b) return false;
return true;
}
int main() {
int n,m; cin>>n>>m;
init(n);
while(m--) {
int p,x,y; scanf("%d%d%d",&p,&x,&y);
if(p==1) unionn(x,y);
else {
if(judge(x,y)) printf("Y\n");
else printf("N\n");
}
}
return 0;
}