Pagini recente » Cod sursa (job #2215911) | Cod sursa (job #2393164) | Cod sursa (job #536242) | Cod sursa (job #1387243) | Cod sursa (job #2905978)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int N = 5e4;
vector <int> a[N+1];
int nrp[N+1];
int main()
{
ifstream in("sortaret.in");
ofstream out("sortaret.out");
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]++;
}
in.close();
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);
}
}
}
out.close();
return 0;
}