Pagini recente » 100m | Diferente pentru planificare/sedinta_20070507 intre reviziile 15 si 16 | Profil Mihai_Ariton | Florian Marcu | Cod sursa (job #2569076)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
const int MAX_N = 50000;
int n, m;
int grad[MAX_N + 5];
vector<int> G[MAX_N + 5], ans;
queue<int> q;
int main() {
fin >> n >> m;
for (int i = 1; i <= m; i++){
int x, y;
fin >> x >> y;
G[x].push_back(y);
grad[y]++;
}
for (int i = 1; i <= n; i++)
if (grad[i] == 0)
q.push(i);
while (!q.empty()) {
int u = q.front();
ans.push_back(u);
q.pop();
for (int v : G[u]){
grad[v]--;
if (grad[v] == 0)
q.push(v);
}
}
for (int i : ans)
fout << i << ' ';
return 0;
}