forked from rudi8848/data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.4.auto_analysis.cpp
More file actions
75 lines (62 loc) · 1.23 KB
/
2.4.auto_analysis.cpp
File metadata and controls
75 lines (62 loc) · 1.23 KB
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
#include <iostream>
#include <vector>
class DisjoinSet
{
public:
DisjoinSet(unsigned numVariables) {
_rank.resize(numVariables + 1);
_parent.resize(numVariables + 1);
for (int i = 1; i < numVariables + 1; ++i)
makeSet(i);
}
~DisjoinSet() {}
void join(unsigned i, unsigned j) {
unsigned i_id, j_id;
i_id = find(i);
j_id = find(j);
if (i_id == j_id)
return;
if (_rank[i_id] > _rank[j_id])
_parent[j_id] = i_id;
else
_parent[i_id] = j_id;
if (_rank[i_id] == _rank[j_id])
_rank[j_id] += 1;
}
unsigned find(unsigned i) {
while (i != _parent[i])
i = _parent[i];
return i;
}
private:
void makeSet(unsigned i) {
_parent[i] = i;
_rank[i] = 0;
}
std::vector<unsigned> _parent;
std::vector<unsigned> _rank;
};
int main(void)
{
unsigned numVariables, equal, notEqual;
std::cin >> numVariables >> equal >> notEqual;
if (!notEqual) {
std::cout << "1" << std::endl;
return 0;
}
DisjoinSet set(numVariables);
unsigned a, b;
for (int i = 0; i < equal; ++i){
std::cin >> a >> b;
set.join(a, b);
}
for (int i = 0; i < notEqual; ++i) {
std::cin >> a >> b;
if (set.find(a) == set.find(b)) {
std::cout << "0" << std::endl;
return 0;
}
}
std::cout << "1" << std::endl;
return 0;
}