-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0851.cpp
More file actions
29 lines (26 loc) · 779 Bytes
/
Copy path0851.cpp
File metadata and controls
29 lines (26 loc) · 779 Bytes
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
class Solution {
public:
vector<int> loudAndRich(vector<vector<int>> &richer, vector<int> &quiet) {
const int n = quiet.size();
vector<vector<int>> adj(n);
vector<int> count(n);
vector<int> res(n);
iota(res.begin(), res.end(), 0);
for (auto &p : richer) {
adj[p[0]].push_back(p[1]);
count[p[1]]++;
}
queue<int> q;
for (int i = 0; i < n; i++)
if (!count[i]) q.push(i);
while (!q.empty()) {
int crnt = q.front();
q.pop();
for (int &c : adj[crnt]) {
if (quiet[res[c]] > quiet[res[crnt]]) res[c] = res[crnt];
if (!--count[c]) q.push(c);
}
}
return res;
}
};