Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <queue>
- #include <string.h>
- #include <math.h>
- #include <map>
- #include <set>
- #include <vector>
- #define MOD 998244353
- #define MAX_N 100005
- #define f first
- #define s second
- using namespace std;
- typedef pair<int, int> ii;
- typedef pair<ii, int> pii;
- long long mod_pow(long long num, long long power)
- {
- long long test;
- for(test = 1; power; power >>= 1)
- {
- if (power & 1)
- test = (test * num) % MOD;
- num = (num * num) % MOD;
- }
- return test;
- }
- vector<int> con[MAX_N];
- int vis[MAX_N], in[MAX_N];
- int N, M;
- void solve() {
- cin >> N >> M;
- int a, b;
- for (int i = 0; i < M; i ++) {
- cin >> a >> b;
- con[b - 1].push_back(a - 1);
- }
- for (int i = 0; i < N; i ++) {
- for (int j = 0; j < con[i].size(); j ++) {
- in[con[i][j]] ++;
- }
- }
- multiset<int> s;
- for (int i = 0; i < N; i++)
- if (in[i] == 0)
- s.insert(-i);
- int cnt = 0;
- vector<int> top_order;
- while (!s.empty()) {
- int u = -(*s.begin());
- s.erase(s.begin());
- top_order.push_back(u);
- // Iterate through all its neighbouring nodes
- // of erased node u and decrease their in-degree
- // by 1
- for (int i = 0; i < con[u].size(); i ++) {
- in[con[u][i]] --;
- if (in[con[u][i]] == 0) {
- s.insert(-con[u][i]);
- }
- }
- cnt++;
- }
- for (int i = top_order.size() - 1; i >= 0; i --)
- cout << (top_order[i] + 1) << " ";
- cout << endl;
- return;
- }
- int main() {
- ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
- int T;
- T = 1;
- for (int i = 0; i < T; i ++) {
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement