cses

My solution for the coding challenges of cses.fi
git clone https://git.tronto.net/cses
Download | Log | Files | Refs | README

subordinates_1674.cpp (441B)


      1 #include <iostream>
      2 #include <vector>
      3 
      4 int f(const std::vector<std::vector<int>>& a, std::vector<int>& b, int i) {
      5 	for (auto x : a[i]) 
      6 		b[i] += f(a, b, x) + 1;
      7 	return b[i];
      8 }
      9 
     10 int main() {
     11 	int n, x;
     12 	std::cin >> n;
     13 	std::vector<std::vector<int>> a(n);
     14 	for (int i = 1; i < n; i++) {
     15 		std::cin >> x;
     16 		a[x-1].push_back(i);
     17 	}
     18 	std::vector<int> b(n, 0);
     19 	f(a, b, 0);
     20 	for (auto y : b)
     21 		std::cout << y << " ";
     22 	std::cout << std::endl;
     23 }