File tree 1 file changed +48
-0
lines changed
1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Time: O(n + e)
2
+ // Space: O(n + e)
3
+
4
+ // bfs
5
+ class Solution {
6
+ public:
7
+ vector<int > remainingMethods (int n, int k, vector<vector<int >>& invocations) {
8
+ vector<vector<int >> adj (n);
9
+ const auto & bfs = [&]() {
10
+ vector<bool > lookup (n);
11
+ lookup[k] = true ;
12
+ vector<int > q = {k};
13
+ while (!empty (q)) {
14
+ vector<int > new_q;
15
+ for (const auto & u : q) {
16
+ for (const auto & v : adj[u]) {
17
+ if (lookup[v]) {
18
+ continue ;
19
+ }
20
+ lookup[v] = true ;
21
+ new_q.emplace_back (v);
22
+ }
23
+ }
24
+ q = move (new_q);
25
+ }
26
+ return lookup;
27
+ };
28
+
29
+ for (const auto & e : invocations) {
30
+ adj[e[0 ]].emplace_back (e[1 ]);
31
+ }
32
+ const auto & lookup = bfs ();
33
+ for (const auto & e : invocations) {
34
+ if (lookup[e[0 ]] != lookup[e[1 ]]) {
35
+ vector<int > result (n);
36
+ iota (begin (result), end (result), 0 );
37
+ return result;
38
+ }
39
+ }
40
+ vector<int > result;
41
+ for (int u = 0 ; u < n; ++u) {
42
+ if (!lookup[u]) {
43
+ result.emplace_back (u);
44
+ }
45
+ }
46
+ return result;
47
+ }
48
+ };
You can’t perform that action at this time.
0 commit comments