Cod sursa(job #2705387)
| Utilizator | Data | 12 februarie 2021 15:13:53 | |
|---|---|---|---|
| Problema | Sortare topologica | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.52 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
int n,m;
vector<int> g[50001];
list<int> ord;
bool viz[50001];
void dfs(int x)
{
viz[x] = 1;
for(int i : g[x])
if(!viz[i])
dfs(i);
ord.push_front(x);
}
int main()
{
int x, y;
fin >> n >> m;
for(int i = 1; i <= m; i++){
fin >> x >> y;
g[x].push_back(y);
}
dfs(1);
for(int i : ord)
fout << i << " ";
return 0;
}