Codeforces 161D Distance in Tree(树型DP)
题目链接 Distance in Tree
$k <= 500$ 这个条件十分重要。
设$f[i][j]$为以$i$为子树,所有后代中相对深度为$j$的结点个数。
状态转移的时候,一个结点的信息由他的儿子转移过来。
那么一边进行状态转移,一边统计答案即可。
#include <bits/stdc++.h> using namespace std; int f[][], deep[];
vector <int> v[];
int n, k, x, y;
long long ans; void dfs(int x, int fa, int dep){
int c[];
deep[x] = dep;
for (auto u : v[x]){
if (u == fa) continue;
dfs(u, x, dep + );
memset(c, , sizeof c); c[] = ;
for (int i = ; i <= k; ++i) c[i + ] = f[u][i];
for (int i = ; i <= k - ; ++i) ans += (long long)f[x][i] * c[k - i];
for (int i = ; i <= k; ++i) f[x][i] += c[i];
}
} int main(){ ans = ;
scanf("%d%d", &n, &k);
for (int i = ; i <= n - ; ++i){
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
} memset(deep, , sizeof deep);
dfs(, , );
for (int i = ; i <= n; ++i) if (deep[i] >= k) ++ans;
printf("%lld\n", ans); return ; }
Codeforces 161D Distance in Tree(树型DP)的更多相关文章
- CodeForces 160D - Distance in Tree 树型DP
题目给了512MB的空间....用dp[k][i]代表以k为起点...往下面走(走直的不打岔)i步能有多少方案....在更新dp[k][i]过程中同时统计答案.. Program: #include& ...
- CodeForces 161D Distance in Tree【树形DP】
<题目链接> 题目大意:一颗无向无环树,有n个顶点,求其中距离为k的点对数是多少,(u,v)与(v,u)为同一点对. #include <cstdio> #include &l ...
- D. Distance in Tree(树型Dp计数)
\(其实思路都能想到一点,就是去重这里特别麻烦,没有好的思路.\) \(设dp[i][j]为以i为根深度为j的节点数量\) \(dp[parent][j]=\sum{dp[son][j-1]}\) \ ...
- Codeforces 149D Coloring Brackets(树型DP)
题目链接 Coloring Brackets 考虑树型DP.(我参考了Q巨的代码还是略不理解……) 首先在序列的最外面加一对括号.预处理出DFS树. 每个点有9中状态.假设0位不涂色,1为涂红色,2为 ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
- Codeforces 161D Distance in Tree(树的点分治)
题目大概是,给一棵树,统计距离为k的点对数. 不会DP啊..点分治的思路比较直观,啪啪啪敲完然后AC了.具体来说是这样的: 树上任何两点的路径都可以看成是一条过某棵子树根的路径,即任何一条路径都可以由 ...
- Educational Codeforces Round 52 (Rated for Div. 2) F. Up and Down the Tree 树型DP
题面 题意:给你一棵树,你起点在1,1也是根节点,你每次可以选择去你子树的某个叶子节点,也可以选择,从叶子节点返回距离不超过k的一个根, 也就是说,你从1开始,向下跳,选择一个叶子(就是没有子树的节点 ...
- codeforces 161D Distance in Tree 树上点分治
链接:https://codeforces.com/contest/161/problem/D 题意:给一个树,求距离恰好为$k$的点对是多少 题解:对于一个树,距离为$k$的点对要么经过根节点,要么 ...
- POJ 2486 Apple Tree ( 树型DP )
#include <iostream> #include <cstring> #include <deque> using namespace std; #defi ...
随机推荐
- eeeeeeeeeee
http://58.241.123.38/hot.cdn.baidupcs.com/file/91623e76f776475da9c3223cdac861f0?xcode=68983c005f6e3c ...
- Android 自定义debug.keystore
场景分析: 有时候,我们要使用第三方的服务,需要提供自己的包名以及keystore的sha1值,比如微信支付,百度地图,都需要包名和keystore的sha1值作为唯一标识.这时候我们测试的时候,如果 ...
- Couchbase II( View And Index)
Couchbase II( View And Index) Views view的作用是从没有结构和半结构的数据对象中抽取过滤需要的信息,并生成相关的index信息,通常生成json数据. vie ...
- 【Two Sum】cpp
题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...
- leetcode 【 Find Minimum in Rotated Sorted Array II 】python 实现
题目: Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? W ...
- CentOS 7 编译安装最新版git
安装wget yum install wget -y 下载最新版本的git源码,并解压 cd /usr/local/src/ wget https://mirrors.edge.kernel.org/ ...
- TopK-微博今日热门话题
大纲 TopK on single node TopK on multiple nodes Realtime topK with low QPS Realtime topK with high QPS ...
- 【bzoj4836】[Lydsy2017年4月月赛]二元运算 分治+FFT
题目描述 定义二元运算 opt 满足 现在给定一个长为 n 的数列 a 和一个长为 m 的数列 b ,接下来有 q 次询问.每次询问给定一个数字 c 你需要求出有多少对 (i, j) 使得 a_ ...
- proteus仿真 引脚显示电平变化但不能显示波形
proteus仿真 引脚显示电平变化但不能显示波形 原来是没有选择通道问题,proteus默认优先使用A通道才会显示波形,如果优先使用B,C,D通道,需要选择...
- linux系统——日志文件系统及性能分析
Linux日志文件系统及性能分析 日志文件系统可以在系统发生断电或者其它系统故障时保证整体数据的完整性,Linux是目前支持日志文件系统最多的操作系统之一,本文重点研究了Linux常用的日志文件系统: ...