Pagini recente » Cod sursa (job #977884) | Cod sursa (job #2252228) | Cod sursa (job #3314392) | Cod sursa (job #2360017) | Cod sursa (job #3304277)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("sortaret.in");
ofstream fout ("sortaret.out");
vector <int> g[100001];
int n, m;
vector <int> sortare_topo()
{
int in_degree[100001];
for (int nod = 1; nod <= n; nod++)
for (int vecin : g[nod])
in_degree[vecin]++;
queue <int> q;
for (int nod = 1; nod <= n; nod++)
if (in_degree[nod] == 0)
q.push(nod);
vector <int> sorted;
while (!q.empty())
{
int nod = q.front();
q.pop();
sorted.push_back(nod);
if (in_degree[nod] == 0)
for (int vecin : g[nod])
{
in_degree[vecin]--;
if (in_degree[vecin] == 0)
q.push(vecin);
}
}
return sorted;
}
int main()
{
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y;
fin >> x >> y;
g[x].push_back(y);
}
vector <int> ans = sortare_topo();
for (int nod : ans)
fout << nod << ' ';
return 0;
}