Advertisement
a53

Nivele11

a53
May 15th, 2022
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. ifstream fin("nivele11.in");
  4. ofstream fout("nivele11.out");
  5. int n , k, t[105], nivel[105], rad, niv;
  6.  
  7. void dfs(int k, int nv)
  8. {
  9. nivel[k] = nv;
  10. for(int i=1 ; i<=n; ++i)
  11. if(t[i] == k)
  12. dfs(i, nv+1);
  13. }
  14. int main()
  15. {
  16. int i;
  17. fin>>n;
  18. for(i=1; i<=n; i++)
  19. {
  20. fin>>t[i];
  21. if(!t[i])
  22. rad=i;
  23. }
  24. dfs(rad,1);
  25. for(i=1; i<=n; i++)
  26. {
  27. if(nivel[i]>niv)
  28. niv=nivel[i];
  29. }
  30. for(k=1; k<=niv; ++k)
  31. {
  32. for(int i=1; i<=n; ++i)
  33. if(nivel[i]==k)
  34. fout<<i<<" ";
  35. fout<<'\n';
  36. }
  37. return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement