Cod sursa(job #3344423)
| Utilizator | Data | 1 martie 2026 22:58:31 | |
|---|---|---|---|
| Problema | Sortare topologica | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.62 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
int n, m, viz[50001], e[50001], k;
vector <int> G[50001];
void DFS(int nod)
{
viz[nod] = 1;
for (auto next : G[nod])
if (viz[next] == 0)
DFS(next);
e[++k] = nod;
}
int main()
{
int i, x, y;
fin >> n >> m;
for (i = 1 ; i <= m ; i++)
{
fin >> x >> y;
G[x].push_back(y);
}
for (i = 1 ; i <= n ; i++)
if (viz[i] == 0)
DFS(i);
for (i = k ; i >= 1 ; i--)
fout << e[i] << " ";
return 0;
}
