Submission #2210566


Source Code Expand

Copy
#include <bits/stdc++.h>
#define pq priority_queue
using namespace std; typedef pair<int, int> P; typedef pair<int, P> P2;
typedef pair<int, P2> P3; typedef long long ll; typedef long double ld;
constexpr long long gcd(long long a, long long b){return b ? gcd(b, a % b) : a;}
constexpr long long lcm(long long a, long long b){return a / gcd(a, b) * b;}
constexpr int INF = 1e9, MOD = INF + 7, around[] = {0, 1, 1, -1, -1, 0, -1, 1, 0, 0};
constexpr int mod_pow(long long x, long long n, const int mod){long long ret=1;while(n){if(n&1)(ret*=x)%=mod;(x*=x)%=mod;n>>=1;}return ret;}
template<int n> struct Prime{bool arr[n+1];constexpr bool operator[](int k){return arr[k];}constexpr Prime():arr(){for(int i=2;i<n;i++){arr[i]=true;for(int j=2;j*j<=i;j++){if(!(i%j))arr[i]=false;}}}};
template<int n> struct Factorial{long long arr[n+1],ary[n+1];constexpr Factorial():arr(),ary(){arr[0]=1;ary[0]=1;for(int i=0;i<n;i++){arr[i+1]=arr[i]*(i+1)%MOD;ary[i+1]=mod_pow(arr[i+1],MOD-2,MOD);}}};
constexpr Factorial<10> fact; constexpr Prime<10> prime;
constexpr int comb(int a, int b){long long pos = fact.arr[a], pot = fact.ary[a - b], por = fact.ary[b];return pos * pot % MOD * por % MOD;}
template<int n> struct Bernoulli{long long arr[n+1];constexpr Bernoulli():arr(){arr[0]=1;for(int i=1;i<=n;i++){long long sum=0;for(int j=0;j<i;j++){(sum+=comb(i+1,j)*arr[j]%MOD)%=MOD;}arr[i]=(MOD-mod_pow(i+1,MOD-2,MOD))%MOD*sum%MOD;}}};
constexpr int vx[] = {1, 0, -1, 0}, vy[] = {0, 1, 0, -1};
constexpr int sqrtN = 512, logN = 32;
constexpr ld PI = abs(acos(-1));
constexpr ll LINF=1e18;

int dp[100010], p[100010], n;
vector<vector<int>> g;

int dfs(int v, int par = -1){
	if(dp[v]) return dp[v]; p[v] = par;
	
	int ret = 1;
	for(auto e:g[v]){
		if(e == par) continue;
		ret += dfs(e, v);
	}
	
	return dp[v] = ret;
}

int main(){
	cin >> n; g.resize(n);
	for(int i = 0; i < n - 1; i++){
		cin >> p[i];
		g[p[i]].push_back(i + 1);
		g[i + 1].push_back(p[i]);
	}
	dfs(0);
	
	for(int i = 0; i < n; i++){
		int ma = 0, s = 0;
		for(auto e:g[i]){
			if(e == p[i]) continue;
			ma = max(ma, dp[e]);
			s += dp[e];
		}
		
		ma = max(ma, n - 1 - s);
		cout << ma << endl;
	}
	
	return 0;
}

Submission Info

Submission Time
Task C - 高橋王国の分割統治
User ecasdqina
Language C++14 (GCC 5.4.1)
Score 100
Code Size 2221 Byte
Status
Exec Time 215 ms
Memory 13312 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 sample_01.txt, sample_02.txt
Subtask1 30 / 30 sample_01.txt, sample_02.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt
Subtask2 70 / 70 sample_01.txt, sample_02.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask2_01.txt, subtask2_02.txt, subtask2_03.txt, subtask2_04.txt, subtask2_05.txt, subtask2_06.txt, subtask2_07.txt
Case Name Status Exec Time Memory
sample_01.txt 1 ms 256 KB
sample_02.txt 1 ms 256 KB
subtask1_01.txt 1 ms 256 KB
subtask1_02.txt 1 ms 256 KB
subtask1_03.txt 1 ms 256 KB
subtask1_04.txt 1 ms 256 KB
subtask1_05.txt 3 ms 256 KB
subtask1_06.txt 3 ms 256 KB
subtask1_07.txt 3 ms 256 KB
subtask1_08.txt 3 ms 384 KB
subtask1_09.txt 3 ms 384 KB
subtask2_01.txt 154 ms 5376 KB
subtask2_02.txt 195 ms 6656 KB
subtask2_03.txt 215 ms 7168 KB
subtask2_04.txt 209 ms 7168 KB
subtask2_05.txt 176 ms 7416 KB
subtask2_06.txt 186 ms 7680 KB
subtask2_07.txt 202 ms 13312 KB