当前位置:网站首页>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;
}
边栏推荐
- 2022-2028 current situation and future development trend of fuel cell market for cogeneration application in the world and China
- Power supply anti reverse connection and anti backflow - use MOS tube and op amp to realize ideal diode
- 模拟Oracle锁等待与手工解锁
- STC hardware only automatic download circuit V2
- 周刊02|不瞒你说,我其实是MIT的学生
- Current situation and future development trend of global and Chinese cogeneration system market from 2022 to 2028
- On scale of canvas recttransform in ugui
- Application analysis of Poe image acquisition card in machine vision industrial computer
- 27. this指向问题
- Research and Analysis on the market status of polybutene-1 in China from 2021 to 2027 and forecast report on its development prospect
猜你喜欢

moderlarts第一次培训

Role of RESNET residual block

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

Wechat applet | rotation chart
![[file upload vulnerability 04] server side mime detection and bypass experiment (based on upload-labs-2 shooting range)](/img/b8/521ca4bb8931afab9a3a4d0b015125.jpg)
[file upload vulnerability 04] server side mime detection and bypass experiment (based on upload-labs-2 shooting range)

STC hardware only automatic download circuit V2

moderlarts第一次培訓

Final examination of Dialectics of nature 1

Compilation process of program

Redis第四话 -- redis高性能原理(多路复用)和高可用分析(备份、主从)
随机推荐
Redis第四话 -- redis高性能原理(多路复用)和高可用分析(备份、主从)
Object storage of CEPH distributed storage
8 r subset
Js 监听滚动触底加载更多_浏览器滚动触底加载更多
Weekly 02 | pour être honnête, je suis un étudiant du MIT
Simulate Oracle lock waiting and manual unlocking
On scale of canvas recttransform in ugui
为什么100G网络传输要使用iWARP、RoCE v2、NVMe-oF等协议
修改本地微信小程序的AppID
29. location object
[unity plug-in] shader keyword analysis tool shadercontrol
Première formation sur les largeurs modernes
Modify appid of local wechat applet
Docker installation redis
Application analysis of Poe image acquisition card in machine vision industrial computer
Mysql add 新增多个新字段并指定字段位置
[nk] 牛客练习赛100 C 小红的删数字
Unity screenshot
Force buckle 6 Zigzag transformation
Interviewer: what is the event flow and event model in JS?