Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 1e5 + 10;
- vector <int> g[N];
- int color[N], indeg[N];
- bool vs[N];
- bool cycle(int u){
- if(color[u] == 1)
- return true;
- if(color[u] == 2)
- return false;
- color[u] = 1;
- for(auto v: g[u]){
- if(cycle(v))
- return true;
- }
- color[u] = 2;
- return false;
- }
- int main(){
- int n, m;
- scanf("%d %d", &n, &m);
- for(int i=1;i<=m;i++){
- int u, v;
- scanf("%d %d", &u, &v);
- g[u].push_back(v);
- indeg[v] ++;
- }
- if(cycle(1)){
- printf("no");
- return 0;
- }
- queue <int> q;
- for(int u=1;u<=n;u++){
- if(indeg[u] == 0)
- q.push(u);
- }
- while(!q.empty()){
- int u = q.front();
- q.pop();
- printf("%d\n", u);
- for(auto v: g[u]){
- indeg[v] --;
- if(indeg[v] == 0 and !vs[v])
- q.push(v);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement