time limit per test

3 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

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 (vu) and (uv) are considered to be the same pair.

Input

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 ≤ nai ≠ bi), where ai and bi are the vertices connected by the i-th edge. All given edges are different.

Output

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.

Examples
input

Copy
5 2
1 2
2 3
3 4
2 5
output

Copy
4
input

Copy
5 3
1 2
2 3
3 4
4 5
output

Copy
2
Note

In the first sample the pairs of vertexes at distance 2 from each other are (1, 3), (1, 5), (3, 5) and (2, 4).

这道题就是求树上距离为K的点对数量。以前写过<=K的点对数量,直接<=K的数量 - <K的数量,讲道理应该也是可以的,但是一直TLE11和TLE17样例。。。

最后换了一种写法,直接求,没有中间-子树的过程,最后过了,有点迷。。。

不容斥版的快,就这样。

代码:

 //树分治-点分治
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#pragma GCC optimize(2)
//#define FI(n) FastIO::read(n)
const int inf=1e9+;
const int maxn=1e5+;
const int maxm=+; int head[maxn<<],tot;
int root,allnode,n,m,k;
bool vis[maxn];
int deep[maxn],dis[maxn],siz[maxn],maxv[maxn];//deep[0]子节点个数(路径长度),maxv为重心节点
int num[maxm],cnt[maxm];
ll ans=; //namespace FastIO {//读入挂
// const int SIZE = 1 << 16;
// char buf[SIZE], obuf[SIZE], str[60];
// int bi = SIZE, bn = SIZE, opt;
// int read(char *s) {
// while (bn) {
// for (; bi < bn && buf[bi] <= ' '; bi++);
// if (bi < bn) break;
// bn = fread(buf, 1, SIZE, stdin);
// bi = 0;
// }
// int sn = 0;
// while (bn) {
// for (; bi < bn && buf[bi] > ' '; bi++) s[sn++] = buf[bi];
// if (bi < bn) break;
// bn = fread(buf, 1, SIZE, stdin);
// bi = 0;
// }
// s[sn] = 0;
// return sn;
// }
// bool read(int& x) {
// int n = read(str), bf;
//
// if (!n) return 0;
// int i = 0; if (str[i] == '-') bf = -1, i++; else bf = 1;
// for (x = 0; i < n; i++) x = x * 10 + str[i] - '0';
// if (bf < 0) x = -x;
// return 1;
// }
//}; inline int read()
{
int x=;char ch=getchar();
while(ch<''||ch>'')ch=getchar();
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x;
} struct node{
int to,next,val;
}edge[maxn<<]; void add(int u,int v,int w)//前向星存图
{
edge[tot].to=v;
edge[tot].next=head[u];
edge[tot].val=w;
head[u]=tot++;
} void init()//初始化
{
memset(head,-,sizeof head);
memset(vis,,sizeof vis);
tot=;
} void get_root(int u,int father)//重心
{
siz[u]=;maxv[u]=;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(v==father||vis[v]) continue;
get_root(v,u);//递归得到子树大小
siz[u]+=siz[v];
maxv[u]=max(maxv[u],siz[v]);//更新u节点的maxv
}
maxv[u]=max(maxv[u],allnode-siz[u]);//保存节点size
if(maxv[u]<maxv[root]) root=u;//更新当前子树的重心
} void get_dis(int u,int father)//获取子树所有节点与根的距离
{
if(dis[u]>k) return ;
ans+=num[k-dis[u]];
cnt[dis[u]]++;//计数
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(v==father||vis[v]) continue;
int w=edge[i].val;
dis[v]=dis[u]+w;
get_dis(v,u);
}
} void cal(int u,int now)
{
for(int i=;i<=k;i++){//初始化,清空
num[i]=;
}
num[]=;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(vis[v]) continue;
for(int j=;j<=k;j++){//初始化
cnt[j]=;
}
dis[v]=now;
get_dis(v,u);//跑路径
for(int j=;j<=k;j++){
num[j]+=cnt[j];//计数
}
}
} void solve(int u)//分治处理
{
cal(u,);
vis[u]=;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
int w=edge[i].val;
if(vis[v]) continue;
allnode=siz[v];
root=;
get_root(v,u);
solve(root);
}
} int main()
{
// FI(n);FI(k);
n=read();k=read();
init();
for(int i=;i<n;i++){
int u,v,w;w=;
// FI(u);FI(v);
u=read();v=read();
add(u,v,w);
add(v,u,w);
}
root=;allnode=n;maxv[]=inf;
get_root(,);
solve(root);
printf("%lld\n",ans);
return ;
}

Codeforces 161.D. Distance in Tree-树分治(点分治,不容斥版)-树上距离为K的点对数量-蜜汁TLE (VK Cup 2012 Round 1)的更多相关文章

  1. Codeforces 161 D. Distance in Tree (树dp)

    题目链接:http://codeforces.com/problemset/problem/161/D 题意: 给你一棵树,问你有多少对点的距离为k. 思路: dp[i][j]表示离i节点距离为j的点 ...

  2. codeforces 161 D. Distance in Tree(树形dp)

    题目链接:http://codeforces.com/problemset/problem/161/D 题意:给出一个树,问树上点到点的距离为k的一共有几个. 一道简单的树形dp,算是一个基础题. 设 ...

  3. VK Cup 2012 Round 1 D. Distance in Tree (树形dp)

    题目:http://codeforces.com/problemset/problem/161/D 题意:给你一棵树,问你两点之间的距离正好等于k的有多少个 思路:这个题目的内存限制首先大一倍,他有5 ...

  4. 【树形dp】VK Cup 2012 Round 1 D. Distance in Tree

    统计树中长度为K的路径条数. 用f[u][k]表示从u结点的子树中出发,终止于u结点的长度为k的路径条数. 边dp边统计答案.为了防止重复统计,在枚举子节点的时候,先将该子节点和当前u结点(和前面已经 ...

  5. 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的点反向遍 ...

  6. POJ1741--Tree (树的点分治) 求树上距离小于等于k的点对数

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12276   Accepted: 3886 Description ...

  7. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 菜鸡只会ABC!

    Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 全场题解 菜鸡只会A+B+C,呈上题解: A. Bear and ...

  8. 洛谷 P3806 【模板】点分治1-树分治(点分治,容斥版) 模板题-树上距离为k的点对是否存在

    P3806 [模板]点分治1 题目背景 感谢hzwer的点分治互测. 题目描述 给定一棵有n个点的树 询问树上距离为k的点对是否存在. 输入格式 n,m 接下来n-1条边a,b,c描述a到b有一条长度 ...

  9. 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 ...

随机推荐

  1. OpenStack 中 RabbitMQ 的使用

    OpenStack 中 RabbitMQ 的使用 本文是 OpenStack 中的 RabbitMQ 使用研究 两部分中的第一部分,将介绍 RabbitMQ 的基本概念,即 RabbitMQ 是什么. ...

  2. DotnetSpider爬虫简单示例 net core

    文章地址 https://blog.csdn.net/sD7O95O/article/details/78097556 安装爬虫框架  NUGET 安装DotnetSpider 创建HTTP协议数据包 ...

  3. 上传文件大小与时间 Web.Config文件 httpRuntime 限制

    httpRuntime  <httpRuntime executionTimeout="90" maxRequestLength="40960" useF ...

  4. Spring4参考手册中文版

    Spring4参考手册中文版 前言 https://github.com/b2gats/stone-docs/blob/master/spring-4-beans.md Part III. 核心技术 ...

  5. Django ForeignKey不需要参照完整性?

    我想在django模型中设置一个ForeignKey字段,它在某些时候指向另一个表.但我希望可以在这个字段中插入一个id,它引用另一个表中可能不存在的条目.因此,如果该行存在于另一个表中,我希望获得F ...

  6. ios app UI自动化测试用到的命令

    ios测试的app测试包,真机设备需要开发者证书并且将测试机的udid加入到pp文件文件,configruation 要求为debug模式的ipa包, 1.苹果手机的UDID, a.通过 xcode- ...

  7. 使用Nexus3搭建Maven私服+上传第三方jar包到本地maven仓库

    1.搭建Maven私服背景 公司还是按捺不住,要搭建一个自己的Maven本地仓库,可以让开发人员down架包,从内网还是快很多. 这样公司的maven本地仓库就是 开发人员自己电脑上的maven仓库 ...

  8. English--动词语态

    English|动词语态 动词的语态在理解长难句中很重要.了解被动语态与主动语态,掌握语态的变化方式. 前言 目前所有的文章思想格式都是:知识+情感. 知识:对于所有的知识点的描述.力求不含任何的自我 ...

  9. 英语fraunce法兰西

    fraunce  外文词汇,中文翻译为代指法兰西(地名) 中文名:法兰西 外文名:fraunce 目录 释义 Fraunce 读音:英 [frɑ:ns] 美 [fræns] Noun(名词) 1. ...

  10. android中实现service动态更新UI界面

    案例:通过service向远程服务器发送请求,根据服务器返回的结果动态更新主程序UI界面,主程序可实时关闭或重启服务. 注册BroadcastReceiver 在主程序activity中注册一个Bro ...