fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. // // Common file
  6. // #include <ext/pb_ds/assoc_container.hpp>
  7. // // Including tree_order_statistics_node_update
  8. // #include <ext/pb_ds/tree_policy.hpp>
  9.  
  10. // using namespace __gnu_pbds;
  11. // #define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
  12. //asd
  13.  
  14. #define MOD 1000000007
  15.  
  16. void fast() {
  17. cin.sync_with_stdio(false);
  18. cout.sync_with_stdio(false);
  19. cin.tie(0);
  20. cout.tie(0);
  21. }
  22.  
  23. vector <vector<int>> graph;
  24.  
  25. int infect = 0, spread = 0;
  26.  
  27. void dfs(int i) {
  28.  
  29.  
  30. infect += min((int)graph[i].size(), 1);
  31. spread += max((int)graph[i].size() - 1, 0);
  32.  
  33. //cout << i << " " << min((int)graph[i].size(), 1) << " " << max((int)graph[i].size() - 1, 0) << "\n";
  34.  
  35. for(auto j: graph[i])
  36. dfs(j);
  37. }
  38.  
  39. int main() {
  40. //fast();
  41. //freopen("input.in", "r", stdin);
  42. //freopen("output.out", "w", stdout);
  43. int t;
  44. t = 1;
  45. cin >> t;
  46.  
  47. while(t--) {
  48. //cout << "hi\n";
  49. int n;
  50. cin >> n;
  51.  
  52. graph.assign(n+1, {});
  53. infect = 1;
  54. spread = 0;
  55.  
  56. for(int i = 2; i <= n; i ++) {
  57. int a;
  58. cin >> a;
  59. graph[a].push_back(i);
  60. }
  61.  
  62. dfs(1);
  63.  
  64. if(infect > spread) {
  65. cout << infect << "\n";
  66. } else {
  67. cout << (long long)(1 + ceil((spread + infect - 1) / 2.0)) << "\n";
  68. }
  69. }
  70. }
Success #stdin #stdout 0.01s 5504KB
stdin
Standard input is empty
stdout
1