Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 2e4 + 5;
- vector<int> adj[N];
- int inDeg[N];
- int main(){
- int nVertex, nEdge;
- scanf("%d%d", &nVertex, &nEdge);
- for(int i = 1; i <= nEdge; ++i){
- int u, v;
- scanf("%d%d", &u, &v);
- adj[u].push_back(v);
- ++inDeg[v];
- }
- priority_queue<int, vector<int>, greater<int>> pq;
- for(int i = 1; i <= nVertex; ++i){
- if(inDeg[i] == 0){
- pq.push(i);
- }
- }
- vector<int> topo;
- while(!pq.empty()){
- int u = pq.top();
- pq.pop();
- topo.push_back(u);
- for(int v : adj[u]){
- --inDeg[v];
- if(inDeg[v] == 0){
- pq.push(v);
- }
- }
- }
- if(topo.size() < nVertex){
- cout << "-1";
- } else {
- for(int x : topo){
- cout << x << ' ';
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement