-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
remove-methods-from-project.cpp
48 lines (46 loc) · 1.34 KB
/
remove-methods-from-project.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
// Time: O(n + e)
// Space: O(n + e)
// bfs
class Solution {
public:
vector<int> remainingMethods(int n, int k, vector<vector<int>>& invocations) {
vector<vector<int>> adj(n);
const auto& bfs = [&]() {
vector<bool> lookup(n);
lookup[k] = true;
vector<int> q = {k};
while (!empty(q)) {
vector<int> new_q;
for (const auto& u : q) {
for (const auto& v : adj[u]) {
if (lookup[v]) {
continue;
}
lookup[v] = true;
new_q.emplace_back(v);
}
}
q = move(new_q);
}
return lookup;
};
for (const auto& e : invocations) {
adj[e[0]].emplace_back(e[1]);
}
const auto& lookup = bfs();
for (const auto& e : invocations) {
if (lookup[e[0]] != lookup[e[1]]) {
vector<int> result(n);
iota(begin(result), end(result), 0);
return result;
}
}
vector<int> result;
for (int u = 0; u < n; ++u) {
if (!lookup[u]) {
result.emplace_back(u);
}
}
return result;
}
};