Pagini recente » Cod sursa (job #636966) | Cod sursa (job #723987) | Cod sursa (job #2220660) | Cod sursa (job #1793808) | Cod sursa (job #2855099)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin ("sortaret.in");
ofstream fout ("sortaret.out");
const int MAXN = 500003, MAXM = 100003;
int n, m;
vector<int> adj[MAXN], result;
int gradi[MAXN];
queue<int> q;
void kahn()
{
for(int i = 1; i <= n; i++)
if(!gradi[i])
q.push(i);
while(!q.empty())
{
int crt = q.front();
q.pop();
result.push_back(crt);
for(auto &it : adj[crt])
{
gradi[it]--;
if(!gradi[it]) q.push(it);
}
}
}
int main()
{
fin >> n >> m;
for(int i = 0, x, y; i < m; i++)
{
fin >> x >> y;
adj[x].push_back(y);
gradi[y]++;
}
kahn();
if(result.size() == n)
for(int i = 0; i < result.size(); i++)
fout << result[i] << " ";
else
fout << "nu exista";
return 0;
}