当前位置:网站首页>2020ccpc Qinhuangdao J - Kingdom's power
2020ccpc Qinhuangdao J - Kingdom's power
2022-07-05 05:31:00 【solemntee】
#include<bits/stdc++.h>
using namespace std;
vector<int>v[1000005];
int siz[1000005],dep[1000005];
int dp[2][1000005];
void dfs1(int x,int pre)
{
siz[x]=1;
if(x!=1)dep[x]=dep[pre]+1;
for(auto to:v[x])if(to!=pre)
{
dfs1(to,x);
siz[x]+=siz[to];
}
}
void dfs(int x,int pre)
{
dp[0][x]=siz[x]*2;
if((int)v[x].size()==1&&x!=1)
{
dp[1][x]=dep[x];
return;
}
bool flag=false;
int ans=0;
int mn=1000000;
for(auto to:v[x])if(to!=pre)
{
dfs(to,x);
if(dp[1][to]<=dp[0][to])
{
flag=true;
ans+=dp[1][to];
mn=min(mn,dp[1][to]-dp[0][to]);
}
else
{
ans+=dp[0][to];
mn=min(mn,dp[1][to]-dp[0][to]);
}
}
if(flag==false)ans+=mn;
dp[1][x]=ans;
}
int main()
{
int T;
scanf("%d",&T);
for(int tt=1;tt<=T;tt++)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)v[i].clear();
for(int i=1;i<=n;i++)dp[1][i]=dp[0][i]=0;
for(int i=1;i<n;i++)
{
int temp;
scanf("%d",&temp);
v[i+1].push_back(temp);
v[temp].push_back(i+1);
}
dfs1(1,1);
dfs(1,1);
printf("Case #%d: %d\n",tt,dp[1][1]);
}
return 0;
}
边栏推荐
- Sword finger offer 53 - I. find the number I in the sorted array
- Pointnet++的改进
- Codeforces round 712 (Div. 2) d. 3-coloring (construction)
- Haut OJ 1350: choice sends candy
- On-off and on-off of quality system construction
- lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
- Kubedm series-00-overview
- Haut OJ 1357: lunch question (I) -- high precision multiplication
- 用STM32点个灯
- The number of enclaves
猜你喜欢
Fragment addition failed error lookup
Sword finger offer 53 - ii Missing numbers from 0 to n-1
[interval problem] 435 Non overlapping interval
Improvement of pointnet++
Codeforces round 712 (Div. 2) d. 3-coloring (construction)
Using HashMap to realize simple cache
Chapter 6 data flow modeling - after class exercises
Light a light with stm32
Sword finger offer 09 Implementing queues with two stacks
YOLOv5添加注意力機制
随机推荐
质量体系建设之路的分分合合
Pointnet++学习
ssh免密登录设置及使用脚本进行ssh登录并执行指令
Annotation and reflection
SAP method of modifying system table data
剑指 Offer 05. 替换空格
Web APIs DOM node
Add level control and logger level control of Solon logging plug-in
Haut OJ 1245: large factorial of CDs --- high precision factorial
Using HashMap to realize simple cache
FVP和Juno平台的Memory Layout介绍
Reader writer model
剑指 Offer 58 - II. 左旋转字符串
kubeadm系列-01-preflight究竟有多少check
C language Essay 1
[merge array] 88 merge two ordered arrays
软件测试 -- 0 序
Haut OJ 1221: a tired day
Daily question - Search two-dimensional matrix PS two-dimensional array search
Haut OJ 1218: maximum continuous sub segment sum