-
Notifications
You must be signed in to change notification settings - Fork 0
/
disjoint.cpp
98 lines (52 loc) · 881 Bytes
/
disjoint.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
#define INF 1000000000
typedef pair<int ,int> ii;
typedef vector <int> vi;
vector <pair<int ,ii> > edges;
vi pset;
void init(int N)
{
pset.assign(N, 0);
for(int i = 0; i < N; i++)
{
pset[i] = i;
}
}
int find_set(int i)
{
if(pset[i] == i)
{
return pset[i];
}
return pset[i] = find_set(pset[i]);
}
bool issameset (int i, int j)
{
return find_set(i) == find_set(j);
}
void Union(int i, int j)
{
pset[find_set(i)] = find_set(j);
}
int main()
{
for (int i=1;i<13;i++)
init(i);
Union(2, 10);
Union(7, 5);
Union(6, 1);
Union(3, 4);
Union(5, 11);
Union(7, 8);
Union(7, 3);
Union(12, 2);
Union(9, 6);
cout<<(find_set(6));
cout<<(find_set(3));
cout<<(find_set(11));
cout<<(find_set(9));
}