Cod sursa(job #2707401)
| Utilizator | Data | 16 februarie 2021 21:56:04 | |
|---|---|---|---|
| Problema | Sortare topologica | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.54 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream f("sortaret.in"); ofstream g("sortaret.out");
int n, m, k, viz[50005], sol[50005];;
vector<int>G[50005];
void Dfs(int x)
{ viz[x]=1;
for(unsigned int j, i=0; i<G[x].size(); ++i)
{ j=G[x][i];
if(!viz[j]) Dfs(j);
}
sol[++k]=x;
}
int main()
{ f>>n>>m;
for(int a, b, i=1; i<=m; ++i) {f>>a>>b; G[a].push_back(b);}
for(int i=1; i<=n; ++i)
if(!viz[i]) Dfs(i);
for(int i=n; i; --i) g<<sol[i]<<' ';
g.close(); f.close(); return 0;
}
