Pagini recente » Cod sursa (job #130384) | Cod sursa (job #2612150) | Cod sursa (job #2401910) | Cod sursa (job #3210871) | Cod sursa (job #2892780)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int N = 50000;
vector <int> a[N+1];
int nrp[N+1];
int main()
{
ifstream f("sortaret.in");
ofstream g("sortaret.out");
int m, n, i;
f >> n >> m;
for (i = 0; i < m; i++)
{
int x, y;
f >> x >> y;
a[x].push_back(y);
nrp[y]++;
}
///initializarea cozii
queue <int> q;
for (i = 1; i <= n; i++)
{
if (nrp[i] == 0)
{
q.push(i);
}
}
while (!q.empty())
{
int x = q.front();
q.pop();
g << x << " ";
for (auto y: a[x])
{
nrp[y]--;
if (nrp[y] == 0)
{
q.push(y);
}
}
}
f.close();
g.close();
return 0;
}