Codeforces 161 D. Distance in Tree (树dp)
题目链接:http://codeforces.com/problemset/problem/161/D
题意:
给你一棵树,问你有多少对点的距离为k。
思路:
dp[i][j]表示离i节点距离为j的点个数,2次dfs,一次从底向上,另一次从顶向下。
//#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = 1e5 + ;
int dp[][], n, m;
vector <int> G[]; void dfs1(int u, int p) {
dp[u][] = ;
for(int i = ; i < G[u].size(); ++i) {
int v = G[u][i];
if(v == p)
continue;
dfs1(v, u);
for(int i = ; i <= m; ++i) {
dp[u][i] += dp[v][i - ];
}
}
} void dfs2(int u, int p) {
for(int i = ; i < G[u].size(); ++i) {
int v = G[u][i];
if(v == p)
continue;
for(int i = m; i >= ; --i) {
dp[v][i] += dp[u][i - ] - dp[v][i - ];
}
dp[v][]++;
dfs2(v, u);
}
} int main()
{
int u, v;
while(~scanf("%d %d", &n, &m)) {
for(int i = ; i < n; ++i) {
scanf("%d %d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
if(m == ) {
printf("%d\n", n - );
} else {
dfs1(, -);
dfs2(, -);
LL ans = ;
for(int i = ; i <= n; ++i) {
ans += (LL)dp[i][m];
}
printf("%lld\n", ans / );
}
}
return ;
}
Codeforces 161 D. Distance in Tree (树dp)的更多相关文章
- codeforces 161 D. Distance in Tree(树形dp)
题目链接:http://codeforces.com/problemset/problem/161/D 题意:给出一个树,问树上点到点的距离为k的一共有几个. 一道简单的树形dp,算是一个基础题. 设 ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- Codeforces 161D Distance in Tree(树型DP)
题目链接 Distance in Tree $k <= 500$ 这个条件十分重要. 设$f[i][j]$为以$i$为子树,所有后代中相对深度为$j$的结点个数. 状态转移的时候,一个结点的信息 ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
- CodeChef - PRIMEDST Prime Distance On Tree 树分治 + FFT
Prime Distance On Tree Problem description. You are given a tree. If we select 2 distinct nodes unif ...
- Codeforces 161.D. Distance in Tree-树分治(点分治,不容斥版)-树上距离为K的点对数量-蜜汁TLE (VK Cup 2012 Round 1)
D. Distance in Tree time limit per test 3 seconds memory limit per test 512 megabytes input standard ...
- [CF1060F]Shrinking Tree[树dp+组合计数]
题意 你有一棵 \(n\) 个点的树,每次会随机选择树上的一条边,将两个端点 \(u,v\) 合并,新编号随机为 \(u,v\).问最后保留的编号分别为 \(1\) 到 \(n\) 的概率. \(n\ ...
- CodeForces 160D - Distance in Tree 树型DP
题目给了512MB的空间....用dp[k][i]代表以k为起点...往下面走(走直的不打岔)i步能有多少方案....在更新dp[k][i]过程中同时统计答案.. Program: #include& ...
- D. Distance in Tree(树型Dp计数)
\(其实思路都能想到一点,就是去重这里特别麻烦,没有好的思路.\) \(设dp[i][j]为以i为根深度为j的节点数量\) \(dp[parent][j]=\sum{dp[son][j-1]}\) \ ...
随机推荐
- 51nod1495 中国好区间
双指针扫一遍 #include<cstdio> #include<cstring> #include<cctype> #include<algorithm&g ...
- [转载]hao123军事频道首页JQ焦点图
适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗. *本作品由[站长素材]收集整理,转载请注明出处! 下载地址
- 通知(NSNotification)
通知 一个完整的通知一般包含3个属性: - (NSString *)name; // 通知的名称 - (id)object; // 通知发布者(是谁要发布通知) - (NSDictionary *)u ...
- Qt 获取usb设备信息 hacking
/************************************************************************** * Qt 获取usb设备信息 hacking * ...
- &引用的问题
C++中&可以代表引用也代表取地址符. 引用这一方面略微有点头大. 注意: 1)引用的函数可以作为左值 一个简单的作为左值的例子 #include <iostream> using ...
- wx菜单栏
#include "MainFrame.h" BEGIN_EVENT_TABLE(MyFrame,wxFrame) EVT_MENU(wxID_FILE,MyFrame::OnMe ...
- [转载] FFmpeg 错误 C4996: ‘avcodec_alloc_frame’: 被声明为已否决 解决方法
在 Visual Studio 2013 下编写 FFmpeg 程序时出错,错误如下: 出错代码如下: 解决方法为:将 avcodec_alloc_frame() 替换为 av_frame_alloc ...
- UVA 11796 - Dog Distance
题意 两条狗啊,同时跑,,同时结束,各自跑各自的道路,问跑的过程中,他们最大距离和最小距离的差: 方法 恶心一点就是,最大最小距离的求解方法,假设两只狗都只有一条线段要跑,则可以判定在端点处有最大 ...
- EIG集团简单介绍
有朋友会问为什么要介绍EIG集团,他们是干什么的?与域名.主机.IDC行业资讯等有啥关系?EIG集团很牛逼么?带着这些疑问,简单的给大家做个介绍,希望能帮助大家了解这个IDC行业里面的“魔鬼”! EI ...
- PHP中超全局变量$GLOBALS和global的区别
一.超全局变量$GLOBALS PHP超全局变量有很多,如下的都属于超全局变量(Superglobal): $GLOBALS,$_SERVER,$_GET,$_POST,$_FILES,$_COOKI ...