Untitled
unknown
plain_text
a year ago
956 B
6
Indexable
signed main(){
fastio;
int t=1;
//cin >> t;
while(t--){
int n,m;
cin>>n>>m;
vvi adj(n+1);
vi indeg(n+1,0);
for(int i=0;i<m;i++){
int u,v;
cin>>u>>v;
adj[u].pb(v);
indeg[v]++;
}
queue<int> q;
for(int i=1;i<=n;i++){
if(indeg[i] ==0){
q.push(i);
}
}
vi topo;
while(!q.empty()){
int u = q.front();
q.pop();
topo.pb(u);
for(int v:adj[u]){
indeg[v]--;
if(indeg[v] == 0){
q.push(v);
}
}
}
if(sz(topo)==n){
for(int v:topo){
cout << v << " ";
}
cout << endl;
}
else{
cout << -1 << endl;
}
}
}Editor is loading...
Leave a Comment