Pagini recente » Cod sursa (job #317999) | Cod sursa (job #485195) | Cod sursa (job #915441) | Cod sursa (job #1950889) | Cod sursa (job #2927194)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
const int N = 1e5 + 1;
vector <int> L[N];
priority_queue <int, vector<int>, greater<int> > pq;
vector <int> indeg;
int n;
void Read()
{
ifstream fin ("sortaret.in");
int m;
fin >> n >> m;
indeg.resize(n + 1, 0);
while (m--)
{
int x, y;
fin >> x >> y;
L[x].push_back(y);
indeg[y]++;
}
fin.close();
}
void TopSort()
{
ofstream fout ("sortaret.out");
for (int i = 1; i <= n; i++)
if (indeg[i] == 0)
pq.push(i);
while (!pq.empty())
{
int nod = pq.top();
fout << nod << " ";
pq.pop();
for (auto& next : L[nod])
{
indeg[next]--;
if (indeg[next] == 0) pq.push(next);
}
}
fout.close();
}
int main()
{
Read();
TopSort();
}