当前位置:网站首页>Serval and rooted Tree (cf1153d) - DP
Serval and rooted Tree (cf1153d) - DP
2022-06-11 20:56:00 【Tan Yuu】
Liens vers les sujets;
RÉFÉRENCES;
Objet
Pour quelqu'un qui a k Une feuillemax minArbre des opérations,In k Remplir les feuilles1~k,Trouvez la valeur maximale pour la racine de l'arbre;
Idées
Comme la sortie des résultats de remplissage n'est pas requise dans la question,On peut utiliser“Quel est le plus grand?”Pour marquer la taille:PourmaxNoeud,Sa valeur de noeud est la valeur minimale du noeud Enfant,PourminNoeud,Dont la valeur du noeud est la somme des noeuds enfants;Dans ce processus,,Nous avons compressé beaucoup d'informations inutiles,Simplifie les problèmes;
Définir la représentation de l'état: f [ i ] f[i] f[i] Pour i Dans le Sous - arbre du noeud racine,i La valeur de est la plus élevée des sous - arbres;
Définir les valeurs initiales:Pour les noeuds de feuilles i , f [ i ] = 1 f[i]=1 f[i]=1 ;
Définir l'équation de transfert d'état:
PourmaxNoeud, f [ i ] = min j is son f [ j ] f[i]=\text{min}_{j\text{ is son}}f[j] f[i]=minj is sonf[j] ;
PourminNoeud, 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;
}
边栏推荐
- 14 r basic exercises
- php pcntl_fork 创建多个子进程解析
- JSON introduction
- Systematically study the recommendation system from a global perspective to improve competitiveness in actual combat (Chapter 8)
- [index system] the latest modeling method of data warehouse index system
- Explanation of each column output by explain statement
- Current situation and future development trend of global and Chinese cogeneration system market from 2022 to 2028
- Date of SQL optimization_ Format() function
- 2022-2028 global and Chinese thermal power generation (TEG) module market status and future development trend
- Ora-01089 ora-19809 ora-19815 exceeded the limit for recovering files
猜你喜欢

Usage methods and cases of PLSQL blocks, cursors, functions, stored procedures and triggers of Oracle Database

Object storage of CEPH distributed storage

第二部分 数据链路层

moderlarts第二次作业

从概率论基础出发推导卡尔曼滤波
![[data visualization] Apache superset 1.2.0 tutorial (II) - Quick Start (visualizing King hero data)](/img/21/c2212a674fdf77571305446217a5ca.png)
[data visualization] Apache superset 1.2.0 tutorial (II) - Quick Start (visualizing King hero data)

New product release: lr-link Lianrui launched the first 25g OCP 3.0 network card

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

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

10 R vector operation construction
随机推荐
Ubantu1804 two opencv versions coexist
模拟Oracle锁等待与手工解锁
Unity package manager starting server stuck
[nk] deleted number of 100 C Xiaohong in Niuke practice match
Wechat applet Bluetooth development
Toolbar替换ActionBar后Title不显示
新品发布:国产单电口千兆网卡正式量产!
Current situation and future development trend of global and Chinese cogeneration system market from 2022 to 2028
In idea, run the yarn command to show that the file cannot be loaded because running scripts is disabled on this system
Vectordrawable error
MySQL add adds multiple new fields and specifies the field location
The world's first public chain integrating commercial and financial fields
moderlarts第二次作业
Js 监听滚动触底加载更多_浏览器滚动触底加载更多
What is the essence and process of SCM development? Chengdu Automation Development Undertaking
Interviewer: what is the event flow and event model in JS?
Power supply anti reverse connection and anti backflow - use MOS tube and op amp to realize ideal diode
[file upload vulnerability 04] server side mime detection and bypass experiment (based on upload-labs-2 shooting range)
Rtd2171u, substitute for trd2171u, substitute for trd2171u, cs5261 C to hdmi4k_ 30Hz
2022-2028 global and Chinese thermal ionization isotope mass spectrometer (TIMS) market status and future development trend