【链接】 我是链接,点我呀:)

【题意】

问你一棵树上有多少条长度为k的路径

【题解】

树形dp

size[i]表示以节点i为根节点的子树的节点个数
dp[i][k]表示以i为根节点的子树里面距离节点i的距离为k的节点有多少个.
长度为k的路径有两种情况.
1.这个路径从x开始,只经过x的一个子树.
2.这个路径经过x,横跨x的两个子树.
第一种情况直接累加dp[x][k]即可
第二种情况,可以枚举其中一棵子树y包括的路径长度i,另外一棵子树(其余size[x]-1棵子树都可以作为另外一棵子树)
自然包括的路径长度就是k-i了
累加0.5*dp[y][i-1]*(dp[x][k-i]-dp[y][k-i-1]);
乘0.5是因为把(x,y)和(y,x)都算一遍
(dp[x][k-i]-dp[y][k-i-1])表示的是除去y子树其余size[x]-1棵子树中和x距离为k-i的节点个数
具体的代码中有注释

【代码】

import java.io.*;
import java.util.*; public class Main { static InputReader in;
static PrintWriter out; public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
} static int N = 50000;
static int K = 500;
static class Task{
long [][]dp;
long ans;
ArrayList []g;
int n,k; void dfs(int x,int fa) {
dp[x][0] = 1;
int len = g[x].size();
for (int i = 0;i < len;i++) {
int y = (int)g[x].get(i);
if (y==fa) continue;
dfs(y,x);
for (int j = 1;j <= k;j++) {
dp[x][j] = dp[x][j] + dp[y][j-1];
}
}
//以x为开始的链
ans = ans + dp[x][k]; //跨过两颗子树
long ans2 = 0;
if (k>=2)//只有长度大于等于2才可能跨过两棵子树
for (int i = 0;i < len;i++) {
int y = (int)g[x].get(i);
if (y==fa) continue;
//以子树y的某个节点作为起点,然后到达x再到达其他位置
for (int l = 1;l<=k-1;l++) {//不能全都在这棵子树里面,得留k-l在另外一个子树
//l是x到y下面的某个节点的长度,比如l=1那么就是到达y节点
//从y的话就是往下l-1长度的点
long temp1 = dp[y][l-1]; //紧接着要找从x出发长度为k-l的点(不能包括y这棵子树)
long temp2 = dp[x][k-l]-dp[y][k-l-1];
ans2 = ans2 + temp1*temp2;
}
}
ans2/=2;//每种都会重复算一次
ans = ans + ans2;
} public void solve(InputReader in,PrintWriter out) {
dp = new long[N+10][K+10];
g = new ArrayList[N+10];
for (int i = 1;i<= N;i++) g[i] = new ArrayList(); n = in.nextInt(); k = in.nextInt(); for (int i = 1;i <= n-1;i++) {
int x,y;
x = in.nextInt();y = in.nextInt();
g[x].add(y);g[y].add(x);
} dfs(1,-1);
out.println(ans);
}
} static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer; public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
} public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
} public int nextInt() {
return Integer.parseInt(next());
}
}
}

【Codeforces 161D】Distance in Tree的更多相关文章

  1. 【27.91%】【codeforces 734E】Anton and Tree

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【codeforces 791D】 Bear and Tree Jumps

    [题目链接]:http://codeforces.com/contest/791/problem/D [题意] 你可以从树上的节点一次最多走k条边. (称为跳一次); 树为无权树; 然后问你任意两点之 ...

  3. 【树形dp】Distance in Tree

    [CF161.D] Distance in Tree time limit per test 3 seconds memory limit per test 512 megabytes A tree  ...

  4. 【19.27%】【codeforces 618D】Hamiltonian Spanning Tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【27.48%】【codeforces 699D】 Fix a Tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. 【Codeforces 1086B】Minimum Diameter Tree

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 统计叶子节点个数m 把每条和叶子节点相邻的边权设置成s/cnt就可以了 这样答案就是2*s/m(直径最后肯定是从一个叶子节点开始,到另外一个叶 ...

  7. 【codeforces 698B】 Fix a Tree

    题目链接: http://codeforces.com/problemset/problem/698/B 题解: 还是比较简单的.因为每个节点只有一个父亲,可以直接建反图,保证出现的环中只有一条路径. ...

  8. 【CodeForces 699D】Fix a Tree

    dfs找出联通块个数cnt,当形成环时,令指向已访问过节点的节点变成指向-1,即做一个标记.把它作为该联通图的根. 把所有联通的图变成一颗树,如果存在指向自己的点,那么它所在的联通块就是一个树(n-1 ...

  9. 【CodeForces 614A】Link/Cut Tree

    题 题意 给你一个区间,求里面有多少个数是k的次方. 分析 暴力,但是要注意这题范围会爆long long,当k=1e8: l=1:r=1e18时 k²=1e16,判断了是≤r,然后输出,再乘k就是1 ...

随机推荐

  1. 转 linux/unix学习经典书籍

    都是一些链接. 1. Linux网络编程经典书籍推荐 http://blog.csdn.net/zhangpeng_linux/article/details/7001970 2. C语言经典著作导读 ...

  2. windows下安装MySQL-python遇到的问题

    执行安装命令 pip install MySQL-python 一.执行时会报一个错误 error: Microsoft Visual C++ 9.0 is required (Unable to f ...

  3. php settype()和gettype()

    gettype()是获得变量的类型,settype()函数用来配置或转换变量类型.成功返回 true 值,其它情形返回 false 值.参数 var 为原来的变量名,参数 type 为下列的类型之一: ...

  4. 如何判断js的变量的数据类型

    文章首发: http://www.cnblogs.com/sprying/p/4349426.html 本文罗列了一般的Js中类型检测的方法,实际上是每个新手在构建Js知识体系时,都要知晓的,而我只是 ...

  5. P3128 [USACO15DEC]最大流Max Flow(LCA+树上差分)

    P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of  pipes to transport mil ...

  6. Django总结一

    HTTPRequest与HTTPresponse 一. 1.互联网两台机器之间通行:ip.端口.协议 - 协议 - HTTP (80) - HTTPS (443) 2.浏览器输入URL一回车返回页面发 ...

  7. hdu2027

    http://acm.hdu.edu.cn/showproblem.php?pid=2027 #include<iostream> #include<stdio.h> #inc ...

  8. 题解报告:hdu 1285 确定比赛名次

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 Problem Description 有N个比赛队(1<=N<=500),编号依次 ...

  9. SVN异常处理(五)-状态小图标不见了

    1.发现问题 装了Win10,再装了Office2016等一些最新软件后,发现SVN状态小图标竟然就不见了 2.分析问题 在Window系统中,当UAC启动时,有些应用程序的图标上会显示一个盾牌,像这 ...

  10. Python随笔-切片

    Python为取list部分元素提供了切片操作,list[begin:end]获取list的[begin,end)区间元素. 可以用负数索引. tuple.str都是list的一种,所以也适用. 可以 ...