-
Notifications
You must be signed in to change notification settings - Fork 22
/
589.connecting-graph.cpp
53 lines (47 loc) · 1017 Bytes
/
589.connecting-graph.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
class ConnectingGraph {
private:
vector<int> table;
public:
/*
* @param n: An integer
*/ConnectingGraph(int n) {
// do intialization if necessary
for (auto i = 0; i <= n; ++i)
{
table.push_back(i);
}
}
int find(int a){
if (a == table[a])
{
return a;
}
table[a] = find(table[a]);
return table[a];
}
/*
* @param a: An integer
* @param b: An integer
* @return: nothing
*/
void connect(int a, int b) {
// write your code here
int root_a = find(a);
int root_b = find(b);
if (root_a != root_b)
{
table[root_a] = root_b;
}
}
/*
* @param a: An integer
* @param b: An integer
* @return: A boolean
*/
bool query(int a, int b) {
// write your code here
int root_a = find(a);
int root_b = find(b);
return root_a == root_b;
}
};