Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <unordered_map>
- #include <vector>
- using namespace std;
- int getResult(int &count, const int index, unordered_map<int, int> &result, const int directorId)
- {
- if (index != directorId)
- {
- getResult(count, result[index], result, directorId);
- count++;
- }
- return count;
- }
- int main()
- {
- int N;
- cin >> N;
- unordered_map<int, int> result;
- int directorId(0);
- int i = 0;
- while (i < N)
- {
- int tempInd;
- cin >> tempInd;
- if (tempInd != 0)
- result[i] = tempInd - 1;
- else
- {
- result[i] = tempInd;
- directorId = i;
- }
- i++;
- }
- vector<int> res;
- int count = 0;
- for (int j = N - 1; j >= 0; j--)
- {
- res.push_back(getResult(count, j, result, directorId));
- count = 0;
- }
- for (int j = res.size() - 1; j >= 0; j--)
- {
- if (j != 0)
- cout << res[j] << " ";
- else
- cout << res[j];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement