Pagini recente » Cod sursa (job #1913656) | Cod sursa (job #2624667) | Cod sursa (job #3267629) | Cod sursa (job #6020) | Cod sursa (job #2666295)
#include <fstream>
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
vector < vector <int> > graf;
queue <int> lista, sol;
int main()
{
int n, m, x, y, i;
fin >> n >> m;
graf = vector < vector <int> > (n + 1);
int grad_intern[100001];
for(i = 0; i < m; i++)
{
fin >> x >> y;
graf[x].push_back(y);
grad_intern[y]++;
}
for(i = 1; i <= n; i++)
if(grad_intern[i] == 0)
lista.push(i);
while(!lista.empty())
{
int nod_curent = lista.front();
sol.push(nod_curent);
lista.pop();
int n_nod = graf[nod_curent].size();
for(i = 0; i < n_nod; i++)
{
grad_intern[graf[nod_curent][i]]--;
if(grad_intern[graf[nod_curent][i]] == 0)
lista.push(graf[nod_curent][i]);
}
}
while(!sol.empty())
{
fout << sol.front() << " ";
sol.pop();
}
}