codeforces 161D Distance in Tree 树形dp
题目链接:
http://codeforces.com/contest/161/problem/D
D. Distance in Tree
time limit per test 3 secondsmemory 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的更多相关文章
- CF 161D Distance in Tree 树形DP
一棵树,边长都是1,问这棵树有多少点对的距离刚好为k 令tree(i)表示以i为根的子树 dp[i][j][1]:在tree(i)中,经过节点i,长度为j,其中一个端点为i的路径的个数dp[i][j] ...
- Codeforces 161D Distance in Tree(树型DP)
题目链接 Distance in Tree $k <= 500$ 这个条件十分重要. 设$f[i][j]$为以$i$为子树,所有后代中相对深度为$j$的结点个数. 状态转移的时候,一个结点的信息 ...
- VK Cup 2012 Round 1 D. Distance in Tree (树形dp)
题目:http://codeforces.com/problemset/problem/161/D 题意:给你一棵树,问你两点之间的距离正好等于k的有多少个 思路:这个题目的内存限制首先大一倍,他有5 ...
- CodeForces 161D Distance in Tree【树形DP】
<题目链接> 题目大意:一颗无向无环树,有n个顶点,求其中距离为k的点对数是多少,(u,v)与(v,u)为同一点对. #include <cstdio> #include &l ...
- Codeforces 461B. Appleman and Tree[树形DP 方案数]
B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- codeforces 161D Distance in Tree 树上点分治
链接:https://codeforces.com/contest/161/problem/D 题意:给一个树,求距离恰好为$k$的点对是多少 题解:对于一个树,距离为$k$的点对要么经过根节点,要么 ...
- Codeforces 161D Distance in Tree(树的点分治)
题目大概是,给一棵树,统计距离为k的点对数. 不会DP啊..点分治的思路比较直观,啪啪啪敲完然后AC了.具体来说是这样的: 树上任何两点的路径都可以看成是一条过某棵子树根的路径,即任何一条路径都可以由 ...
- 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 ...
- Codeforces 161D Distance in Tree
题目大意:给出一棵n个节点的树,统计树中长度为k的路径的条数(1<=n<=50000 , 1<=k<=500) 思路:树分治! #include<cstdio> # ...
随机推荐
- POJ C++程序设计 编程作业—类和对象 编程题 #2
编程题 #2 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面程序的输出 ...
- SQL Server :DBLINK创建及使用
Exec sp_droplinkedsrvlogin bvtwfld12,Null --若存在先刪除Exec sp_dropserver bvtwfld12EXEC sp_addlinkedserve ...
- aspx页面与服务器控件间运行原理
一.进入服务器控件以及aspx页面前,必须的准备: a)在WebForm 中只要带有runat="server" 那么那就是一个控件. b)如果form有runat="s ...
- kettle的windows安装
1.首先去官网下载安装包,这个安装包在所有平台上是通用的. 2.kettle是java语言开发的,所以需要配置JAVA_HOME 3.解压kettle的安装包 4.配置环境变量,KETTLE_HOME ...
- 重拾C,一天一点点_8
这两天发现一个问题,我最近发的几篇博文稀里糊涂地被转到别的网站去了,目前发现有两个网站转载了,一个注明了作者出处(博客园 lltong),但没给任何链接.另一个网站呢,就是直接抓的,而且还抓的乱七八糟 ...
- apache下ab.exe使用方法。。
自己在cmd中写了半天的路径也没有写对..最后网上的一个哥们告诉我说没有共同语言了...毛线啊 差距确实很大!大能猫死panda早晚干掉你,叫你丫整天嘲讽我! 比如我的ab.exe在D盘的wamp文件 ...
- 随机获取Mysql数据表的一条或多条记录
随机获得Mysql数据表的一条或多条记录有很多方法,下面我就以users(userId,userName,password......)表(有一百多万条记录)为例,对比讲解下几个方法效率问题: sel ...
- 双栈排序(codevs 1170)题解
[问题描述] Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈 ...
- hdu 2660 Accepted Necklace
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2660 Accepted Necklace Description I have N precious ...
- jdk 1.5
1995.5.23 java语言诞生 sun公司推出java语言的同时,也推出了一系列的开发工具,比如JDK(java development Kit)JVMjava API 时间 版本 API 用途 ...