Pagini recente » Cod sursa (job #1427655) | Cod sursa (job #3226254) | Cod sursa (job #2345151) | Cod sursa (job #2360227) | Cod sursa (job #3298816)
#include <bits/stdc++.h>
using namespace std;
const int nmax = 5e4;
vector<vector<int>> dx(nmax+5);
int in[nmax+5];
int main() {
ifstream f("sortaret.in");
ofstream g("sortaret.out");
int n, m; f >> n >> m;
for(int i=0; i<m; i++) {
int x, y; f >> x >> y;
dx[x].emplace_back(y);
in[y]++;
}
queue<int> q;
for(int i=1; i<=n; i++)
if(in[i] == 0)
q.emplace(i);
vector<int> topsort;
while(!q.empty()) {
int now = q.front();
q.pop();
topsort.emplace_back(now);
for(auto i : dx[now]) {
in[i]--;
if(in[i] == 0) q.emplace(i);
}
}
for(auto i : topsort)
g << i << " ";
return 0;
}