Pagini recente » Cod sursa (job #1370216) | Cod sursa (job #2162305) | Cod sursa (job #845111) | Cod sursa (job #2820361) | Cod sursa (job #3242872)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int NMAX = 5e4 + 1;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
int n, m, gr[NMAX];
vector<int> G[NMAX], sol;
queue<int> q;
void citire()
{
int x, y;
fin >> n >> m;
while(m--)
{
fin >> x >> y;
G[x].push_back(y);
++gr[y];
}
}
void solutie()
{
for(int i = 1; i <= n; ++i)
if(!gr[i])
q.push(i);
while(!q.empty())
{
int x = q.front();
q.pop();
sol.push_back(x);
for(const auto &y : G[x])
{
if(!(--gr[y]))
q.push(y);
}
}
}
void afisare()
{
for(const auto &x : sol)
fout << x << ' ';
}
int main()
{
citire();
solutie();
afisare();
return 0;
}