Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define MAXN 100005
- typedef vector < int > Vec;
- int N, M, deg[MAXN], nr;
- Vec G[MAXN];
- priority_queue < int > Q;
- inline void read () {
- int a, b;
- cin >> N >> M;
- while (M --) {
- cin >> a >> b;
- G[a].emplace_back (b);
- deg[b] ++;
- }
- for (int i = 1; i <= N; ++i) if (deg[i] == 0) Q.push (-i);
- }
- inline void solve () {
- Vec::iterator it;
- while (Q.size()) {
- nr = -Q.top();
- Q.pop();
- cout << nr << " ";
- for (it = G[nr].begin(); it != G[nr].end(); ++it) {
- deg[*it] --;
- if (deg[*it] == 0) Q.push (-*it);
- }
- }
- }
- int main () {
- read();
- solve();
- return 0;
- }
Add Comment
Please, Sign In to add comment