-
Notifications
You must be signed in to change notification settings - Fork 3
/
851.cpp
35 lines (33 loc) · 1 KB
/
851.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
class Solution {
public:
vector<int> loudAndRich(vector<vector<int>>& richer, vector<int>& quiet) {
int n = quiet.size();
vector<vector<int>> adjacency(n);
vector<int> inDegrees(n, 0);
for (auto& p : richer) {
adjacency[p[0]].push_back(p[1]);
inDegrees[p[1]]++;
}
queue<int> q;
for (int i = 0; i < n; ++i) {
if (inDegrees[i] == 0) q.push(i);
}
vector<int> res(n, -1);
for (int i = 0; i < n; ++i) res[i] = i;
int level = 0;
while (!q.empty()) {
int node = q.front();
q.pop();
for (auto& neighbor : adjacency[node]) {
if (quiet[res[neighbor]] > quiet[res[node]]) {
res[neighbor] = res[node];
}
inDegrees[neighbor]--;
if (inDegrees[neighbor] == 0) {
q.push(neighbor);
}
}
}
return res;
}
};