CodeForces 161D Distance in Tree【树形DP】
<题目链接>
题目大意:
一颗无向无环树,有n个顶点,求其中距离为k的点对数是多少,(u,v)与(v,u)为同一点对。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; #define N int(5e4+10)
int n,k,ans,cnt;
int dp[N][],head[N];
struct Edge{
int to,nxt;
}edge[N<<];
void init(){
cnt=;ans=;
memset(head,-,sizeof(head));
memset(dp,,sizeof(dp));
}
void addedge(int u,int v){
edge[cnt].to=v,edge[cnt].nxt=head[u];
head[u]=cnt++;
}
void dfs(int u,int fa){
dp[u][]=; //dp[i][j]表示以u为根的子树中,距u的距离为j的点的数量
for(int i=head[u];~i;i=edge[i].nxt){
int v=edge[i].to;
if(v==fa)continue;
dfs(v,u);
for(int j=;j<k;j++)ans+=dp[u][j]*dp[v][k-j-];
for(int j=;j<=k;j++)dp[u][j]+=dp[v][j-];
}
}
int main(){
while(~scanf("%d%d",&n,&k)){
init();
for(int i=;i<n;i++){
int u,v;scanf("%d%d",&u,&v);
addedge(u,v);addedge(v,u);
}
dfs(,-);
printf("%d\n",ans);
}
}
2019-02-07
CodeForces 161D Distance in Tree【树形DP】的更多相关文章
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
- 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 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> # ...
随机推荐
- chkconfig: command not found
问题描述 Ubuntu 16.04 下安装 Nginx 服务器,在添加 nginx 服务时出现如下信息 # chkconfig --add nginx chkconfig: command not f ...
- Confluence 6 Oracle 创建数据库用户
创建用户后并且指派权限: 使用 sqlplus 命令行工具通过命令行来访问 Oracle sqlplus user/password <as sysdba|as sysoper> 如果你的 ...
- Max Sum (dp)
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. F ...
- Practical Web Penettation Testing (the first one Mutillidae 大黄蜂 之二)
1.how to use dpkg cmmand first it can be used for list all software , dpkg -l (由于kali linux 没有启动所以 ...
- cf1042d 树状数组逆序对+离散化
/* 给定一个数组,要求和小于t的段落总数 求前缀和 dp[i]表示以第i个数为结尾的小于t的段落总数,sum[i]-sum[l]<t; sum[i]-t<sum[l],所以只要找到满足条 ...
- 用来表达更复杂的sql语句!!!!!extra 原声sql
extra 用来表达更复杂的sql语句!!!!! extra可以指定一个或多个 参数,例如 select, where or tables. 这些参数都不是必须的,但是你至少要使用一个!要注意这些额外 ...
- 简单的做一个图片上传预览(web前端)
转载:点击查看原文 在做web项目很多的时候图片都是避免不了的,所以操作图片就成了一个相对比较棘手的问题,其实也不是说很麻烦,只是说上传然后直接预览的过程很恶心,今天简单的做一个处理. 效果预览: & ...
- Java 获取窗口的宽、高
创建一个新窗口,通过getSize()获取这个窗口的宽.高. import javax.swing.JFrame; public class WindowInTheMiddle extends JFr ...
- java数组元素的复制
package day03; import java.util.Arrays; /** * * 数组元素的复制: int的默认值是0,boolean默认值是flase 数组的扩容和缩容(本质的实现数组 ...
- vetur插件提示 [vue-language-server] Elements in iteration expect to have 'v-bind:key' directives错误的解决办法
错误提示: [vue-language-server] Elements in iteration expect to have 'v-bind:key' directives.Renders the ...