-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwheresmyinternet.cc
123 lines (92 loc) · 2.07 KB
/
wheresmyinternet.cc
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// William Sjöblom
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
/**
* Set node.
*/
struct SetNode {
SetNode* parent;
int rank;
};
/**
* Set forest.
*/
std::vector<SetNode*> forest;
/**
* Get root of set node.
*/
SetNode* root(SetNode* node) {
while (node->parent != node) {
node = node->parent;
}
return node;
}
void init_forest(int size) {
forest.reserve(size);
for (int i = 0; i < size; i++) {
SetNode* set = new SetNode;
set->parent = set;
set->rank = 0;
forest.push_back(set);
}
}
/**
* Merge the sets containing a and b.
* Return root node of set.
*/
SetNode* uni(int a, int b) {
SetNode* a_root = root(forest[a]);
SetNode* b_root = root(forest[b]);
if (a_root == b_root) return a_root;
if (a_root->rank < b_root->rank) {
std::swap(a_root, b_root);
}
b_root->parent = a_root;
if (a_root->rank == b_root->rank) {
a_root->rank++;
}
return a_root;
}
/**
* Are a and b in the same set?
*/
bool same(int a, int b) {
SetNode* a_root = root(forest[a]);
SetNode* b_root = root(forest[b]);
return a_root == b_root;
}
bool same(SetNode* a, SetNode* b) {
SetNode* a_root = root(a);
SetNode* b_root = root(b);
return a_root == b_root;
}
#include <assert.h>
int main() {
int house_count, op_count;
scanf("%d %d\n", &house_count, &op_count);
init_forest(house_count);
int first_a = 0;
while (op_count--) {
int a, b;
scanf("%d %d", &a, &b);
if (!first_a) first_a = a;
uni(a - 1, b - 1);
}
SetNode* internet_root = forest[0];
if (!internet_root) printf("no root\n");
bool connected = true;
for (int i = 0; i < house_count; i++) {
SetNode* current = forest[i];
if (!same(current, internet_root)) {
printf("%d\n", i + 1);
//connected &= false;
connected = false;
}
}
if (connected) {
printf("Connected\n");
}
return 0;
}