Educational Codeforces Round 52 (Rated for Div. 2) F. Up and Down the Tree 树型DP
题意:给你一棵树,你起点在1,1也是根节点,你每次可以选择去你子树的某个叶子节点,也可以选择,从叶子节点返回距离不超过k的一个根,
也就是说,你从1开始,向下跳,选择一个叶子(就是没有子树的节点),然后可以选择一个距离小于等于k的点跳回去,然后继续跳下去再跳上来,问你最多能去多少个叶子节点,n<=1e6
题解:大概分析就是要找一颗子树,然后叶子节点的到主链上的距离小于等于k的尽量多,这样我们就可以跳下跳上的跳很多次了
h[u]表示u到最近的叶子节点的距离,f[u]表示u能去到最多叶子节点个数(f[1]也就是答案),a[u]表示如果从u跳下去又能跳回来的叶子节点个数
大概描述我们的想法就是,对于每个点,我们先访问它的子树,自下而上,告诉他们,哪些叶子可以跳回来,对于每个点的答案就是,能跳回来的都跳,最后再跳下去不回来
所以对于那些h[u]>=k 的u,a[u]=0,为什么有等号,其实这个a是对他的父亲节点起作用的,他的父亲需要他的a,而他需要的是他的儿子节点.
代码上,学习了台湾人一波,inf的使用和f[v]和a[v]的先减后加使得代码更简洁ORz
#include<bits/stdc++.h>
using namespace std;
#define N 1000006
#define inf ((int)1e9)
int nex[N],head[N],gox[N];
int a[N],f[N],h[N],n,k,x,p;
void build(int a,int b)
{
p++;
nex[p]=head[a];
gox[p]=b;
head[a]=p;
}
void dfs(int u)
{
h[u]=inf;
for (int e=head[u];e;e=nex[e])
{
int v=gox[e];
dfs(v);
h[u]=min(h[u],h[v]+);
a[u]+=a[v];
f[u]=max(f[u],f[v]-a[v]);
}
f[u]+=a[u];
if (h[u]==inf)
{
h[u]=;
f[u]=a[u]=;
}
if (h[u]>=k) a[u]=;
} int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;++i)
{
scanf("%d",&x);
build(x,i);
}
dfs();
printf("%d",f[]);
return ;
}
Educational Codeforces Round 52 (Rated for Div. 2) F. Up and Down the Tree 树型DP的更多相关文章
- Educational Codeforces Round 33 (Rated for Div. 2) F. Subtree Minimum Query(主席树合并)
题意 给定一棵 \(n\) 个点的带点权树,以 \(1\) 为根, \(m\) 次询问,每次询问给出两个值 \(p, k\) ,求以下值: \(p\) 的子树中距离 \(p \le k\) 的所有点权 ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块
Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块 [Problem Description] ...
- Educational Codeforces Round 52 (Rated for Div. 2) E. Side Transmutations
http://codeforces.com/contest/1065/problem/E 数学推导题 #include <bits/stdc++.h> using namespace st ...
- Educational Codeforces Round 52 (Rated for Div. 2)
题目链接 A. Vasya and Chocolate 题意 已知钱,价格,赠送规则求最多获得巧克力数 思路常规算即可 代码 #include <bits/stdc++.h> #defin ...
- Educational Codeforces Round 52 (Rated for Div. 2) -C
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...
- Educational Codeforces Round 50 (Rated for Div. 2) F - Relatively Prime Powers(数学+容斥)
题目链接:http://codeforces.com/contest/1036/problem/F 题意: 题解:求在[2,n]中,x != a ^ b(b >= 2 即为gcd)的个数,那么实 ...
- Educational Codeforces Round 58 (Rated for Div. 2) F dp + 优化(新坑) + 离线处理
https://codeforces.com/contest/1101/problem/F 题意 有n个城市,m辆卡车,每辆卡车有起点\(s_i\),终点\(f_i\),每公里油耗\(c_i\),可加 ...
- Educational Codeforces Round 42 (Rated for Div. 2)F - Simple Cycles Edges
http://codeforces.com/contest/962/problem/F 求没有被两个及以上的简单环包含的边 解法:双联通求割顶,在bcc中看这是不是一个简单环,是的话把整个bcc的环加 ...
随机推荐
- zoj4027 Sequence Swapping
首先容易想到二维方程dp(i,j),表示第i个左括号去匹配到第j个右括号时产生的最大值,但如果如此表示的话,首先需要枚举(i,j)以及一个k即dp(i-1,k). 考虑变化dp(i,j)的表示方法,可 ...
- java基础语法1
一:基础语法之--标识符,修饰符,关键字 1.标识符: 定义:类名.变量名以及方法名都被称为标识符.自定义的名字. 注意: ·所有的标识符都应该以字母(A-Z或者a-z),美元符($).或者下划线(_ ...
- Spring Data JPA 入门篇
Spring Data JPA是什么 它是Spring基于ORM框架(如hibernate,Mybatis等).JPA规范(Java Persistence API)封装的一套 JPA应用框架,可使开 ...
- Linux 思维导图
1.Linux学习路径: 2.Linux桌面介绍: 3.FHS(文件系统目录标准): 以上三张图,都是在学习实验楼上的课程--Linux 基础入门,教程里面看到的. 4.Linux需要特别注意的目录: ...
- php事务操作示例
<?php //数据库连接 $conn = mysql_connect('localhost', 'root', '');mysql_select_db('test', $conn); /* 支 ...
- 在调试C++程序是出现这个问题的解决方案illegal pure syntax, must be '= 0'
笔者在调试c++的时候遇见了这个问题 E:\Data Struct\SqString\新建 文本文档.cpp(5) : error C2258: illegal pure syntax, must b ...
- STM32的精确延时
/*---------------------------------------------------------- 文件名:systick.c 文件描写叙述:sysTick 系统滴答时钟1us中 ...
- 使用PowerShell 创建SharePoint 站点
使用PowerShell 创建SharePoint 站点 在SharePoint开发中,你应该学会使用PowerShell管理SharePoint网站.SharePoint Manag ...
- 如何用css给博客换一个好看的样式
第一步:点击设置,将如下代码复制到页面定制css代码 h3 { color: #fff; background-color: #008eb7; -moz-border-radius: 3px; bor ...
- 当你使用LINQ做底层时,最好设计一个工厂,不要把LINQ的动作暴露给业务层
1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Text; 5: ...