提出 #69086498


ソースコード 拡げる

//# pragma GCC target("avx2")
# pragma GCC optimize("O3")
# pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#include"atcoder/all"
using namespace atcoder;
typedef modint998244353 mi;
using namespace std;
#define all(a) a.begin(),a.end()
template<typename T> bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;}
template<typename T> bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;}
#define compress(a) sort(all(a));a.erase(unique(all(a)),a.end())
typedef long long ll;
typedef pair<ll,ll> P;
constexpr ll mod=998244353;
constexpr ll inf=3e18;

void solve(){
	int n;
	cin>>n;
	vector<int>p(n);
	vector<vector<int>>G(n);
	for(int i=1;i<n;i++){
		cin>>p[i];
		p[i]--;
		G[p[i]].push_back(i);
	}
	vector<int>d(n),weight(n);
	function<void(int)> dfs=[&](int now){
		weight[now]+=1;
		for(auto &e:G[now]){
			d[e]=d[now]+1;
			dfs(e);
			weight[now]+=weight[e];
		}
	};
	
	dfs(0);
	int now=0,stock=0,ans=0;
	while(G[now].size()){
		int val=-1,v=-1;
		for(auto &e:G[now]){
			stock+=weight[e];
			if(val<weight[e]){
				val=weight[e];
				v=e;
			}
		}
		stock-=val;
		now=v;
		if(stock){
			stock--;
			ans++;
		}
	}
	ans+=stock/2;
	cout<<ans<<endl;

}

int main(){
	int t;
	cin>>t;
	while(t--)solve();
}

提出情報

提出日時
問題 D - Non-Ancestor Matching
ユーザ Rho17
言語 C++ 20 (gcc 12.2)
得点 600
コード長 1349 Byte
結果 AC
実行時間 212 ms
メモリ 67780 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 600 / 600
結果
AC × 1
AC × 87
セット名 テストケース
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
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 1 ms 3560 KiB
01_small_00.txt AC 100 ms 3692 KiB
01_small_01.txt AC 119 ms 3560 KiB
01_small_02.txt AC 116 ms 3544 KiB
02_handmade_00.txt AC 56 ms 22856 KiB
02_handmade_01.txt AC 133 ms 67572 KiB
02_handmade_02.txt AC 124 ms 44196 KiB
02_handmade_03.txt AC 124 ms 44124 KiB
02_handmade_04.txt AC 113 ms 28572 KiB
02_handmade_05.txt AC 112 ms 28524 KiB
02_handmade_06.txt AC 154 ms 46688 KiB
02_handmade_07.txt AC 184 ms 28804 KiB
02_handmade_08.txt AC 181 ms 28816 KiB
02_handmade_09.txt AC 177 ms 28728 KiB
02_handmade_10.txt AC 88 ms 3700 KiB
02_handmade_11.txt AC 88 ms 3764 KiB
02_handmade_12.txt AC 88 ms 3804 KiB
02_handmade_13.txt AC 88 ms 3688 KiB
02_handmade_14.txt AC 88 ms 3696 KiB
02_handmade_15.txt AC 89 ms 3708 KiB
02_handmade_16.txt AC 90 ms 3748 KiB
02_handmade_17.txt AC 91 ms 3748 KiB
02_handmade_18.txt AC 171 ms 28740 KiB
02_handmade_19.txt AC 94 ms 3788 KiB
02_handmade_20.txt AC 94 ms 3860 KiB
02_handmade_21.txt AC 95 ms 3716 KiB
02_handmade_22.txt AC 94 ms 3704 KiB
02_handmade_23.txt AC 85 ms 3720 KiB
02_handmade_24.txt AC 86 ms 3804 KiB
02_handmade_25.txt AC 85 ms 3784 KiB
02_handmade_26.txt AC 85 ms 3788 KiB
02_handmade_27.txt AC 88 ms 3692 KiB
02_handmade_28.txt AC 87 ms 3716 KiB
02_handmade_29.txt AC 88 ms 3828 KiB
02_handmade_30.txt AC 87 ms 3784 KiB
02_handmade_31.txt AC 124 ms 50008 KiB
02_handmade_32.txt AC 124 ms 52340 KiB
02_handmade_33.txt AC 133 ms 67636 KiB
02_handmade_34.txt AC 132 ms 67692 KiB
02_handmade_35.txt AC 132 ms 67616 KiB
02_handmade_36.txt AC 132 ms 67648 KiB
02_handmade_37.txt AC 133 ms 67564 KiB
02_handmade_38.txt AC 132 ms 67780 KiB
02_handmade_39.txt AC 134 ms 67600 KiB
02_handmade_40.txt AC 133 ms 67296 KiB
02_handmade_41.txt AC 136 ms 64480 KiB
02_handmade_42.txt AC 134 ms 61448 KiB
02_handmade_43.txt AC 133 ms 58540 KiB
02_handmade_44.txt AC 131 ms 55236 KiB
02_handmade_45.txt AC 60 ms 16660 KiB
03_random_00.txt AC 206 ms 28748 KiB
03_random_01.txt AC 194 ms 28736 KiB
03_random_02.txt AC 206 ms 28980 KiB
03_random_03.txt AC 197 ms 28844 KiB
03_random_04.txt AC 202 ms 28804 KiB
03_random_05.txt AC 212 ms 28724 KiB
03_random_06.txt AC 211 ms 28804 KiB
03_random_07.txt AC 211 ms 28808 KiB
03_random_08.txt AC 192 ms 28808 KiB
03_random_09.txt AC 198 ms 28876 KiB
03_random_10.txt AC 56 ms 22908 KiB
03_random_11.txt AC 60 ms 22952 KiB
03_random_12.txt AC 75 ms 23732 KiB
03_random_13.txt AC 83 ms 23788 KiB
03_random_14.txt AC 87 ms 3744 KiB
03_random_15.txt AC 87 ms 3692 KiB
03_random_16.txt AC 88 ms 3864 KiB
03_random_17.txt AC 87 ms 3616 KiB
03_random_18.txt AC 88 ms 3684 KiB
03_random_19.txt AC 140 ms 39184 KiB
03_random_20.txt AC 140 ms 38876 KiB
03_random_21.txt AC 140 ms 39056 KiB
03_random_22.txt AC 154 ms 44148 KiB
03_random_23.txt AC 152 ms 44156 KiB
03_random_24.txt AC 150 ms 44108 KiB
03_random_25.txt AC 157 ms 44288 KiB
03_random_26.txt AC 158 ms 44108 KiB
04_from_path_subtree_01.txt AC 146 ms 48612 KiB
04_from_path_subtree_02.txt AC 137 ms 55656 KiB
04_from_path_subtree_03.txt AC 135 ms 60232 KiB
04_from_path_subtree_04.txt AC 146 ms 51716 KiB
04_from_path_subtree_05.txt AC 136 ms 57852 KiB
04_from_path_subtree_06.txt AC 133 ms 64708 KiB
04_from_path_subtree_07.txt AC 138 ms 54972 KiB
04_from_path_subtree_08.txt AC 136 ms 59376 KiB
04_from_path_subtree_09.txt AC 147 ms 49900 KiB
04_from_path_subtree_10.txt AC 132 ms 64880 KiB