a53

Competitie_ANDRU_DFS

a53
Dec 13th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. ifstream fin("competitie.in");
  5. ofstream fout("competitie.out");
  6. int n, m, a[1005][1005], x, y, viz[1005], postordine[1005], nr;
  7. void DFS(int x)
  8. {
  9. int i;
  10. viz[x]=1;
  11. for(i=1;i<=a[x][0];i++)
  12. if(!viz[a[x][i]])
  13. DFS(a[x][i]);
  14. postordine[++nr]=x;
  15. }
  16. int main()
  17. {
  18. int i;
  19. fin>>n>>m;
  20. for(i=0;i<m;i++)
  21. {
  22. fin>>x>>y;
  23. a[x][0]++;
  24. a[x][a[x][0]]=y;
  25. }
  26. for(i=n;i>0;i--)
  27. if(!viz[i])
  28. DFS(i);
  29. for(i=n;i>0;i--)
  30. fout<<postordine[i]<<" ";
  31. return 0;
  32. }
Add Comment
Please, Sign In to add comment