VK Cup 2012 Round 1 D. Distance in Tree (树形dp)
题目:http://codeforces.com/problemset/problem/161/D
题意:给你一棵树,问你两点之间的距离正好等于k的有多少个
思路:这个题目的内存限制首先大一倍,他有5*1e5个点,k的范围是<=500,首先暴力n^2肯定不行,这个题其实很容易看出是树形dp
首先k的范围只有500,我们可以开一个dp[1e5][500]的dp数组,代表以这个点为上端点的所有情况,然后最后递归到1点处,dp[1][k]就是答案
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#define maxn 50005
#define mod 1000000007
using namespace std;
typedef long long ll;
vector<int> mp[maxn];
ll n,k,num;
ll q[maxn][];
void dfs(int x,int f)
{
for(int i=;i<mp[x].size();i++){
int u=mp[x][i];
if(u==f) continue;
dfs(u,x);
q[u][]=;//每到一个新点,他的孩子到他的距离都是1
for(int j=k-;j>=;j--){
if(q[u][j])
{
q[u][j+]=q[u][j+]+q[u][j];
q[u][j]=;
q[x][k]+=q[u][j+]*q[x][k-j-];//如果当前点不是上端点,只是两个孩子之间要过度的点
}
}
for(int j=;j<=k;j++){
q[x][j]=q[x][j]+q[u][j];
}
}
/*printf("%d:",x);
for(int i=0;i<=k;i++)
{
printf(" %d",q[x][i]);
}
printf("\n");*/
}
int main()
{
cin>>n>>k;
int x,y;
for(int i=;i<n-;i++){
cin>>x>>y;
mp[x].push_back(y);
mp[y].push_back(x);
}
dfs(,-);
/*for(int i=1;i<=n;i++)
{
printf("i:%d",i);
for(int j=0;j<=k;j++)
{
printf(" %d",q[i][j]);
}
printf("\n");
}*/
cout<<q[][k];
}
VK Cup 2012 Round 1 D. Distance in Tree (树形dp)的更多相关文章
- 【树形dp】VK Cup 2012 Round 1 D. Distance in Tree
统计树中长度为K的路径条数. 用f[u][k]表示从u结点的子树中出发,终止于u结点的长度为k的路径条数. 边dp边统计答案.为了防止重复统计,在枚举子节点的时候,先将该子节点和当前u结点(和前面已经 ...
- VK Cup 2012 Round 3 (Unofficial Div. 2 Edition)
VK Cup 2012 Round 3 (Unofficial Div. 2 Edition) 代码 VK Cup 2012 Round 3 (Unofficial Div. 2 Edition) A ...
- 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 ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
- Codeforces VK Cup 2012 Round 3 A. Variable, or There and Back Again(dfs)
题目链接:http://codeforces.com/problemset/problem/164/A 思路:用vector分别保留原图和发图,然后分别从val值为1的点正向遍历,va值为2的点反向遍 ...
- 【树形dp】Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) B. Bear and Tree Jumps
我们要统计的答案是sigma([L/K]),L为路径的长度,中括号表示上取整. [L/K]化简一下就是(L+f(L,K))/K,f(L,K)表示长度为L的路径要想达到K的整数倍,还要加上多少. 于是, ...
- CF 161D Distance in Tree 树形DP
一棵树,边长都是1,问这棵树有多少点对的距离刚好为k 令tree(i)表示以i为根的子树 dp[i][j][1]:在tree(i)中,经过节点i,长度为j,其中一个端点为i的路径的个数dp[i][j] ...
- DP VK Cup 2012 Qualification Round D. Palindrome pairs
题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...
- Codeforces Round VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM 暴力出奇迹!
VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM Time Lim ...
随机推荐
- 【python数据分析】利用Anaconda在window上搭建数据分析环境
由于在进行数据分析过程中,需要安装一些第三方库,导致python总会报一些错误,现将通过利用Anaconda搭建数据分析环境,已测可用. 1.到官网上下载python:https://www.pyth ...
- python数据类型(数字\字符串\列表)
一.基本数据类型——数字 1.布尔型 bool型只有两个值:True和False 之所以将bool值归类为数字,是因为我们也习惯用1表示True,0表示False. (1)布尔值是False的各种情况 ...
- Android DataBinding实现地址三联动
这篇文章主要是写关于Android实现地址三联动的功能,现在附上demo地址:https://github.com/qiuyueL/NewAddressDemo,里面会有详细的注释,以及控件的使用,其 ...
- CSS基础语法与选择器
CSS基础 语法 : <head> <style type="text/css"> 选择器(即修饰对象){ 修饰属性:属性值; 修饰属性:属性值; } &l ...
- 在windows环境下运行compass文件出现的错误提示解决方案
在windows环境下运行compass文件出现的错误提示解决方案 例如:经常在项目中运行grunt命令编译scss文件的时候,会出现下面的错误提示 (Encoding::CompatibilityE ...
- angular怎么样注销事件
angular怎么样注销事件 $scope.$on("$destroy", function() { //清除配置,不然scroll会重复请求 }) 在Controller中监听$ ...
- Apache Flume
An Event is a unit of data that flows through a Flume agent. The Event flows from Source to Channel ...
- Java JSONArray的封装与解析
package com.kigang.test; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import java.ut ...
- Scope_Pre_Post
@Scope , 设置bean的生命周期,示例: @Scope(value="prototype")//设置生存范围,一般用 singleton或prototype 14.@P ...
- Informatica 9.1.0 Domain地址变化修改
由于公司机房的变动,infa所连数据库的IP地址变化,致使INFA不能启动.经过查找资料终于解决,现分享给大家,解决方法如下: 1.查看日志路径: Informatica/9.1.0/tomcat/l ...