ProgMe

Task for graph

May 23rd, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. vector <set <int>> graph;
  6.  
  7.  
  8. int main()
  9. {
  10. int n;
  11. cin >> n;
  12. int a [n + 1];
  13. for (int i = 1; i <= n; i++)
  14. cin >> a [i];
  15. graph.resize(n + 1);
  16. for (int i = 1; i <= n; i++)
  17. {
  18. if (a [i] != -1)
  19. graph [a [i]].insert(i);
  20. }
  21. int ans = 0;
  22. int x = 0;
  23. bool visited [n + 1] = {};
  24. while (x != n)
  25. {
  26. for (int i = 1; i <= n; i++)
  27. {
  28. if (!visited [i] && graph [i].empty())
  29. {
  30. if (a [i] != -1)
  31. graph [a [i]].erase(i);
  32. visited [i] = 1;
  33. x++;
  34. }
  35. }
  36. ans++;
  37. }
  38. cout << ans;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment