题面

题意:给你一棵树,你起点在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的更多相关文章

  1. Educational Codeforces Round 33 (Rated for Div. 2) F. Subtree Minimum Query(主席树合并)

    题意 给定一棵 \(n\) 个点的带点权树,以 \(1\) 为根, \(m\) 次询问,每次询问给出两个值 \(p, k\) ,求以下值: \(p\) 的子树中距离 \(p \le k\) 的所有点权 ...

  2. 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 ...

  3. Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块

    Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块 [Problem Description] ​ ...

  4. 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 ...

  5. Educational Codeforces Round 52 (Rated for Div. 2)

    题目链接 A. Vasya and Chocolate 题意 已知钱,价格,赠送规则求最多获得巧克力数 思路常规算即可 代码 #include <bits/stdc++.h> #defin ...

  6. Educational Codeforces Round 52 (Rated for Div. 2) -C

    #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...

  7. 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)的个数,那么实 ...

  8. 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\),可加 ...

  9. Educational Codeforces Round 42 (Rated for Div. 2)F - Simple Cycles Edges

    http://codeforces.com/contest/962/problem/F 求没有被两个及以上的简单环包含的边 解法:双联通求割顶,在bcc中看这是不是一个简单环,是的话把整个bcc的环加 ...

随机推荐

  1. Distinct Subsequences (dp)

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  2. Spring MVC异常处理实例

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/mvc-framework/spring-exception-handling-example.ht ...

  3. 微信接入登录功能access_token流程记录

    提示:只有认证过的订阅号或者服务号才能获取access_token. 1.app微信登录第一步是,app调起来微信客户端,通过app端的配置,引入一个微信类库, 2.授权成功后,微信会返回你一个cod ...

  4. 【APUE】信号

    一.信号概念 信号都被定义为正整数,不存在编号为0的信号. 信号是异步事件的经典实例.产生信号的事件对进程而言是随机出现的,进程不能简单地测试一个变量来判别是否出现了一个信号,而是必须告诉内核在此信号 ...

  5. 关于rman duplicate 一些比較重要的知识点--系列三

    FYI: http://docs.oracle.com/cd/E11882_01/backup.112/e10643/rcmsynta020.htm#RCMRF126 rman duplicate d ...

  6. hdu 4950 Monster(数学题,多校8)

    题目链接:pid=4950http://acm.hdu.edu.cn/showproblem.php?pid=4950">http://acm.hdu.edu.cn/showprobl ...

  7. #define中的#和##作用

    #define语句中的#是把参数字符串化,##是连接两个参数成为一个整体. #define FACTORY_REF(name) { #name, Make##name } 中#name就是将传入的na ...

  8. 【Mongodb教程 第一课 补加课】 Failed to connect to 127.0.0.1:27017, reason: errno:10061 由于目标计算机积极拒绝,无法连接

    1:启动MongoDB 2014-07-25T11:00:48.634+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errn ...

  9. Selenium系列之--测试框架断言【转】

    selenium提供了三种模式的断言:assert .verify.waitfor 1)Assert(断言) 失败时,该测试将终止. 2)Verify(验证) 失败时,该测试将继续执行,并将错误记入日 ...

  10. ubuntu 安装后要做的事情

    1. 安装chrome,软件中心就可以. 2. 安装vim 和一些插件.这里引入一大牛配置的插件集 sudo apt-get install vim-gtk wget -qO- https://raw ...