拓扑排序 | #任务调度#

任务调度

https://www.nowcoder.com/practice/88d5fa34fe0748e09062e48c6ae6ffc7

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

int main() {
    int n;
    map<string, int> degree;//入度
    map<string, vector<string>> grid;//邻接矩阵
    set<string> nodes;
    while (cin >> n) { // 注意 while 处理多个 case
        string task;
        for (int i = 0; i < n; i++) {
            cin>>task;
            int index=task.find('(');
            string s1=task.substr(0,index);
            nodes.insert(s1);
            string tmp="";
            for(int j=index+1;j<task.size();j++){
                if((task[j]==','||task[j]==')')&&tmp.compare("NULL")!=0){
                    // arr.push_back(tmp);
                    grid[s1].push_back(tmp);
                    degree[tmp]++;//入度增加
                    nodes.insert(tmp);
                    tmp="";
                }else
                    tmp+=task[j];
            } 
        }
        queue<string> q;
        for(string s:nodes){
            if(degree[s]==0)
                q.push(s);      
        }
        vector<string> ans;
        while(!q.empty()){
            int size=q.size();
            for(int i=0;i<size;i++){
                string x=q.front();
                q.pop();
                ans.push_back(x);
                for(string neb:grid[x]){
                    degree[neb]--;
                    if(degree[neb]==0)
                        q.push(neb);
                }
            }
        }
        for_each(ans.begin(),ans.end(),[](string x){cout<<x<<" ";});
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

ohs的小木屋:比不少实习待遇高了
点赞 评论 收藏
分享
程序员牛肉:主要是因为小厂的资金本来就很吃紧,所以更喜欢有实习经历的同学。来了就能上手。 而大厂因为钱多,实习生一天三四百的就不算事。所以愿意培养你,在面试的时候也就不在乎你有没有实习(除非是同级别大厂的实习。) 按照你的简历来看,同质化太严重了。项目也很烂大街。 要么换项目,要么考研。 你现在选择工作的话,前景不是很好了。
点赞 评论 收藏
分享
zYvv:双一流加大加粗再标红,然后广投。主要是获奖荣誉不够,建议开始不用追求大厂,去别的厂子刷下实习。
点赞 评论 收藏
分享
就在我现在公司的隔壁每天经过都唏嘘不已(就是羡慕)什么时候可以到这里上班啊
柯基在debug:从大学毕业投简历到现在了,应届的时候我都面到终面了,现在工作四年了连简历初筛都过不了了
投递莉莉丝游戏等公司8个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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