Untitled

mail@pastecode.io avatar
unknown
plain_text
18 days ago
1.2 kB
5
Indexable
Never
#include <bits/stdc++.h>

using namespace std;

void File() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
#ifdef MON
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("errors.txt", "w", stderr);
#else
#endif
}

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
char di[] = {'D', 'U', 'R', 'L'};

int main() {
    File();
    int n, m;
    cin >> n >> m;
    vector<vector<int>> adj(n + 1);
    vector<int> inDeg(n + 1);
    for (int i = 0; i < m; ++i) {
        int u, v;
        cin >> u >> v;
        inDeg[v]++;
        adj[u].push_back(v);
    }
    priority_queue<int, vector<int>, greater<>> q;
    for (int i = 1; i <= n; ++i)
        if(inDeg[i] == 0) q.push(i);
    vector<int> ord;
    while(!q.empty()) {
        int u = q.top();
        q.pop();
        ord.push_back(u);
        for(auto &v: adj[u]) {
            inDeg[v]--;
            if(inDeg[v] == 0) q.push(v);
        }
    }
    if (ord.size() == n) {
        for(auto &x: ord) cout << x << ' ';
    } else {
        cout << "Sandro fails.";
    }
    return 0;
}
Leave a Comment