题目大意:给出一棵n个节点的树,统计树中长度为k的路径的条数(1<=n<=50000 , 1<=k<=500)

思路:树分治!

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#define ll long long
int n,K,son[],F[],sum;
ll ans;
int c[],pd[],vis[],A[];
int sz,dis[],root,mxdeep,deep[];
int tot,go[],first[],next[];
void insert(int x,int y){
tot++;
go[tot]=y;
next[tot]=first[x];
first[x]=tot;
}
void add(int x,int y){
insert(x,y);insert(y,x);
}
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void findroot(int x,int fa){
son[x]=;F[x]=;
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (pur==fa||vis[pur]) continue;
findroot(pur,x);
F[x]=std::max(F[x],son[pur]);
son[x]+=son[pur];
}
F[x]=std::max(F[x],sum-son[x]);
if (F[x]<F[root]) root=x;
}
void Dfs(int x,int fa){
mxdeep=std::max(mxdeep,deep[x]);
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (fa==pur||vis[pur]) continue;
deep[pur]=deep[x]+;
Dfs(pur,x);
}
}
void bfs(int x){
pd[x]=sz;
int h=,t=;c[]=x;dis[x]=;
while (h<=t){
int now=c[h++];
for (int i=first[now];i;i=next[i]){
int pur=go[i];
if (pd[pur]==sz||vis[pur]) continue;
dis[pur]=dis[now]+;
c[++t]=pur;
pd[pur]=sz;
}
}
for (int i=;i<=t;i++)
if (K>=dis[c[i]])
ans+=A[K-dis[c[i]]];
for (int i=;i<=t;i++)
A[dis[c[i]]]++;
}
void solve(int x,int fa){
vis[x]=;
mxdeep=;
deep[x]=;
Dfs(x,);
for (int i=;i<=mxdeep;i++)
A[i]=;
A[]=;
sz++;
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (pur==fa||vis[pur]) continue;
bfs(pur);
}
int cnt=sum;
for (int i=;i<=mxdeep;i++)
A[i]=;
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (pur==fa||vis[pur]) continue;
root=;
if (son[pur]>son[x]) sum=cnt-son[x];
else sum=son[pur];
findroot(pur,x);
solve(root,x);
}
}
int main(){
n=read();K=read();
for (int i=;i<n;i++){
int x=read(),y=read();
add(x,y);
}
F[]=0x7fffffff;root=;sum=n;
findroot(,);
solve(root,);
printf("%I64d\n",ans);
}

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

  1. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

  2. Codeforces 161D Distance in Tree(树型DP)

    题目链接 Distance in Tree $k <= 500$ 这个条件十分重要. 设$f[i][j]$为以$i$为子树,所有后代中相对深度为$j$的结点个数. 状态转移的时候,一个结点的信息 ...

  3. codeforces 161D Distance in Tree 树上点分治

    链接:https://codeforces.com/contest/161/problem/D 题意:给一个树,求距离恰好为$k$的点对是多少 题解:对于一个树,距离为$k$的点对要么经过根节点,要么 ...

  4. Codeforces 161D Distance in Tree(树的点分治)

    题目大概是,给一棵树,统计距离为k的点对数. 不会DP啊..点分治的思路比较直观,啪啪啪敲完然后AC了.具体来说是这样的: 树上任何两点的路径都可以看成是一条过某棵子树根的路径,即任何一条路径都可以由 ...

  5. CodeForces 161D Distance in Tree【树形DP】

    <题目链接> 题目大意:一颗无向无环树,有n个顶点,求其中距离为k的点对数是多少,(u,v)与(v,u)为同一点对. #include <cstdio> #include &l ...

  6. CF 161D Distance in Tree 树形DP

    一棵树,边长都是1,问这棵树有多少点对的距离刚好为k 令tree(i)表示以i为根的子树 dp[i][j][1]:在tree(i)中,经过节点i,长度为j,其中一个端点为i的路径的个数dp[i][j] ...

  7. CodeForces 160D - Distance in Tree 树型DP

    题目给了512MB的空间....用dp[k][i]代表以k为起点...往下面走(走直的不打岔)i步能有多少方案....在更新dp[k][i]过程中同时统计答案.. Program: #include& ...

  8. CF 161D Distance in Tree【树DP】

    题目大意:给一棵树,求树上两点之间距离为K的点对数目. 方程含义: dp(i,j)表示从已经遍历过的点到当前点i,路径长度为 j 的路径条数.因此,对于当前点,每当遍历了其中一个儿子节点的时候,首先统 ...

  9. Distance in Tree CodeForces - 161D

    Distance in Tree CodeForces - 161D 题意:给一棵n个结点的树,任意两点之间的距离为1,现在有点u.v,且u与v的最短距离为k,求这样的点对(u,v)的个数((u,v) ...

随机推荐

  1. expect: spawn id exp4 not open

    spawn rsync -avH --delete /home/dwetl/bin dwetl@10.128.8.151:/home/dwetl/bin sending incremental fil ...

  2. Linux 安装字体

    把XP下的字体C:\WINDOWS\FONTS\simsun.ttc(也就是宋体,大小为10M),把他重命名为 simsun.ttf 拷贝simsun.ttf 字体到 /usr/share/fonts ...

  3. google开源的C++性能分析工具 - gperftools

    gperftools是Google提供的一套工具,其中的一个功能是CPU profiler,用于分析程序性能,找到程序的性能瓶颈. 安装 gperftools:http://code.google.c ...

  4. 【转】Android adb shell操作时出现“ XXX ... Read-only file system”解决办法--不错

    原文网址:http://blog.csdn.net/whu_zhangmin/article/details/25364349 手机连接PC后 adb shell su rm -r /system/a ...

  5. 黑马程序员_Java集合Map

    Map Map概述: 接口Map<k,v> 类型参数: k-此映射所维护的键的类型 v-映射值的类型 Map集合:该集合存储键值对.一对一对往理存.而且要保证键的唯一性. 嵌套类摘要: s ...

  6. Windows服务器Pyton辅助运维--02.远程重启IIS服务器

    Windows服务器Pyton辅助运维 02.远程重启IIS服务器 开发环境: u  Web服务器: Windows Server 2008 R2 SP1 IIS 7.5 u  运维服务器: Pyth ...

  7. 51nod-正整数分组问题(基础方程DP-01背包)

    正整数分组 将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1,是所有方案中相差最少的. 思路: 这题的实质其实也是0-1背包问 ...

  8. red-hat6.5 yum 源配置,cloud-init 安装 This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register

    This system is not registered to Red Hat Subscription Management. You can use subscription-manager t ...

  9. squid 代理服务器安装配置

    ubuntu16.04 安装squid代理服务器配置 本文参考 http://www.cnblogs.com/newflypig/archive/2012/09/28/2862000.html 1,删 ...

  10. 关于phpmyadmin中添加外键的做法

    今天想加个外键,又不想用命令行,打开PHPMYADMIN看怎么弄,找了半天没有找到添加外键的地方,然后上网搜了一会,发现目前的PHPMYADMIN确实没有这个设置,所以只能手动命令行添加了.   语法 ...