当前位置:网站首页>Serval and Rooted Tree(CF1153D)-DP
Serval and Rooted Tree(CF1153D)-DP
2022-06-11 20:56: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;
}
边栏推荐
- Vectordrawable error
- Implement AOP and interface caching on WPF client
- 浅谈UGUI中Canvas RectTransform的Scale
- Unity package manager starting server stuck
- JMeter load test finds the maximum number of concurrent users (including step analysis)
- Chrome V8 source code 48 The secret of weak type addition,'+'source code analysis
- 2022-2028 global and Chinese thermal power generation (TEG) module market status and future development trend
- R 16 basic exercises
- What are striplines and microstrip lines? Reference planes and transmission lines
- Power supply anti reverse connection and anti backflow - use MOS tube and op amp to realize ideal diode
猜你喜欢

Black circle display implementation

Why is your LDO output unstable?

周刊02|不瞒你说,我其实是MIT的学生

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

28. JS implementation mechanism

Final examination of theory and practice of socialism with Chinese characteristics 1

27. this pointing problem

The scale of the global machine vision market continues to rise. Poe image acquisition card provides a high-speed transmission channel for industrial cameras

Unity package manager starting server stuck

In idea, run the yarn command to show that the file cannot be loaded because running scripts is disabled on this system
随机推荐
10 R vector operation construction
重投农业,加码技术服务,拼多多底盘进一步夯实
Compilation process of program
Deploy website traffic statistics background based on Tencent cloud lightweight application server and umami
Usage methods and cases of PLSQL blocks, cursors, functions, stored procedures and triggers of Oracle Database
Serval and Rooted Tree(CF1153D)-DP
Gestionnaire de paquets d'Unit é Starting Server Stuck
The e-sports Internet cafe uses a 2.5G network card to experience the feeling of flying!
var 和 let的区别_let 和 var的区别
New product release: domestic single port Gigabit network card is officially mass produced!
Rtd2171u, substitute for trd2171u, substitute for trd2171u, cs5261 C to hdmi4k_ 30Hz
[nk] deleted number of 100 C Xiaohong in Niuke practice match
Using the flask framework to write the bezel
黑圆圈显示实现
2022-2028 current situation and future development trend of fuel cell market for cogeneration application in the world and China
Space transcriptome experiment | what factors will affect the quality of space transcriptome sequencing during the preparation of clinical tissue samples?
Teach you how to grab ZigBee packets through cc2531 and parse encrypted ZigBee packets
Unity package manager starting server stuck
2022-2028 global and Chinese thermal conductivity hydrogen analyzer Market Status and future development trend
2022-2028 global and Chinese thermal power generation (TEG) module market status and future development trend