| Run ID | 作者 | 问题 | 语言 | 测评结果 | 时间 | 内存 | 代码长度 | 提交时间 |
|---|---|---|---|---|---|---|---|---|
| 84024 | sh25_shenpy | 任务调度 | C++ | 无测评数据 | 0 MS | 0 KB | 2766 | 2026-02-05 16:39:33 |
#include <iostream> #include <vector> #include <queue> #include <unordered_map> #include <algorithm> using namespace std; struct Task { string name; vector<string> successors; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; while (cin >> n) { unordered_map<string, int> inDegree; unordered_map<string, vector<string>> graph; vector<Task> tasks(n); // 读取输入并构建图 for (int i = 0; i < n; ++i) { string line; cin.ignore(); // 忽略行首的换行符(如果存在) getline(cin, line); size_t paren_pos = line.find('('); string taskName = line.substr(0, paren_pos); string successorsStr = line.substr(paren_pos + 1, line.size() - paren_pos - 2); tasks[i].name = taskName; if (successorsStr != "NULL") { size_t start = 0; size_t end = successorsStr.find(','); while (end != string::npos) { string successor = successorsStr.substr(start, end - start); tasks[i].successors.push_back(successor); start = end + 1; end = successorsStr.find(',', start); } string lastSuccessor = successorsStr.substr(start); if (!lastSuccessor.empty()) { tasks[i].successors.push_back(lastSuccessor); } } // 初始化入度 inDegree[taskName] = 0; } // 构建图和入度表 for (const auto &task : tasks) { for (const string &successor : task.successors) { graph[task.name].push_back(successor); inDegree[successor]++; } } // 拓扑排序 priority_queue<string, vector<string>, greater<string>> pq; // 最小堆 for (const auto &entry : inDegree) { if (entry.second == 0) { pq.push(entry.first); } } vector<string> result; while (!pq.empty()) { string current = pq.top(); pq.pop(); result.push_back(current); for (const string &neighbor : graph[current]) { if (--inDegree[neighbor] == 0) { pq.push(neighbor); } } } // 输出结果 for (size_t i = 0; i < result.size(); ++i) { if (i != 0) { cout << " "; } cout << result[i]; } cout << endl; } return 0; }