第三题 typedef struct node {     int x, y;     friend bool operator <(node a, node b) {         if (a.x == b.x)             return a.y > b.y;         else             return a.x > b.x;     }     node(int a, int b) {         x = a;         y = b;     } }; int main() {     priority_queue<node> q;     int n, m;     cin >> n >> m;     vector<int> need_time(n + 1);     for (int i = 1; i <= n; i++)         cin >> need_time[i];     vector<vector<int>> depen(n + 1);     vector<int> de_nums(n + 1, 0);     for (int i = 0; i < m; i++) {         int a, b;         cin >> a >> b;         de_nums[b]++;         depen[a].push_back(b);     }     for (int i = 1; i <= n; i++)         if (de_nums[i] == 0)             q.push(node(need_time[i], i));     vector<int> res;     while (!q.empty()) {         int y = q.top().y;         res.push_back(y);         q.pop();         for (int i = 0; i < depen[y].size(); i++) {             de_nums[depen[y][i]]--;             if (de_nums[depen[y][i]] == 0)                 q.push(node(need_time[depen[y][i]], depen[y][i]));         }     }     for (int i = 0; i < res.size(); i++)         cout << res[i] << ' ';     cout << endl;     system("pause");         return 0; }
点赞 16

相关推荐

牛客网
牛客网在线编程
牛客网题解
牛客企业服务