题目链接:

http://codeforces.com/contest/161/problem/D

D. Distance in Tree

time limit per test 3 seconds
memory limit per test 512 megabytes
#### 问题描述
> A tree is a connected graph that doesn't contain any cycles.
>
> The distance between two vertices of a tree is the length (in edges) of the shortest path between these vertices.
>
> You are given a tree with n vertices and a positive number k. Find the number of distinct pairs of the vertices which have a distance of exactly k between them. Note that pairs (v, u) and (u, v) are considered to be the same pair.
#### 输入
> The first line contains two integers n and k (1 ≤ n ≤ 50000, 1 ≤ k ≤ 500) — the number of vertices and the required distance between the vertices.
>
> Next n - 1 lines describe the edges as "ai bi" (without the quotes) (1 ≤ ai, bi ≤ n, ai ≠ bi), where ai and bi are the vertices connected by the i-th edge. All given edges are different.
#### 输出
> Print a single integer — the number of distinct pairs of the tree's vertices which have a distance of exactly k between them.
>
> Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
#### 样例
> **sample input**
> 5 2
> 1 2
> 2 3
> 3 4
> 2 5
>
> **sample output**
> 4

题意

给你一颗树,每条边长为1,求所有距离为k的顶点对,(u,v)和(v,u)算一对。

题解

树形dp:

dp[i][j]表示与第i个节点距离为j的节点数。

两次dfs:

第一次求以i为根的子树中与i距离为j的节点数dp[i][j]。

第二次求i与不在i的子树中的节点金额距离为j的节点数。

两次加起来就是表示与i节点距离为j的所有的树上节点数。

答案就是sigma(dp[i][k])。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#define lson (o<<1)
#define rson ((o<<1)|1)
#define M (l+(r-l)/2)
using namespace std; const int maxn=5e4+10;
const int maxm=555; typedef __int64 LL; int n,k;
LL dp[maxn][maxm];
vector<int> G[maxn]; void dfs1(int u,int fa) {
dp[u][0]=1;
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(v==fa) continue;
dfs1(v,u);
for(int j=0;j+1<=k;j++){
dp[u][j+1]+=dp[v][j];
}
}
} LL tmp[maxm];
void dfs2(int u,int fa) {
if(fa!=-1){
tmp[0]=dp[fa][0];
for(int j=1;j<=k;j++){
tmp[j]=dp[fa][j]-dp[u][j-1];
}
for(int j=0;j+1<=k;j++){
dp[u][j+1]+=tmp[j];
}
}
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(v==fa) continue;
dfs2(v,u);
}
} int main() {
scanf("%d%d",&n,&k);
memset(dp,0,sizeof(dp));
for(int i=0; i<n-1; i++) {
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs1(1,-1);
dfs2(1,-1);
LL ans=0;
for(int i=1;i<=n;i++){
ans+=dp[i][k];
}
printf("%I64d\n",ans/2);
return 0;
}

codeforces 161D Distance in Tree 树形dp的更多相关文章

  1. CF 161D Distance in Tree 树形DP

    一棵树,边长都是1,问这棵树有多少点对的距离刚好为k 令tree(i)表示以i为根的子树 dp[i][j][1]:在tree(i)中,经过节点i,长度为j,其中一个端点为i的路径的个数dp[i][j] ...

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

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

  3. VK Cup 2012 Round 1 D. Distance in Tree (树形dp)

    题目:http://codeforces.com/problemset/problem/161/D 题意:给你一棵树,问你两点之间的距离正好等于k的有多少个 思路:这个题目的内存限制首先大一倍,他有5 ...

  4. CodeForces 161D Distance in Tree【树形DP】

    <题目链接> 题目大意:一颗无向无环树,有n个顶点,求其中距离为k的点对数是多少,(u,v)与(v,u)为同一点对. #include <cstdio> #include &l ...

  5. Codeforces 461B. Appleman and Tree[树形DP 方案数]

    B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  6. codeforces 161D Distance in Tree 树上点分治

    链接:https://codeforces.com/contest/161/problem/D 题意:给一个树,求距离恰好为$k$的点对是多少 题解:对于一个树,距离为$k$的点对要么经过根节点,要么 ...

  7. Codeforces 161D Distance in Tree(树的点分治)

    题目大概是,给一棵树,统计距离为k的点对数. 不会DP啊..点分治的思路比较直观,啪啪啪敲完然后AC了.具体来说是这样的: 树上任何两点的路径都可以看成是一条过某棵子树根的路径,即任何一条路径都可以由 ...

  8. codeforces 416B. Appleman and Tree 树形dp

    题目链接 Fill a DP table such as the following bottom-up: DP[v][0] = the number of ways that the subtree ...

  9. Codeforces 161D Distance in Tree

    题目大意:给出一棵n个节点的树,统计树中长度为k的路径的条数(1<=n<=50000 , 1<=k<=500) 思路:树分治! #include<cstdio> # ...

随机推荐

  1. linux下不重启添加硬盘

    linux下热加载磁盘 临时给虚拟机加了一块硬盘,增加后懒得重启,于是看了看热加载 [root@centos5 ~]# cat /proc/scsi/scsiAttached devices:Host ...

  2. Windows Server 2003 增加远程用户个数

    1.进入“开始”-“控制面板”-“添加或删除程序”如下图: 2.点击“添加/删除windows组件(A)”,进入如下图 如果该服务器没有安装“终端服务器”和“终端服务器授权”组件的话,则需要使用系统光 ...

  3. PHP高级笔记汇总

    一.PHP日期 PHP的date()函数用于格式化时间或日期.PHP Date()函数可把时间戳格式化为可读性更好的日期和时间.语法:date(format,timestamp)format:必需.规 ...

  4. 【Window】Tor(洋葱头路由)+Privoxy 网络实践(附带Java实例代码)

    1.背景 平时我们需要访问onion后缀的网站,需要通过Tor(The Onion Router,洋葱路由器).一般来说安装Tor Broswer就可以满足需要.但是项目我要做的是通过程序来获取oni ...

  5. ASCII码详解

    ASCII码详解 ASCII码表 ASCII码大致可以分作三部分組成.第一部分是:ASCII非打印控制字符: 第二部分是:ASCII打印字符: 第三部分是:扩展ASCII打印字符. 第一部分:ASCI ...

  6. Mongodb 3.0 创建用户

    MongoDB 3.0 安全权限访问控制,在添加用户上面3.0版本和之前的版本有很大的区别,这里就说明下3.0的添加用户的方法. 创建第一个用户(该用户需要有grant权限,即:账号管理的授权权限) ...

  7. Android开发初始

    由于本人一直的主攻方向是.NET平台,所以移动开发方面主要是Windows Phone平台,但是确实Windows Phone的市场占有率太小了,在加上本人是个技术迷,希望尝试新的东西,所以Andro ...

  8. OpenStack的bridge_sto off的解释

    北京_张华(28620211) 2014/1/27 星期一 11:10:57传统的网络是通过STP协议来避免交换机在二层形成环路,但是对于虚拟交换机,因为有东西向的流量,故将bridge_stp设成o ...

  9. Golang与MySQL

    1. 在golib下载go-sql-driver/mysql go get github.com/go-sql-driver/mysql 2. 代码引入 import ( "database ...

  10. iOS另类的内存管理

    iOS的内存管理算是老生常谈的问题了,我们写iOS的时候无时无刻不在涉及到内存管理.从开始的MRR(manual retain-release)到后来ARC(Automatic Reference C ...