class Solution {
public:
vector<int> loudAndRich(vector<vector<int>>& richer, vector<int>& quiet) {
int n = quiet.size();
vector<vector<int>> edges(n);
vector<int> inDegree(n, 0);
for (const auto& edge : richer) {
edges[edge[0]].push_back(edge[1]);
inDegree[edge[1]]++;
}
queue<int> que;
for (int i = 0; i < n; i++) {
if (inDegree[i] == 0)
que.push(i);
}
vector<int> res(n);
for (int i = 0; i < n; i++)
res[i] = i;
while (!que.empty()) {
int current = que.front();
que.pop();
for (auto v : edges[current]) {
if (quiet[res[current]] < quiet[res[v]])
res[v] = res[current];
if (--inDegree[v] == 0)
que.push(v);
}
}
return res;
}
};