-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3310.cpp
More file actions
51 lines (51 loc) · 1.48 KB
/
Copy path3310.cpp
File metadata and controls
51 lines (51 loc) · 1.48 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
class Solution {
public:
vector<int> remainingMethods(int n, int k, vector<vector<int>>& invocations) {
vector<vector<int>> graph(n);
vector<vector<int>> revGraph(n);
for (auto& invocation : invocations) {
graph[invocation[0]].push_back(invocation[1]);
revGraph[invocation[1]].push_back(invocation[0]);
}
vector<bool> visited(n, false);
queue<int> q;
q.push(k);
visited[k] = true;
while (!q.empty()) {
int m = q.size();
while (m--) {
int node = q.front();
q.pop();
for (auto& neighbor : graph[node]) {
if (visited[neighbor]) continue;
visited[neighbor] = true;
q.push(neighbor);
}
}
}
bool flag = true;
for (int i = 0; i < n; ++i) {
if (visited[i]) {
for (auto& neighbor : revGraph[i]) {
if (!visited[neighbor]) {
flag = false;
break;
}
}
if (!flag) break;
}
}
vector<int> res;
if (flag) {
for (int i = 0; i < n; ++i) {
if (!visited[i]) res.push_back(i);
}
}
else {
for (int i = 0; i < n; ++i) {
res.push_back(i);
}
}
return res;
}
};