CF 551 D.Serval and Rooted Tree 树形DP
传送门:http://codeforces.com/contest/1153/problem/D
思路:
这道题想了一天,突发奇想,就是维护每个点两个值,第几大和第几小,就可以有传递性了。
#include <bits/stdc++.h> using namespace std; #define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
const ll inff = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f; const int maxn = 3e5+;
int a[maxn];
vector<int>mp[maxn];
int dp[maxn][];
int all = ;
void dfs(int u, int fa) {
if(mp[u].size() == ) dp[u][] = dp[u][] = , all++;
else if(a[u] == ) dp[u][] = , dp[u][] = inf;
else if(a[u] == ) dp[u][] = inf, dp[u][] = ; for(int i=; i<mp[u].size(); i++) {
int v = mp[u][i];
if(v == fa) continue;
dfs(v, u); if(a[u] == ) {
dp[u][] = min(dp[v][], dp[u][]);
dp[u][] += dp[v][];
}
else {
dp[u][] += dp[v][];
dp[u][] = min(dp[v][], dp[v][]);
}
}
} int main(){
int n;
scanf("%d", &n);
for(int i=; i<=n; i++) scanf("%d", &a[i]);
for(int i=; i<=n; i++) {
int x; scanf("%d", &x);
mp[x].pb(i);
}
memset(dp, inf, sizeof(dp));
dfs(, );
// cout<<" dp[5][1] = " <<dp[5][1]<<endl;
// cout<<dp[1][0] << " , " << dp[1][1]<<endl;
printf("%d\n", max(dp[][], all - dp[][] + ));
return ;
}
CF 551 D.Serval and Rooted Tree 树形DP的更多相关文章
- Codeforces Round #551 (Div. 2) D. Serval and Rooted Tree (树形dp)
题目:http://codeforces.com/contest/1153/problem/D 题意:给你一棵树,每个节点有一个操作,0代表取子节点中最小的那个值,1代表取子节点中最大的值,叶子节点的 ...
- cf-Round551-Div2-D. Serval and Rooted Tree(DP)
题目链接:https://codeforces.com/contest/1153/problem/D 题意:有一棵树,给定结点数n,在每个结点上的操作(max:表示该结点的number为其孩子结点中的 ...
- CF1153D Serval and Rooted Tree
题目地址:CF1153D Serval and Rooted Tree 挺好玩儿也挺考思维的一道题 思路:树形DP+贪心 数组 \(d\) 维护这样一个值: 对于一个节点 \(x\) ,它的值最大可以 ...
- D. Serval and Rooted Tree (樹狀DP)
Codeforce 1153D Serval and Rooted Tree (樹狀DP) 今天我們來看看CF1153D 題目連結 題目 給一棵數,假設有$k$個葉節點,我們可以給葉節點分配$1$~$ ...
- 熟练剖分(tree) 树形DP
熟练剖分(tree) 树形DP 题目描述 题目传送门 分析 我们设\(f[i][j]\)为以\(i\)为根节点的子树中最坏时间复杂度小于等于\(j\)的概率 设\(g[i][j]\)为当前扫到的以\( ...
- Codeforces Round #551 (Div. 2) D. Serval and Rooted Tree (树形dp)
题目链接 题意:给你一个有根树,假设有k个叶子节点,你可以给每个叶子节点编个号,要求编号不重复且在1-k以内.然后根据节点的max,minmax,minmax,min信息更新节点的值,要求根节点的值最 ...
- CF 461B Appleman and Tree 树形DP
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other ...
- hdu-5834 Magic boy Bi Luo with his excited tree(树形dp)
题目链接: Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
随机推荐
- BFS(宽度优先搜索) -例题
原题地址 https://vjudge.net/contest/313171 密码:algorithm A - Rescue Angel was caught by the MOLIGPY! ...
- js数组排序 多条件
按照[次数]和[时间]排序,选择次数最多的排在前面,同样次数的情况下时间较新排在前面. 原始数据: var arr= [ {name:'qqq', num:2,time:'2015-06-08 13: ...
- 【nodejs原理&源码赏析(9)】用node-ssh实现轻量级自动化部署
目录 一. 需求描述 二. 预备知识 IP+端口访问 域名访问 三. Nodejs应用的手动部署 四. 基于nodejs的自动部署 4.1 package.json中的scripts 4.2 自动化发 ...
- 【有容云】PPT | 容器与CICD的遇见
编者注:本文为12月21日晚上8点有容云高级咨询顾问蒋运龙在腾讯课堂中演讲的PPT,本次课堂为有容云主办的线上直播Docker Live时代●Online Meetup-第四期:容器与CICD的遇见, ...
- oracle-11g2下载安装笔记
一.下载链接地址 http://download.oracle.com/otn/nt/oracle11g/112010/win64_11gR2_database_1of2.zip http://dow ...
- hive分桶表bucketed table分桶字段选择与个数确定
为什么分桶 (1)获得更高的查询处理效率.桶为表加上了额外的结构,Hive 在处理有些查询时能利用这个结构.具体而言,连接两个在(包含连接列的)相同列上划分了桶的表,可以使用 Map 端连接 (Map ...
- 第十五章 LVM管理和ssm存储管理器使用 随堂笔记
第十五章 LVM管理和ssm存储管理器使用 本节所讲内容: 15.1 LVM的工作原理 15.2 创建LVM的基本步骤 15.3 实战-使用SSM工具为公司的邮件服务器创建可动态扩容的存储池 LVM的 ...
- python basemap readshapefile二三事
今天要用到basemap读取shp文件报错,查了很多资料,都没有解决. 先是: fig,ax = plt.subplots(figsize=(15,10)) from mpl_toolkits.bas ...
- 聊一聊 SpringBoot 自动配置的原理
解析思路 我们建立好一个SpringBoot的工程后,我们将从启动类,SpringBootApplication开始进行探究. 开始解析 首先我们建立一个 Springboot的工程.找到启动类,我们 ...
- CGI getenv 参数
变量名 描述 CONTENT_TYPE 这个环境变量的值指示所传递来的信息的MIME类型.目前,环境变量CONTENT_TYPE一般都是:application/x-www-form-urlencod ...