当前位置:网站首页>Serval and Rooted Tree(CF1153D)-DP
Serval and Rooted Tree(CF1153D)-DP
2022-06-11 20:56:00 【Tan_ Yuu】
Topic link ;
Reference resources ;
The main idea of the topic
For one there is k A leaf max min Operation tree , stay k Fill the leaves with 1~k, Find the maximum value of tree root ;
Ideas
Because the title does not require the output of filling results , We can use “ What is the largest ” To mark the size : about max node , The node value is the minimum value of the child node , about min node , The node value is the sum of the child nodes ; In the process , We have compressed a lot of useless information , Simplify the problem ;
Define the state representation : f [ i ] f[i] f[i] For at i In the subtree of the root node ,i Is the largest value in the subtree ;
Define the initial value : For leaf nodes i , f [ i ] = 1 f[i]=1 f[i]=1 ;
Define the state transfer equation :
about max node , f [ i ] = min j is son f [ j ] f[i]=\text{min}_{j\text{ is son}}f[j] f[i]=minj is sonf[j] ;
about min node , f [ i ] = ∑ j is son f [ j ] f[i]=\sum_{j\text{ is son}}f[j] f[i]=∑j is sonf[j] ;
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int op[300005], f[300005];
vector<int> sn[300005];
void dfs(int x)
{
if (sn[x].empty())
{
f[x] = 1;
return;
}
int fmx = 1000006, fmn = 0;
for (auto m : sn[x])
{
dfs(m);
fmx = min(fmx, f[m]);
fmn += f[m];
}
if (op[x])
f[x] = fmx;
else
f[x] = fmn;
}
int main()
{
int n, tmp;
cin >> n;
for (int i = 1; i <= n; i++)
scanf("%d", &op[i]);
for (int i = 2; i <= n; i++)
scanf("%d", &tmp), sn[tmp].push_back(i);
int k = 0;
for (int i = 1; i <= n; i++)
if (sn[i].empty())
k++;
dfs(1);
cout << k - f[1] + 1;
return 0;
}
边栏推荐
- Current situation and future development trend of hot isostatic pressing service market in the world and China from 2022 to 2028
- unity package manager starting server stuck(Unity启动卡在starting server,然后报错)
- Compilation process of program
- 全球机器视觉市场规模持续上涨,PoE图像采集卡为工业相机提供高速传输通道
- 正则校验匹配[0-100]、[0-1000]之间的正整数或小数点位数限制
- Technical exchange | why should network security equipment use bypass function
- Yintai department store and Taobao tmall jointly create a green fashion show to help "carbon neutrality"
- [data visualization] use Apache superset to visualize Clickhouse data
- Mysql add 新增多个新字段并指定字段位置
- 12 date and time in R
猜你喜欢

Teach you how to grab ZigBee packets through cc2531 and parse encrypted ZigBee packets

为什么100G网络传输要使用iWARP、RoCE v2、NVMe-oF等协议

Why is your LDO output unstable?

IDEA中,运行yarn命令,显示无法加载文件,因为在此系统上禁用运行脚本

26. timer

【博弈论-完全信息静态博弈】 战略式博弈

从概率论基础出发推导卡尔曼滤波

The input value "18-20000hz" is incorrect. The setting information is incomplete. Please select a company

【数据可视化】使用 Apache Superset 可视化 ClickHouse 数据

【数据可视化】Apache Superset 1.2.0教程 (二)——快速入门(可视化王者英雄数据)
随机推荐
What is the meaning of holding shares of financial products?
On scale of canvas recttransform in ugui
Application scenario: wide application of Poe network card in NDI technology for live broadcast program production
Frequency domain filter
Lr-link Lianrui makes its debut at the digital Expo with new products - helping the construction of new infrastructure data center
2022-2028 global and Chinese thermal conductivity hydrogen analyzer Market Status and future development trend
php pcntl_fork 创建多个子进程解析
Current situation and future development trend of thermoelectric generator Market in the world and China from 2022 to 2028
Title does not display after toolbar replaces actionbar
How to add text on the border in bar code software
LR-LINK联瑞携新品首次亮相数博会-助力新基建数据中心建设
【计算机推免】哈尔滨工业大学物联网与泛在智能研究中心面向全国高校推免生招收2023级研究生(硕、博、直博生)
Usage methods and cases of PLSQL blocks, cursors, functions, stored procedures and triggers of Oracle Database
为什么100G网络传输要使用iWARP、RoCE v2、NVMe-oF等协议
输入值“18-20000hz”错误,设置信息不完整,请选择单位
[unity plug-in] shader keyword analysis tool shadercontrol
Gestionnaire de paquets d'Unit é Starting Server Stuck
JVM对象分配策略TLAB
【数据可视化】Apache Superset 1.2.0教程 (二)——快速入门(可视化王者英雄数据)
Brain cell membrane equivalent neural network training code