题目链接: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)的更多相关文章

  1. codeforces 161 D. Distance in Tree(树形dp)

    题目链接:http://codeforces.com/problemset/problem/161/D 题意:给出一个树,问树上点到点的距离为k的一共有几个. 一道简单的树形dp,算是一个基础题. 设 ...

  2. Codeforces 219D. Choosing Capital for Treeland (树dp)

    题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

  3. Codeforces 161D Distance in Tree(树型DP)

    题目链接 Distance in Tree $k <= 500$ 这个条件十分重要. 设$f[i][j]$为以$i$为子树,所有后代中相对深度为$j$的结点个数. 状态转移的时候,一个结点的信息 ...

  4. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

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

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

  7. [CF1060F]Shrinking Tree[树dp+组合计数]

    题意 你有一棵 \(n\) 个点的树,每次会随机选择树上的一条边,将两个端点 \(u,v\) 合并,新编号随机为 \(u,v\).问最后保留的编号分别为 \(1\) 到 \(n\) 的概率. \(n\ ...

  8. CodeForces 160D - Distance in Tree 树型DP

    题目给了512MB的空间....用dp[k][i]代表以k为起点...往下面走(走直的不打岔)i步能有多少方案....在更新dp[k][i]过程中同时统计答案.. Program: #include& ...

  9. D. Distance in Tree(树型Dp计数)

    \(其实思路都能想到一点,就是去重这里特别麻烦,没有好的思路.\) \(设dp[i][j]为以i为根深度为j的节点数量\) \(dp[parent][j]=\sum{dp[son][j-1]}\) \ ...

随机推荐

  1. Jenkins+Maven+SVN搭建自动部署、自动测试环境

    .打开http://10.3.15.78:8080/jenkins/,第一次进入里面没有数据,我们需要创建job,我们这有2个项目,需要创建2个job.http://10.3.34.163:9890/ ...

  2. [转][TFS] 禁止默认允许多人签出和强制解除签入签出锁

    转自:http://blog.xieyc.com/tfs-disable-multiple-check-out-and-force-to-undo-locking/ | 小谢的小站 [TFS] 禁止默 ...

  3. 02.C语言关于指针的学习笔记

    指针是一个特殊的变量,它里面存储的数值被解释成为内存里的一个地址. 要搞清一个指针需要搞清指针的四方面的内容: 指针的类型,指针所指向的 类型,指针的值或者叫指针所指向的内存区,还有指针本身所占据的内 ...

  4. 学会简单使用log4j

    简单配置: ### ??Logger?????????? ### ##log4j.rootLogger=debug, stdout,logfile log4j.rootLogger=debug, st ...

  5. codevs 4927 线段树练习5

    赶在期末考试之前把这道傻逼题调了出来. #include<iostream> #include<cstdio> #include<cstring> #include ...

  6. Maven的功用所引发的哲学思想

    我们知道Maven有三个仓库 本地仓库 ~/.m2/repository/ 每一个用户也可以拥有一个本地仓库 远程仓库 中央仓库:Maven默认的远程仓库 http://repo1.maven.org ...

  7. swun 1397 来电显示

    解题思路:这题最关键的是要注意当输入的数据,00123,0000等这些情况, 刚开始用long long, WA了好几发,非常迷茫,后来突然想起特殊数据, 用字符串,则轻松解决问题.顺便多说两句:当你 ...

  8. 【转】为eclipse安装python、shell开发环境和SVN插件

    原文网址:http://www.crazyant.net/1185.html eclipse是一个非常好用的IDE,通常来说我们都用eclipse来开发JAVA程序,为了让开发python.shell ...

  9. Linux/Unix shell 脚本监控磁盘可用空间

    Linux下监控磁盘的空闲空间的shell脚本,对于系统管理员或DBA来说,必不可少.下面是给出的一个监控磁盘空间空间shell脚本的样本,供大家参考. 1.监控磁盘的空闲空间shell脚本 robi ...

  10. 最全的PHP开发Android应用程序

    第一部分是指在Android系统的手机上直接写PHP脚本代码并立即运行: 第二部分则继续讲解如何把写好的PHP脚本代码打包成akp安装文件. 首先,在手机上安装两个apk包. 一个是SL4A(Scri ...