std::vector<int> topologicalSort(std::unordered_map<int, std::vector<int>>& graph) {
std::unordered_map<int, int> inDegree;
for (const auto& node : graph) {
inDegree[node.first] = 0;
}
for (const auto& node : graph) {
for (int neighbor : node.second) {
inDegree[neighbor]++;
}
}
std::queue<int> q;
for (const auto& entry : inDegree) {
if (entry.second == 0) {
q.push(entry.first);
}
}
std::vector<int> result;
while (!q.empty()) {
int node = q.front();
q.pop();
result.push_back(node);
for (int neighbor : graph[node]) {
inDegree[neighbor]--;
if (inDegree[neighbor] == 0) {
q.push(neighbor);
}
}
}
if (result.size() == graph.size()) {
return result;
} else {
return {};
}
}