Cod sursa(job #3306691)
| Utilizator | Data | 12 august 2025 19:36:17 | |
|---|---|---|---|
| Problema | Sortare topologica | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.61 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
int n, m, x, y, stare[50005];
vector<int> v[100005], sol;
void dfs(int nod) {
stare[nod]=1;
for(auto u : v[nod]) {
if(stare[u]!=2) {
stare[u]=1;
dfs(u);
}
}
stare[nod]=2;
sol.push_back(nod);
}
int main()
{
fin >> n >> m;
for(int i=1; i<=m; i++) {
fin >> x >> y;
v[x].push_back(y);
}
dfs(1);
for(int i=sol.size()-1; i>=0; i--) {
fout << sol[i] << " ";
}
return 0;
}
