【树形dp】Distance in Tree
3 seconds
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.
5 2
1 2
2 3
3 4
2 5
4
5 3
1 2
2 3
3 4
4 5
2
In the first sample the pairs of vertexes at distance 2 from each other are (1, 3), (1, 5), (3, 5) and (2, 4).
题目大意:树上有N个点,问多少对不同点对(u,v)最短路为K?
试题分析:设dp[N][K]代表从i走j步能到达多少点。
初始化:dp[i][0]=1;//它不走可以到它自己
转移一步:dp[i][j]=sum(dp[i->son][j-1]);
统计答案分两步,一步是从i走K步能到达的点:dp[i][K]
一步是以i为最近公共祖先的点对:dp[i->son][t-1]*(dp[i][K-t]-dp[i->son][K-t-1]);
因为u,v v,u算一对,所以ans最后加上tmp/2;
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int MAXN=100001;
const int INF=999999;
int N,K;
long long dp[50001][501];
vector<int> vec[50001];
long long ans; void dfs(int x,int fa){
dp[x][0]=1;
for(int i=0;i<vec[x].size();i++){
if(vec[x][i]==fa) continue;
dfs(vec[x][i],x);
}
for(int i=0;i<vec[x].size();i++){
if(vec[x][i]==fa) continue;
for(int j=1;j<=K;j++) dp[x][j]+=dp[vec[x][i]][j-1];
}
ans+=dp[x][K]; long long tmp=0;
for(int i=0;i<vec[x].size();i++){
if(vec[x][i]!=fa)
for(int j=1;j<K;j++) tmp+=(dp[vec[x][i]][j-1]*(dp[x][K-j]-dp[vec[x][i]][K-j-1]));
}
ans+=(tmp/2);
return ;
} int main(){
N=read(),K=read();
for(int i=1;i<N;i++){
int u=read(),v=read();
vec[u].push_back(v);
vec[v].push_back(u);
}
dfs(1,-1);
printf("%d\n",ans);
}
【树形dp】Distance in Tree的更多相关文章
- 【树形dp】Apple Tree
[poj2486]Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10800 Accepted: 3 ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
- 【树形dp】【CF161D】distance on a tree + 【P1352】没有上司的舞会
T1题面: 输入点数为N一棵树 求树上长度恰好为K的路径个数 (n < 1e5, k < 500) 这是今天的考试题,也是一道假的紫题,因为我一个根本不会dp的蒟蒻只知道状态就一遍A掉了- ...
- VK Cup 2012 Round 1 D. Distance in Tree (树形dp)
题目:http://codeforces.com/problemset/problem/161/D 题意:给你一棵树,问你两点之间的距离正好等于k的有多少个 思路:这个题目的内存限制首先大一倍,他有5 ...
- [codeforces161D]Distance in Tree(点分治/树形dp)
题意:求树上距离为k的点对个数: 解题关键:练习一下点分治不用容斥 而直接做的做法.注意先查询,后更新. 不过这个方法有个缺陷,每次以一个新节点为根,必须memset mp数组,或许使用map会好些, ...
- codeforces 161 D. Distance in Tree(树形dp)
题目链接:http://codeforces.com/problemset/problem/161/D 题意:给出一个树,问树上点到点的距离为k的一共有几个. 一道简单的树形dp,算是一个基础题. 设 ...
- Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS换根 || 树形dp】
传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 sec ...
- HDU5834 Magic boy Bi Luo with his excited tree(树形DP)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5834 Description Bi Luo is a magic boy, he also ...
- hdu-5834 Magic boy Bi Luo with his excited tree(树形dp)
题目链接: Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: ...
随机推荐
- send,recv,sendto,recvfrom ~转载
send,recv,sendto,recvfrom send函数 int send( SOCKET s, const char FAR *buf, int len, int fla ...
- Python阶段复习 - part 4 - 用户登录程序
简易版: #!/usr/bin/env python # _*_ coding:UTF-8 _*_ # __auth__:Dahlhin import sys userinfo = r'userinf ...
- 蓝屏代码0X0000007B可能是SATA mode问题
Win7蓝屏代码0X0000007B可能是硬盘模式的问题,我进入BIOS把SATA的mode从Enhanced改为Compatible(及IDE兼容模式)结果系统可以顺利启动没有问题. 从 ...
- 初学者学习Javascript很吃力怎么办?到底该如何学习Js?
Js给初学者的印象总是那么的“杂而乱”,相信很多初学者都在找轻松学习Js的途径.在这里给大家总结一些学习Js的经验,希望能给后来的学习者探索出一条“轻松学习Js之路”. Js给人那种感觉的原因多半 ...
- sql server 学习笔记
1. 修改student表中sdept字段改为varchar类型,长度为30,并且不为空 ) not null 2. 删除student表中的address列 alter table student ...
- FineReport——JS二次开发(下拉框)
下拉框显示多列时,输入的内容检索的内容为显示值整行数据,而不是实际值. 下拉框选择之后,控件显示的是显示值而非实际值. 对于下拉框显示队列,可以有多种方法,但是经过测试大多数方法不适用,检索效率太低, ...
- Network Embedding
网络表示 网络表示学习(DeepWalk,LINE,node2vec,SDNE) https://blog.csdn.net/u013527419/article/details/76017528 网 ...
- Mybatis学习 PageHelper分页插件
1.Maven依赖,注意使用PageHelper时的版本必须与Mybatis版本对应 1 <!-- 添加Mybatis依赖 --> 2 <dependency> 3 <g ...
- 多路复用I/O模型epoll() 模型 代码实现
epoll模型 int epoll_create(int maxevent) //创建一个epoll的句柄 然后maxevent表示监听的数目的大小int epoll_ctl(int epollfd, ...
- Django视图之ORM更改数据库连接——配置MySQL库
Django中创建app01应用 python manage.py startapp app01 1. 首先安装pymsql模块 # Python3中 pip3 install pymysql 在 p ...