Pagini recente » Cod sursa (job #2160280) | Cod sursa (job #1486787) | Cod sursa (job #47140) | Cod sursa (job #2099641) | Cod sursa (job #2216009)
#include <fstream>
#include <queue>
using namespace std;
ifstream fin ("sortaret.in");
ofstream fout ("sortaret.out");
int n, m, x, y, grad[50001];
queue <int> nodes, g[50001];
int main() {
fin >> n >> m;
for (int i = 1; i <= m; ++i) {
fin >> x >> y;
g[x].push(y);
++grad[y];
}
for (int i = 1; i <= n; ++i) {
if (grad[i] == 0)
nodes.push(i);
}
while (!nodes.empty()) {
int nod = nodes.front();
while (!g[nod].empty()) {
if (--grad[g[nod].front()] == 0)
nodes.push(g[nod].front());
g[nod].pop();
}
fout << nod << ' ';
nodes.pop();
}
}