Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
- using namespace std;
- int n, m, cnt[32001] = {};
- vector<int> v[32001];
- void sorting() {
- int result[32001] = {};
- queue<int> q;
- for (int i = 1; i <= n; i++) {
- if (cnt[i] == 0) q.push(i);
- }
- for (int i = 1; i <= n; i++) {
- int x = q.front();
- q.pop();
- result[i] = x;
- for (int g = 0; g < v[x].size(); g++) {
- int y = v[x][g];
- if (--cnt[y] == 0) q.push(y);
- }
- }
- for (int i = 1; i <= n; i++) cout << result[i] << " ";
- }
- int main() {
- fastio;
- cin >> n >> m;
- for (int i = 0; i < m; i++) {
- int a, b;
- cin >> a >> b;
- v[a].push_back(b);
- cnt[b]++;
- }
- sorting();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement