Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int N, M;
- vector < int > G[5006];
- int cnt[5006] = {};
- int main()
- {
- queue < int > que;
- int flag = 0;
- cin >> N >> M;
- for(int i = 0; i < M; i++) {
- int a, b;
- cin >> a >> b;
- G[a - 1].push_back(b - 1); cnt[b - 1]++;
- }
- for(int i = 0; i < N; i++) {
- if(cnt[i] == 0) que.push(i);
- }
- while(!que.empty()) {
- if(que.size() >= 2) flag = 1;
- int v = que.front(); que.pop();
- cout << v + 1 << endl;
- for(int i = 0; i < G[v].size(); i++) {
- int u = G[v][i];
- cnt[u]--;
- if(cnt[u] == 0) que.push(u);
- }
- }
- cout << flag << endl;
- return (0);
- }
Add Comment
Please, Sign In to add comment