Pagini recente » Borderou de evaluare (job #538723) | Cod sursa (job #3235615)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
int n, m, i, j, gi[50002];
vector<int> a[50002];
queue<int> q;
int main() {
fin >> n >> m;
while(m--) {
fin >> i >> j;
a[i].push_back(j);
gi[j]++;
}
for(i = 1; i <= n; i++) {
if(gi[i] == 0) q.push(i);
}
while(!q.empty()) {
int nod = q.front();
q.pop();
fout << nod << " ";
for(auto it : a[nod]) {
gi[it]--;
if(gi[it] == 0) q.push(it);
}
}
return 0;
}