Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int getResult(int &count, const int index, unordered_map<int, int> &result, const int directorId)
  8. {
  9.  
  10. if (index != directorId)
  11. {
  12. getResult(count, result[index], result, directorId);
  13. count++;
  14. }
  15.  
  16. return count;
  17. }
  18.  
  19. int main()
  20. {
  21. int N;
  22.  
  23. cin >> N;
  24. unordered_map<int, int> result;
  25.  
  26.  
  27. int directorId(0);
  28. int i = 0;
  29. while (i < N)
  30. {
  31. int tempInd;
  32. cin >> tempInd;
  33.  
  34. if (tempInd != 0)
  35. result[i] = tempInd - 1;
  36. else
  37. {
  38. result[i] = tempInd;
  39. directorId = i;
  40. }
  41.  
  42. i++;
  43. }
  44.  
  45. vector<int> res;
  46. int count = 0;
  47.  
  48. for (int j = N - 1; j >= 0; j--)
  49. {
  50. res.push_back(getResult(count, j, result, directorId));
  51. count = 0;
  52. }
  53.  
  54.  
  55. for (int j = res.size() - 1; j >= 0; j--)
  56. {
  57. if (j != 0)
  58. cout << res[j] << " ";
  59. else
  60. cout << res[j];
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement