Pagini recente » Cod sursa (job #465120) | Cod sursa (job #1360550) | Cod sursa (job #1018601) | Cod sursa (job #3181527) | Cod sursa (job #2818007)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("sortaret.in");
ofstream fout ("sortaret.out");
const int NMAX = 5e4+5;
vector<int> v[NMAX];
queue <int> q;
vector<int> sol;
int grad[NMAX];
int main() {
int n, m;
fin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y;
fin >> x >> y;
grad[y]++;
v[x].push_back(y);
}
for (int i = 1; i <= n; i++)
if (grad[i] == 0)
q.push(i);
while (!q.empty()) {
int nod = q.front();
q.pop();
sol.push_back(nod);
for (int fiu : v[nod]) {
grad[fiu]--;
if (grad[fiu] == 0)
q.push(fiu);
}
}
for (int x : sol)
fout << x << " ";
return 0;
}