Pagini recente » Istoria paginii runda/speed3 | Cod sursa (job #1608342) | Cod sursa (job #1505493) | Cod sursa (job #2840846) | Cod sursa (job #2905961)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream in("sortaret.in");
ofstream out("sortaret.out");
const int N = 50000;
vector<int> a[N + 1];
int nrp[N+1];
int main()
{
int n, m;
in >> n >> m;
for (int i = 0; i < m; i++)
{
int x, y;
in >> x >> y;
a[x].push_back(y);
nrp[y]++;
}
queue<int> q;
for (int i = 1; i <= n; i++)
{
if (nrp[i] == 0)
{
q.push(i);
}
}
while (!q.empty())
{
int x = q.front();
out << x << " ";
q.pop();
for (auto y : a[x])
{
nrp[y]--;
if (nrp[y] == 0)
{
q.push(y);
}
}
}
in.close();
out.close();
return 0;
}