Pagini recente » Cod sursa (job #2583315) | Cod sursa (job #666866) | Cod sursa (job #1328209) | Cod sursa (job #767399) | Cod sursa (job #2952659)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
vector<int> v[50001];
int grad[50001];
queue <int> q;
int n, m;
void citire()
{
fin >> n >> m;
int x, y;
for(int i = 0; i < m; ++i)
{
fin >> x >> y;
v[x].push_back(y);
grad[y]++;
}
}
void sortare()
{
for(int i = 1; i <= n; ++i)
if(!grad[i])
q.push(i);
while(!q.empty())
{
int el = q.front();
q.pop();
fout << el << ' ';
for(int i = 0; i < v[el].size(); ++i)
{
grad[v[el][i]]--;
if(grad[v[el][i]] == 0)
q.push(v[el][i]);
}
}
}
int main()
{
citire();
sortare();
return 0;
}