Pagini recente » Cod sursa (job #952737) | Cod sursa (job #3347442) | Diferente pentru aib intre reviziile 26 si 14 | Cod sursa (job #3345654) | Cod sursa (job #3351111)
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 50000;
vector<int> a[NMAX + 1];
int grad_intern[NMAX + 1];
queue<int> q;
vector<int> raspuns;
ifstream in("sortaret.in");
ofstream out("sortaret.out");
int main()
{
int n, m;
in >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y;
in >> x >> y;
a[x].push_back(y);
grad_intern[y]++;
}
for(int i = 1; i <= n; i++)
{
if(grad_intern[i] == 0)
{
q.push(i);
}
}
while(!q.empty())
{
int x = q.front();
q.pop();
raspuns.push_back(x);
for(auto y : a[x])
{
grad_intern[y]--;
if(grad_intern[y] == 0)
{
q.push(y);
}
}
}
for(int x : raspuns)
{
out << x << " ";
}
in.close();
out.close();
return 0;
}