-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
39 lines (31 loc) · 1.01 KB
/
main.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
class Solution {
public:
vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
unordered_map<int, vector<int>> graph;
for (int i = 0; i < pid.size(); ++i) graph[ppid[i]].push_back(pid[i]);
vector<int> result;
deque<int> to_be_killed = {kill};
while (!to_be_killed.empty()) {
int u = to_be_killed.front();
to_be_killed.pop_front();
result.push_back(u);
for (int v : graph[u]) to_be_killed.push_back(v);
}
return result;
}
};
class Solution {
public:
vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
int n = pid.size();
unordered_map<int, vector<int>> adj;
for (int i = 0; i < n; ++i) adj[ppid[i]].push_back(pid[i]);
vector<int> result;
function<void(int)> dfs = [&](int u) -> void {
result.push_back(u);
for (int v : adj[u]) dfs(v);
};
dfs(kill);
return result;
}
};