Top sort 双队列

时间:2021-04-02 18:42:17
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int num[];
int main()
{
queue<int>q;
queue<int>ans;
int n,m;
cin>>n>>m;
int x,y;
vector<int>u[];
for(int i=;i<=m;i++){
cin>>x>>y;
u[x].push_back(y);
num[y]++;
}
for(int i=;i<n;i++){
if(num[i]==){q.push(i);}
}
while(!q.empty()){
int k=q.front();q.pop();ans.push(k);
for(int i=;i<u[k].size();i++){
num[u[k][i]]--;
if(num[u[k][i]]==){q.push(u[k][i]);}
}
}
while(!ans.empty()){
cout<<ans.front()<<endl;
ans.pop();
}
}