Cod sursa(job #3205533)
| Utilizator | Data | 19 februarie 2024 21:53:48 | |
|---|---|---|---|
| Problema | Sortare topologica | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.55 kb |
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
const int NMAX = 50001;
vector<int> g[NMAX], sol;
int v[NMAX], n, m;
void dfs(int k){
v[k] = 1;
for(auto vec : g[k]) if(!v[vec]) dfs(vec);
sol.push_back(k);
}
int main(){
fin >> n >> m;
for(int i = 1; i <= m; i++){
int x, y;
fin >> x >> y;
g[x].push_back(y);
}
for(int i = 1; i <= n; i++) if(!v[i]) dfs(i);
for(auto i : sol) fout << i << ' ';
return 0;
}