Submission #69074508


Source Code Expand

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
typedef long long int ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<vvvl> vvvvl;
typedef vector<bool> vb;      
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;
typedef vector<vvvb> vvvvb;
typedef pair<ll,ll> pl;
typedef pair<ll,pl> ppl;
typedef pair<ll,ppl> pppl;
typedef pair<ll,pppl> pppppl;
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) begin(a),end(a)
#define sz(a) (int)(a).size()
#define F first
#define S second
#define bs(A,x) binary_search(all(A),x)
#define lb(A,x) (ll)(lower_bound(all(A),x)-A.begin())
#define ub(A,x) (ll)(upper_bound(all(A),x)-A.begin())
#define cou(A,x) (ll)(upper_bound(all(A),x)-lower_bound(all(A),x))
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}
//*
using mint=modint998244353;
const ll mod=998244353;
//*/
/*
using mint=modint1000000007;
const ll mod=1000000007;
//*/
//using mint=modint;
//*
typedef vector<mint> vm;
typedef vector<vm> vvm;
typedef vector<vvm> vvvm;
typedef vector<vvvm> vvvvm;
ostream&operator<<(ostream&os,mint a){os<<a.val();return os;}
istream&operator>>(istream&is,mint&a){int x;is>>x;a=mint(x);return is;}
//*/
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>p){os<<p.F<<" "<<p.S;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.F>>p.S;return is;}
template<typename T>ostream&operator<<(ostream&os,vector<T>v){rep(i,0,sz(v))os<<v[i]<<(i+1!=sz(v)?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
int main(){
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);
  ll _;cin>>_;
  while(_--){
    ll N;cin>>N;
    vl P(N);
    rep(i,1,N){
      cin>>P[i];
      P[i]--;
    }
    vvl E(N);
    rep(i,1,N)E[P[i]].emplace_back(i);
    vl DP(N),S(N,1);
    rrep(i,0,N){
      for(auto j:E[i])S[i]+=S[j];
      ll l=-1;
      for(auto j:E[i])if(S[j]>S[i]-1-S[j])l=j;
      if(l==-1||S[l]-2*DP[l]<=S[i]-1-S[l]){
        DP[i]=(S[i]-1)/2;
        continue;
      }
      DP[i]=DP[l]+S[i]-1-S[l];
    }
    cout<<DP[0]<<endl;
  }
  return 0;
}

Submission Info

Submission Time
Task D - Non-Ancestor Matching
User TKTYI
Language C++ 20 (gcc 12.2)
Score 600
Code Size 2548 Byte
Status AC
Exec Time 84 ms
Memory 42252 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 1 ms 3556 KiB
01_small_00.txt AC 71 ms 3572 KiB
01_small_01.txt AC 84 ms 3516 KiB
01_small_02.txt AC 80 ms 3644 KiB
02_handmade_00.txt AC 28 ms 30560 KiB
02_handmade_01.txt AC 45 ms 42084 KiB
02_handmade_02.txt AC 41 ms 34192 KiB
02_handmade_03.txt AC 41 ms 34444 KiB
02_handmade_04.txt AC 42 ms 34104 KiB
02_handmade_05.txt AC 42 ms 34592 KiB
02_handmade_06.txt AC 45 ms 42104 KiB
02_handmade_07.txt AC 63 ms 35768 KiB
02_handmade_08.txt AC 63 ms 35916 KiB
02_handmade_09.txt AC 63 ms 35768 KiB
02_handmade_10.txt AC 45 ms 3672 KiB
02_handmade_11.txt AC 45 ms 3624 KiB
02_handmade_12.txt AC 45 ms 3656 KiB
02_handmade_13.txt AC 45 ms 3592 KiB
02_handmade_14.txt AC 45 ms 3652 KiB
02_handmade_15.txt AC 44 ms 3584 KiB
02_handmade_16.txt AC 44 ms 3652 KiB
02_handmade_17.txt AC 45 ms 3716 KiB
02_handmade_18.txt AC 63 ms 35844 KiB
02_handmade_19.txt AC 45 ms 3652 KiB
02_handmade_20.txt AC 44 ms 3612 KiB
02_handmade_21.txt AC 44 ms 3712 KiB
02_handmade_22.txt AC 45 ms 3712 KiB
02_handmade_23.txt AC 35 ms 3712 KiB
02_handmade_24.txt AC 35 ms 3604 KiB
02_handmade_25.txt AC 35 ms 3672 KiB
02_handmade_26.txt AC 35 ms 3624 KiB
02_handmade_27.txt AC 41 ms 3612 KiB
02_handmade_28.txt AC 41 ms 3716 KiB
02_handmade_29.txt AC 41 ms 3632 KiB
02_handmade_30.txt AC 41 ms 3732 KiB
02_handmade_31.txt AC 46 ms 42152 KiB
02_handmade_32.txt AC 46 ms 42092 KiB
02_handmade_33.txt AC 45 ms 42148 KiB
02_handmade_34.txt AC 46 ms 42248 KiB
02_handmade_35.txt AC 47 ms 42140 KiB
02_handmade_36.txt AC 46 ms 42252 KiB
02_handmade_37.txt AC 47 ms 42244 KiB
02_handmade_38.txt AC 47 ms 42156 KiB
02_handmade_39.txt AC 46 ms 42240 KiB
02_handmade_40.txt AC 46 ms 42244 KiB
02_handmade_41.txt AC 47 ms 42196 KiB
02_handmade_42.txt AC 46 ms 42248 KiB
02_handmade_43.txt AC 46 ms 42188 KiB
02_handmade_44.txt AC 46 ms 42160 KiB
02_handmade_45.txt AC 23 ms 20232 KiB
03_random_00.txt AC 66 ms 35776 KiB
03_random_01.txt AC 66 ms 35824 KiB
03_random_02.txt AC 66 ms 35784 KiB
03_random_03.txt AC 66 ms 35820 KiB
03_random_04.txt AC 67 ms 35772 KiB
03_random_05.txt AC 65 ms 35756 KiB
03_random_06.txt AC 66 ms 35772 KiB
03_random_07.txt AC 65 ms 35768 KiB
03_random_08.txt AC 65 ms 35816 KiB
03_random_09.txt AC 65 ms 35760 KiB
03_random_10.txt AC 27 ms 30828 KiB
03_random_11.txt AC 29 ms 30752 KiB
03_random_12.txt AC 29 ms 32380 KiB
03_random_13.txt AC 31 ms 32608 KiB
03_random_14.txt AC 46 ms 3668 KiB
03_random_15.txt AC 45 ms 3792 KiB
03_random_16.txt AC 45 ms 3596 KiB
03_random_17.txt AC 46 ms 3664 KiB
03_random_18.txt AC 46 ms 3676 KiB
03_random_19.txt AC 48 ms 42088 KiB
03_random_20.txt AC 48 ms 42252 KiB
03_random_21.txt AC 47 ms 42196 KiB
03_random_22.txt AC 47 ms 42188 KiB
03_random_23.txt AC 47 ms 42088 KiB
03_random_24.txt AC 48 ms 42156 KiB
03_random_25.txt AC 47 ms 42188 KiB
03_random_26.txt AC 47 ms 42156 KiB
04_from_path_subtree_01.txt AC 52 ms 38720 KiB
04_from_path_subtree_02.txt AC 51 ms 39468 KiB
04_from_path_subtree_03.txt AC 48 ms 40556 KiB
04_from_path_subtree_04.txt AC 51 ms 39168 KiB
04_from_path_subtree_05.txt AC 48 ms 39808 KiB
04_from_path_subtree_06.txt AC 47 ms 41564 KiB
04_from_path_subtree_07.txt AC 50 ms 39228 KiB
04_from_path_subtree_08.txt AC 48 ms 40844 KiB
04_from_path_subtree_09.txt AC 52 ms 39080 KiB
04_from_path_subtree_10.txt AC 46 ms 41296 KiB