Cod sursa(job #3366030)
| Utilizator | Data | 28 septembrie 2026 18:11:57 | |
|---|---|---|---|
| Problema | Sortare topologica | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.62 kb |
#include <bits/stdc++.h>
using namespace std;
int n,m,a[101][101],viz[101],st[101],k;
void citire()
{
cin>>n>>m;
for(int i=1; i<=m; i++)
{
int x,y;
cin>>x>>y;
a[x][y]=1;
}
}
void dfs(int x)
{
viz[x]=1;
for(int i=1; i<=n; i++)
if(a[x][i] && !viz[i])
dfs(i);
st[++k]=x;
}
void rezolvare()
{
for(int i=1; i<=n; i++)
if(!viz[i])
dfs(i);
}
void afisare()
{
for(int i=k; i>=1; i--)
cout<<st[i]<<" ";
}
int main()
{
citire();
rezolvare();
afisare();
return 0;
}
