Submission #69085083


Source Code Expand

/*
Author : Mikira
9/6/2025 9:20:09 PM
*/

#include "bits/stdc++.h"
using namespace std;

typedef long long LL;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> PII;
typedef pair<int, int> pii;
typedef pair<LL, LL> PLL;
typedef pair<LL, LL> pll;
typedef long double ld;

#define rep(i, a, b) for(int i = a; i < int(b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define popf pop_front
#define pf push_front
#define popb pop_back
#define pb push_back
#define fi first
#define se second

const double EPS = 1e-9;
const int INFMEM = 63;

// Do dir^1 to get reverse direction
const int dx[8] = {0,0,1,-1,1,-1,1,-1};
const int dy[8] = {1,-1,0,0,1,-1,-1,1};
const char dch[4] = {'R','L','D','U'};

// Do (dir + 2)%4 to get reverse direction
// const int dx[8] = {-1,0,1,0,-1,1,1,-1};
// const int dy[8] = {0,1,0,-1,1,1,-1,-1};
// const char dch[4] = {'U','R','D','L'};
const double PI = 3.141592653589793;

inline void fasterios(){
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);
}
#define endl '\n'
const int MOD = 1000000007;
// const int MOD = 998244353;

const int kN = 5e5;
vector <int> edge[kN + 5];
int memo[kN + 5];
int subtreeSize[kN + 5];
void dp(int pos) {
  subtreeSize[pos] = 1;
  vector <int> child;
  int maxMemoChild = -1;
  trav(nx, edge[pos]) {
    dp(nx);
    child.pb(nx);
    subtreeSize[pos] += subtreeSize[nx];
    if(maxMemoChild == -1) maxMemoChild = nx;
    if(memo[maxMemoChild] < memo[nx]) maxMemoChild = nx;
  }
  memo[pos] = 1;
  if(maxMemoChild != -1) {
    int maxDp = 0;
    trav(nx, edge[pos]) {
      if(nx == maxMemoChild) continue;
      int currentTaken = min(subtreeSize[nx], memo[maxMemoChild]);
      maxDp += currentTaken;    
    }
    memo[pos] += max(0, memo[maxMemoChild] - maxDp);
  }
} 
int solve(){
  int n; cin >> n;
  for(int i = 1;i <= n;++i) {
    edge[i].clear();
  }
  for(int i = 2;i <= n;i++) {
    int p; cin >> p;
    edge[p].pb(i);
  }
  dp(1);
  cout << (n - memo[1]) / 2 << endl;
  return 0;
}

int main(){
  fasterios();
  int testCases = 1, tc = 0;
  cin >> testCases;
  while(++tc <= testCases){
    // cout << "Case #" << tc << ": ";
    solve();
  }
}

Submission Info

Submission Time
Task D - Non-Ancestor Matching
User hocky
Language C++ 20 (gcc 12.2)
Score 600
Code Size 2358 Byte
Status AC
Exec Time 195 ms
Memory 97308 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 1
AC × 87
Set Name Test Cases
Sample 00_sample_00.txt
All 00_sample_00.txt, 01_small_00.txt, 01_small_01.txt, 01_small_02.txt, 02_handmade_00.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt, 02_handmade_05.txt, 02_handmade_06.txt, 02_handmade_07.txt, 02_handmade_08.txt, 02_handmade_09.txt, 02_handmade_10.txt, 02_handmade_11.txt, 02_handmade_12.txt, 02_handmade_13.txt, 02_handmade_14.txt, 02_handmade_15.txt, 02_handmade_16.txt, 02_handmade_17.txt, 02_handmade_18.txt, 02_handmade_19.txt, 02_handmade_20.txt, 02_handmade_21.txt, 02_handmade_22.txt, 02_handmade_23.txt, 02_handmade_24.txt, 02_handmade_25.txt, 02_handmade_26.txt, 02_handmade_27.txt, 02_handmade_28.txt, 02_handmade_29.txt, 02_handmade_30.txt, 02_handmade_31.txt, 02_handmade_32.txt, 02_handmade_33.txt, 02_handmade_34.txt, 02_handmade_35.txt, 02_handmade_36.txt, 02_handmade_37.txt, 02_handmade_38.txt, 02_handmade_39.txt, 02_handmade_40.txt, 02_handmade_41.txt, 02_handmade_42.txt, 02_handmade_43.txt, 02_handmade_44.txt, 02_handmade_45.txt, 03_random_00.txt, 03_random_01.txt, 03_random_02.txt, 03_random_03.txt, 03_random_04.txt, 03_random_05.txt, 03_random_06.txt, 03_random_07.txt, 03_random_08.txt, 03_random_09.txt, 03_random_10.txt, 03_random_11.txt, 03_random_12.txt, 03_random_13.txt, 03_random_14.txt, 03_random_15.txt, 03_random_16.txt, 03_random_17.txt, 03_random_18.txt, 03_random_19.txt, 03_random_20.txt, 03_random_21.txt, 03_random_22.txt, 03_random_23.txt, 03_random_24.txt, 03_random_25.txt, 03_random_26.txt, 04_from_path_subtree_01.txt, 04_from_path_subtree_02.txt, 04_from_path_subtree_03.txt, 04_from_path_subtree_04.txt, 04_from_path_subtree_05.txt, 04_from_path_subtree_06.txt, 04_from_path_subtree_07.txt, 04_from_path_subtree_08.txt, 04_from_path_subtree_09.txt, 04_from_path_subtree_10.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 3 ms 3520 KiB
01_small_00.txt AC 26 ms 3448 KiB
01_small_01.txt AC 32 ms 3520 KiB
01_small_02.txt AC 33 ms 3448 KiB
02_handmade_00.txt AC 23 ms 11140 KiB
02_handmade_01.txt AC 86 ms 97176 KiB
02_handmade_02.txt AC 72 ms 58120 KiB
02_handmade_03.txt AC 69 ms 58196 KiB
02_handmade_04.txt AC 52 ms 21144 KiB
02_handmade_05.txt AC 52 ms 21096 KiB
02_handmade_06.txt AC 104 ms 55548 KiB
02_handmade_07.txt AC 156 ms 27136 KiB
02_handmade_08.txt AC 146 ms 27128 KiB
02_handmade_09.txt AC 148 ms 27176 KiB
02_handmade_10.txt AC 36 ms 3512 KiB
02_handmade_11.txt AC 36 ms 3544 KiB
02_handmade_12.txt AC 36 ms 3656 KiB
02_handmade_13.txt AC 36 ms 3448 KiB
02_handmade_14.txt AC 36 ms 3516 KiB
02_handmade_15.txt AC 36 ms 3528 KiB
02_handmade_16.txt AC 36 ms 3596 KiB
02_handmade_17.txt AC 36 ms 3608 KiB
02_handmade_18.txt AC 129 ms 27084 KiB
02_handmade_19.txt AC 39 ms 3660 KiB
02_handmade_20.txt AC 38 ms 3640 KiB
02_handmade_21.txt AC 39 ms 3704 KiB
02_handmade_22.txt AC 38 ms 3704 KiB
02_handmade_23.txt AC 33 ms 3632 KiB
02_handmade_24.txt AC 34 ms 3452 KiB
02_handmade_25.txt AC 34 ms 3608 KiB
02_handmade_26.txt AC 33 ms 3632 KiB
02_handmade_27.txt AC 34 ms 3608 KiB
02_handmade_28.txt AC 34 ms 3516 KiB
02_handmade_29.txt AC 34 ms 3620 KiB
02_handmade_30.txt AC 36 ms 3720 KiB
02_handmade_31.txt AC 75 ms 61628 KiB
02_handmade_32.txt AC 76 ms 66392 KiB
02_handmade_33.txt AC 85 ms 97308 KiB
02_handmade_34.txt AC 85 ms 97156 KiB
02_handmade_35.txt AC 86 ms 97244 KiB
02_handmade_36.txt AC 86 ms 97108 KiB
02_handmade_37.txt AC 86 ms 97172 KiB
02_handmade_38.txt AC 86 ms 97056 KiB
02_handmade_39.txt AC 86 ms 97180 KiB
02_handmade_40.txt AC 87 ms 96804 KiB
02_handmade_41.txt AC 91 ms 91008 KiB
02_handmade_42.txt AC 87 ms 84676 KiB
02_handmade_43.txt AC 85 ms 78436 KiB
02_handmade_44.txt AC 82 ms 72256 KiB
02_handmade_45.txt AC 30 ms 15924 KiB
03_random_00.txt AC 180 ms 27088 KiB
03_random_01.txt AC 183 ms 27208 KiB
03_random_02.txt AC 195 ms 27056 KiB
03_random_03.txt AC 195 ms 27180 KiB
03_random_04.txt AC 191 ms 27140 KiB
03_random_05.txt AC 183 ms 27132 KiB
03_random_06.txt AC 182 ms 27084 KiB
03_random_07.txt AC 179 ms 27056 KiB
03_random_08.txt AC 186 ms 27060 KiB
03_random_09.txt AC 190 ms 27104 KiB
03_random_10.txt AC 24 ms 10492 KiB
03_random_11.txt AC 25 ms 9616 KiB
03_random_12.txt AC 28 ms 10260 KiB
03_random_13.txt AC 31 ms 10116 KiB
03_random_14.txt AC 37 ms 3496 KiB
03_random_15.txt AC 36 ms 3436 KiB
03_random_16.txt AC 37 ms 3500 KiB
03_random_17.txt AC 37 ms 3572 KiB
03_random_18.txt AC 37 ms 3500 KiB
03_random_19.txt AC 98 ms 40208 KiB
03_random_20.txt AC 94 ms 39632 KiB
03_random_21.txt AC 96 ms 39880 KiB
03_random_22.txt AC 116 ms 50500 KiB
03_random_23.txt AC 111 ms 50364 KiB
03_random_24.txt AC 117 ms 50292 KiB
03_random_25.txt AC 110 ms 50240 KiB
03_random_26.txt AC 110 ms 50300 KiB
04_from_path_subtree_01.txt AC 101 ms 63872 KiB
04_from_path_subtree_02.txt AC 91 ms 76804 KiB
04_from_path_subtree_03.txt AC 89 ms 84536 KiB
04_from_path_subtree_04.txt AC 101 ms 69244 KiB
04_from_path_subtree_05.txt AC 87 ms 81080 KiB
04_from_path_subtree_06.txt AC 88 ms 92076 KiB
04_from_path_subtree_07.txt AC 89 ms 75916 KiB
04_from_path_subtree_08.txt AC 90 ms 82604 KiB
04_from_path_subtree_09.txt AC 102 ms 65932 KiB
04_from_path_subtree_10.txt AC 87 ms 92344 KiB