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 ...
随机推荐
- Beam编程系列之Apache Beam WordCount Examples(MinimalWordCount example、WordCount example、Debugging WordCount example、WindowedWordCount example)(官网的推荐步骤)
不多说,直接上干货! https://beam.apache.org/get-started/wordcount-example/ 来自官网的: The WordCount examples demo ...
- 环境搭建:JDK--SSH--VIM--Hadoop--SybaseIQ
放假闲来无事,就自己搭建了一套环境,包含: 工具:ssh,vim 环境:Jdk,Hadoop 在此记录,下次使用 1.工具类 ssh和vim两个常用的工具就是两条命令: vim命令:sudo a ...
- ContextCapture水面约束(水面破洞修复)
[问题描述] 对于水面而言,由于特征点较少,软件在计算时很难匹配正确,导致输出模型的水面通常是支离破碎的.软件针对这种情况提供了一个约束工具,用户手动的为水面添加平面约束后,输出的水面模型就会非常 ...
- nyoj 983 ——首尾相连数组的最大子数组和——————【最大子串和变形】
首尾相连数组的最大子数组和 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给定一个由N个整数元素组成的数组arr,数组中有正数也有负数,这个数组不是一般的数组,其首 ...
- C++程序设计基础(4)宏定义和内联
1.知识点 1.1宏定义 (1)不带参数的宏定义 #define ERROR_MESSAGE -100 #define SECONDS_PER_DAY 60*60*60 (2)带参数宏定义,这种形式称 ...
- Use the list and while to Build Shop car
#Author: Gordonsalary = int(input("请输入你的工资:"))goods = [('0',"Iphone",5000),('1', ...
- 解决iframe IE8透明不兼容
要使 ie8 的 iframe 的透明,需要设置两点: 设置 iframe 的 allowTransparency 属性值为 true: <iframe allowtransparency=&q ...
- IntelliJ IDEA 2017.3.5 安装 lombok-plugin-0.17 失败,通过网络下载总是超时
1.问题: IntelliJ IDEA 2017.3.5 安装 lombok-plugin-0.17 失败,通过网络下载总是超时: 2.原因:IntelliJ IDEA 2017.3.5 目前还不支持 ...
- jquery.rotate.js可选抽奖次数和中奖内容的转盘抽奖demo
需求: 最多可以抽奖5次,而且,每次只会中“2000元理财金”或者“谢谢参与”,其它的不会抽中(哈哈,果然都是套路). 效果如下: 一.页面结构: <div class="g-cont ...
- 慧都启动“正版IDE联合超值推广计划
越来越多的中国软件企业为盗版所害而蒙受巨大损失,正版化意识逐渐兴起.IDE(集成开发环境)是软件开发.编写代码必备工具,而正版IDE更具有运行更稳定.编码更安全.保障更加完善等特点,逾为中国软件行业企 ...