?
?
链接:
iota - C++ Reference
题解:
力扣
?
?
?
class Solution {
public:
vector<int> loudAndRich(vector<vector<int>>& richer, vector<int>& quiet) {
std::vector<int> indegree(quiet.size(), 0);
std::unordered_map<int, std::set<int>> graph;
for (auto& edge : richer) {
// 金钱高指向金钱低的
graph[edge[0]].insert(edge[1]);
++indegree[edge[1]];
}
std::vector<int> result(quiet.size(), 0);
// 初始化结果集合为自己
iota(std::begin(result), std::end(result), 0);
std::queue<int> que;
for (int i = 0; i < quiet.size(); ++i) {
if (indegree[i] == 0) {
que.push(i);
}
}
while (!que.empty()) {
auto f = que.front();
que.pop();
for (auto neighbord : graph[f]) {
// 判读自己的邻居,是否可以更新邻居的结果集合
if (quiet[result[f]] < quiet[result[neighbord]]) {
result[neighbord] = result[f];
}
if (--indegree[neighbord] == 0) {
que.push(neighbord);
}
}
}
return result;
}
};
|