codeforces 1065F Up and Down the Tree
题目链接:codeforces 1065F Up and Down the Tree
题意:给出一棵树的节点数\(n\)以及一次移动的最大距离\(k\),现在有一个标记在根节点1处,每一次可以进行一下的两个操作之一:
1、将标记移动至当前节点的子树中的某一个叶子
2、将当前标记向上移,向上移的距离不得超过\(k\)
求最多可以访问到多少个叶子结点
分析:一看就知道应该用树形dp去维护它
我们记\(dp[u]\)表示以\(u\)为根节点的子树中最多可以访问多少个叶子结点
\(dp[u]\)由两部分组成:一是跳到下面的节点再跳回\(u\)的叶子结点个数,二是跳到\(u\)的某一棵子树中不再跳回\(u\)时可以访问到的叶子结点个数
为了维护这个我们再记两个辅助数组\(dis[u]\)表示距离\(u\)最近的叶子结点的距离,\(back[u]\)表示在\(u\)的子树中跳到\(u\)再跳回来\(u\)时可以访问到的叶子结点个数
那么这两个数组的维护是显而易见的
对于\(dp\)数组的维护,第一部分就是\(back[u]\),直接加上即可
对于第二部分,我们要考虑的是\(u\)应该往哪一棵子树跳,由于能跳回来的已经在1中计算过了,我们在这里也就不能考虑这一部分,因此是选取最大的\(dp[v]-back[v]\)去跳
最后注意在\(dis[u]\geq k\)时,由于此时的\(back[u]\)已经对它的父亲节点不会再存在贡献,直接清零即可
#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
using namespace std;
const int maxd=1e9+7;
struct node{
int to,nxt;
}sq[2001000];
int n,k,head[1001000],all=0,dp[1001000],dis[1001000],back[1001000];
int read()
{
int x=0,f=1;char ch=getchar();
while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
return x*f;
}
void add(int u,int v)
{
all++;sq[all].to=v;sq[all].nxt=head[u];head[u]=all;
}
void dfs(int u,int fa)
{
dis[u]=maxd;int i;
for (i=head[u];i;i=sq[i].nxt)
{
int v=sq[i].to;
if (v==fa) continue;
dfs(v,u);
dis[u]=min(dis[u],dis[v]+1);
back[u]+=back[v];
dp[u]=max(dp[u],dp[v]-back[v]);
}
dp[u]+=back[u];
if (dis[u]==maxd) {dis[u]=0;dp[u]=back[u]=1;}
if (dis[u]>=k) back[u]=0;
}
int main()
{
n=read();k=read();
int i;
for (i=2;i<=n;i++)
{
int v=read();add(i,v);add(v,i);
}
memset(dp,0,sizeof(dp));
memset(dis,0,sizeof(dis));
memset(back,0,sizeof(back));
dfs(1,0);
printf("%d",dp[1]);
return 0;
}
codeforces 1065F Up and Down the Tree的更多相关文章
- Codeforces 914H Ember and Storm's Tree Game 【DP】*
Codeforces 914H Ember and Storm's Tree Game 题目链接 ORZ佬 果然出了一套自闭题 这题让你算出第一个人有必胜策略的方案数 然后我们就发现必胜的条件就是树上 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Up and Down the Tree CodeForces - 1065F (树形dp)
链接 题目大意:给定$n$结点树, 假设当前在结点$v$, 有两种操作 $(1)$移动到$v$的子树内任意一个叶子上 $(2)$若$v$为叶子, 可以移动到距离$v$不超过$k$的祖先上 初始在结点$ ...
- Educational Codeforces Round 6 E. New Year Tree dfs+线段树
题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树
题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...
- Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA链上最大值
E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...
- Codeforces 600E. Lomsat gelral(Dsu on tree学习)
题目链接:http://codeforces.com/problemset/problem/600/E n个点的有根树,以1为根,每个点有一种颜色.我们称一种颜色占领了一个子树当且仅当没有其他颜色在这 ...
- Codeforces Round #540 (Div. 3)--1118F1 - Tree Cutting (Easy Version)
https://codeforces.com/contest/1118/problem/F1 #include<bits/stdc++.h> using namespace std; in ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 模拟
D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...
随机推荐
- filebeat 源码编译安装
下载filebeat源码(6.2.3)下载地址:链接: https://pan.baidu.com/s/1cPR7-xlQJuYZ77uaUpfSpQ 提取码: k77u github下载地址:htt ...
- elasticsearch5.0版本的head安装
elasticsearch5.0版本的head安装 elasticsearch5.0版本由于刚出不久,并且与2.0版本的差距较大.所以,目前大家对5.0的一些使用还有所陌生.这里先把关于head插件的 ...
- Java 读取配置文件数据
Properties类 Properties类,是一个工具类,包含在java.util包中. 功能:可以保存持久的属性,通常用来读取配置文件或者属性文件,将文件中的数据读入properties对象中, ...
- ERP & CRM
ERP流程_百度百科https://baike.baidu.com/item/ERP%E6%B5%81%E7%A8%8B/8099248 CRM_百度百科https://baike.baidu.com ...
- js 正则进阶regexp
一.匹配中文,英文字母和数字及_: const reg = /^[\u4e00-\u9fa5\w]+$/; const str1 = 'shangyy'; const str2 = '尚悦悦ww123 ...
- Storm原理
zookeeper是对称结构
- python学习笔记(11)--文件与数据格式化
文件的概念: 文件是数据的抽象和集合,是存储在辅助存储器上的数据序列,文件是数据存储的一种形式,文件的展现形态,文本文件和二进制文件. 文本文件输出: f.txt文件保存:“我是中国人” >&g ...
- zabbix添加监控Mysql
起因:zabbix自带的mysql监控模板直接使用会显示“不支持的”因为key的值是通过Mysql用户查看"show global status"信息或者用mysqladmin命令 ...
- Python学习之路——Day06 元组
一.元组 t1 = (1, 2) t2 = tuple((1, 2)) t3 = (1, ) # 索引 | 切片 | 长度 # .count(obj) | .index(obj, bIndex, eI ...
- Spring Boot 构建电商基础秒杀项目 (十一) 秒杀
SpringBoot构建电商基础秒杀项目 学习笔记 新建表 create table if not exists promo ( id int not null auto_increment, pro ...