Pagini recente » Cod sursa (job #1082237) | Cod sursa (job #3233806) | Borderou de evaluare (job #2116714) | Cod sursa (job #439145) | Cod sursa (job #2877377)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
const int nmax = 5e4 + 5;
int n, m, cnt[nmax];
vector <int> v[nmax], ans;
queue <int> q;
void sortare_top() {
while(!q.empty()) {
int x = q.front();
q.pop();
ans.push_back(x);
for (auto it = v[x].begin(); it != v[x].end(); ++it) {
--cnt[*it];
if (!cnt[*it])
q.push(*it);
}
}
}
int main()
{
fin >> n >> m;
for (int i = 1; i <= m; ++i) {
int x, y;
fin >> x >> y;
v[x].push_back(y);
++cnt[y];
}
for (int i = 1; i <= n; ++i) {
if (!cnt[i])
q.push(i);
}
sortare_top();
for (int i : ans)
fout << i << ' ';
return 0;
}