Pagini recente » Cod sursa (job #2688386) | Cod sursa (job #1927644) | Cod sursa (job #2793680) | Cod sursa (job #79516) | Cod sursa (job #2818005)
#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);
}
int nod = 1;
while (grad[nod]) ///gasim nodul cu grad 0
nod++;
q.push(nod);
while (!q.empty()) {
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;
}