forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
find-all-people-with-secret.cpp
152 lines (144 loc) · 4.53 KB
/
find-all-people-with-secret.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Time: O(nlogn)
// Space: O(n)
class Solution {
public:
vector<int> findAllPeople(int n, vector<vector<int>>& meetings, int firstPerson) {
sort(begin(meetings), end(meetings), [](const auto& a, const auto& b) { return a[2] < b[2]; });
unordered_set<int> result = {0, firstPerson};
unordered_map<int, vector<int>> adj;
for (int i = 0; i < size(meetings); ++i) {
int x = meetings[i][0], y = meetings[i][1];
adj[x].emplace_back(y);
adj[y].emplace_back(x);
if (i + 1 != size(meetings) && meetings[i + 1][2] == meetings[i][2]) {
continue;
}
vector<int> q;
for (const auto& [k, _] : adj) {
if (result.count(k)) {
q.emplace_back(k);
}
}
while (!empty(q)) {
vector<int> new_q;
for (const auto& u : q) {
for (const auto& v : adj[u]) {
if (result.count(v)) {
continue;
}
result.emplace(v);
new_q.emplace_back(v);
}
}
q = move(new_q);
}
adj.clear();
}
return {cbegin(result), cend(result)};
}
};
// Time: O(nlogn)
// Space: O(n)
class Solution2 {
public:
vector<int> findAllPeople(int n, vector<vector<int>>& meetings, int firstPerson) {
sort(begin(meetings), end(meetings), [](const auto& a, const auto& b) { return a[2] < b[2]; });
unordered_set<int> result = {0, firstPerson};
unordered_map<int, vector<int>> adj;
for (int i = 0; i < size(meetings); ++i) {
int x = meetings[i][0], y = meetings[i][1];
adj[x].emplace_back(y);
adj[y].emplace_back(x);
if (i + 1 != size(meetings) && meetings[i + 1][2] == meetings[i][2]) {
continue;
}
vector<int> stk;
for (const auto& [k, _] : adj) {
if (result.count(k)) {
stk.emplace_back(k);
}
}
while (!empty(stk)) {
int u = stk.back(); stk.pop_back();
for (const auto& v : adj[u]) {
if (result.count(v)) {
continue;
}
result.emplace(v);
stk.emplace_back(v);
}
}
adj.clear();
}
return {cbegin(result), cend(result)};
}
};
// Time: O(nlogn)
// Space: O(n)
class Solution3 {
public:
vector<int> findAllPeople(int n, vector<vector<int>>& meetings, int firstPerson) {
sort(begin(meetings), end(meetings), [](const auto& a, const auto& b) { return a[2] < b[2]; });
UnionFind uf(n);
uf.union_set(0, firstPerson);
unordered_set<int> group;
for (int i = 0; i < size(meetings); ++i) {
int x = meetings[i][0], y = meetings[i][1];
group.emplace(x);
group.emplace(y);
uf.union_set(x, y);
if (i + 1 != size(meetings) && meetings[i + 1][2] == meetings[i][2]) {
continue;
}
for (const auto& x: group) {
if (uf.find_set(x) != uf.find_set(0)) {
uf.reset(x);
}
}
group.clear();
}
vector<int> result;
for (int i = 0; i < n; ++i) {
if (uf.find_set(i) == uf.find_set(0)) {
result.emplace_back(i);
}
}
return result;
}
private:
class UnionFind {
public:
UnionFind(int n)
: set_(n)
, rank_(n) {
iota(set_.begin(), set_.end(), 0);
}
int find_set(int x) {
if (set_[x] != x) {
set_[x] = find_set(set_[x]); // Path compression.
}
return set_[x];
}
bool union_set(int x, int y) {
x = find_set(x), y = find_set(y);
if (x == y) {
return false;
}
if (rank_[x] > rank_[y]) {
swap(x, y);
}
set_[x] = y; // Union by rank.
if (rank_[x] == rank_[y]) {
++rank_[y];
}
return true;
}
void reset(int x) {
set_[x] = x;
rank_[x] = 0;
}
private:
vector<int> set_;
vector<int> rank_;
};
};