Pagini recente » Cod sursa (job #2668224) | Cod sursa (job #2526510) | Cod sursa (job #799655) | Cod sursa (job #2163272) | Cod sursa (job #2461820)
#include <bits/stdc++.h>
#define maxi 50005
using namespace std;
ifstream f("sortaret.in");
ofstream g("sortaret.out");
int grad[maxi];
vector <int> vec[maxi];
queue <int> coada;
int main()
{
int n, m;
f >> n >> m;
while (m --)
{
int x, y;
f >> x >> y;
vec[x].push_back(y);
grad[y] ++;
}
for (int nod = 1; nod <= n; ++ nod)
if (grad[nod] == 0)
{
g << nod << " ";
coada.push(nod);
}
while (!coada.empty())
{
int nod = coada.front();
coada.pop();
for (auto next : vec[nod])
{
grad[next] --;
if (grad[next] == 0)
{
g << next << " ";
coada.push(next);
}
}
}
}