Pagini recente » Cod sursa (job #2348413) | Cod sursa (job #3128159)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
using llx = long long;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
#define NMAX 50000
vector <int> v[NMAX+1];
int grad[NMAX+1], sortat[NMAX+1];
int main()
{
int n, m, i, j, x, y;
fin >> n >> m;
for (i = 1; i<=m; i++)
{
fin >> x >> y;
v[x].push_back(y);
grad[y]++;
}
for (i = 1; i<=n; i++)
if (grad[i] == 0)
sortat[++sortat[0]] = i;
for (i = 1; i<=n; i++)
{
x = sortat[i];
for (const auto &nv : v[x]) // "Range-based for statement" -> https://learn.microsoft.com/en-us/cpp/cpp/range-based-for-statement-cpp?view=msvc-170
{
grad[nv]--;
if (grad[nv] == 0)
sortat[++sortat[0]] = nv;
}
}
for (i = 1; i<=n; i++)
fout << sortat[i] << ' ';
return 0;
}