当前位置:网站首页>Serval and Rooted Tree(CF1153D)-DP
Serval and Rooted Tree(CF1153D)-DP
2022-06-11 20:47:00 【Tan_Yuu】
题目大意
对于一个有 k 个叶子的max min操作树,在 k 个叶子中填入1~k,求树根的最大值;
思路
由于题目中不要求输出填充结果,我们可以使用“第几大”来标记大小:对于max节点,其节点值为子节点的最小值,对于min节点,其节点值为子节点的和;在这个过程中,我们压缩掉了很多无用的信息,简化了问题;
定义状态表示: f [ i ] f[i] f[i] 为在以 i 为根节点的子树中,i 的值为子树中的第几大;
定义初值:对于叶子节点 i , f [ i ] = 1 f[i]=1 f[i]=1 ;
定义状态转移方程:
对于max节点, f [ i ] = min j is son f [ j ] f[i]=\text{min}_{j\text{ is son}}f[j] f[i]=minj is sonf[j] ;
对于min节点, f [ i ] = ∑ j is son f [ j ] f[i]=\sum_{j\text{ is son}}f[j] f[i]=∑j is sonf[j] ;
代码
#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;
}
边栏推荐
- MySQL add adds multiple new fields and specifies the field location
- 7905 and TL431 negative voltage regulator circuit - regulator and floating circuit relative to the positive pole of the power supply
- New product release: lr-link Lianrui launched the first 25g OCP 3.0 network card
- 周刊02|不瞒你说,我其实是MIT的学生
- 【指标体系】最新数仓指标体系建模方法
- Current situation and future development trend of tropical forage seed market in the world and China from 2022 to 2028
- unity package manager starting server stuck(Unity啟動卡在starting server,然後報錯)
- Why should I use iwarp, roce V2, nvme of and other protocols for 100g network transmission
- Calculation of Zeno paradox
- 产品资讯|PoE网卡家族集体亮相,机器视觉完美搭档!
猜你喜欢

JMeter load test finds the maximum number of concurrent users (including step analysis)

unity package manager starting server stuck(Unity启动卡在starting server,然后报错)

29. Objet de localisation

Redis fourth session - redis high performance principle (multiplexing) and high availability analysis (backup, master-slave)

应用场景:现场直播节目制作NDI技术中PoE网卡的广泛应用

unity package manager starting server stuck(Unity啟動卡在starting server,然後報錯)

Solution to unlimited restart of desktop and file explorer

Redis第四话 -- redis高性能原理(多路复用)和高可用分析(备份、主从)

Chinese text classification based on CNN

Teach you how to use win7 system to quickly build your own website
随机推荐
29. Objet de localisation
IDEA中,运行yarn命令,显示无法加载文件,因为在此系统上禁用运行脚本
Frequency domain filter
ORA-01089 ORA-19809 ORA-19815 超过了恢复文件的限制
Power supply anti reverse connection and anti backflow - use MOS tube and op amp to realize ideal diode
Modify appid of local wechat applet
R 16 basic exercises
Wechat applet Bluetooth development
2022-2028 current situation and future development trend of fuel cell market for cogeneration application in the world and China
【数据可视化】Apache Superset 1.2.0教程 (三)—— 图表功能详解
[nk] deleted number of 100 C Xiaohong in Niuke practice match
JSON introduction
Summary of C language programming knowledge points 01
[nk] 牛客练习赛100 C 小红的删数字
Implement AOP and interface caching on WPF client
Technical exchange | why should network security equipment use bypass function
c语言程序设计知识点总结 01
[Monday commuter radio station] cron expression. It's enough to read this article
周刊02|不瞒你说,我其实是MIT的学生
12 date and time in R