Codeforces 161D Distance in Tree
题目大意:给出一棵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的更多相关文章
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
- Codeforces 161D Distance in Tree(树型DP)
题目链接 Distance in Tree $k <= 500$ 这个条件十分重要. 设$f[i][j]$为以$i$为子树,所有后代中相对深度为$j$的结点个数. 状态转移的时候,一个结点的信息 ...
- codeforces 161D Distance in Tree 树上点分治
链接:https://codeforces.com/contest/161/problem/D 题意:给一个树,求距离恰好为$k$的点对是多少 题解:对于一个树,距离为$k$的点对要么经过根节点,要么 ...
- Codeforces 161D Distance in Tree(树的点分治)
题目大概是,给一棵树,统计距离为k的点对数. 不会DP啊..点分治的思路比较直观,啪啪啪敲完然后AC了.具体来说是这样的: 树上任何两点的路径都可以看成是一条过某棵子树根的路径,即任何一条路径都可以由 ...
- CodeForces 161D Distance in Tree【树形DP】
<题目链接> 题目大意:一颗无向无环树,有n个顶点,求其中距离为k的点对数是多少,(u,v)与(v,u)为同一点对. #include <cstdio> #include &l ...
- CF 161D Distance in Tree 树形DP
一棵树,边长都是1,问这棵树有多少点对的距离刚好为k 令tree(i)表示以i为根的子树 dp[i][j][1]:在tree(i)中,经过节点i,长度为j,其中一个端点为i的路径的个数dp[i][j] ...
- CodeForces 160D - Distance in Tree 树型DP
题目给了512MB的空间....用dp[k][i]代表以k为起点...往下面走(走直的不打岔)i步能有多少方案....在更新dp[k][i]过程中同时统计答案.. Program: #include& ...
- CF 161D Distance in Tree【树DP】
题目大意:给一棵树,求树上两点之间距离为K的点对数目. 方程含义: dp(i,j)表示从已经遍历过的点到当前点i,路径长度为 j 的路径条数.因此,对于当前点,每当遍历了其中一个儿子节点的时候,首先统 ...
- Distance in Tree CodeForces - 161D
Distance in Tree CodeForces - 161D 题意:给一棵n个结点的树,任意两点之间的距离为1,现在有点u.v,且u与v的最短距离为k,求这样的点对(u,v)的个数((u,v) ...
随机推荐
- MVC通俗演义系列开篇
最近在读<世界史通俗演义>,发现这本书非常好,比从小上大的历史书好的多.读的开心之余,也不敢跟别人说,怕被说成“通俗”.这跟英文的技术类文章很像.英文中的IT类文章几乎是通俗形象 ...
- python手记(38)
runfile(r'K:\testpro\testopencv.py', wdir=r'K:\testpro') http://blog.csdn.net/myhaspl myhaspl@qq.com ...
- 客户端把rsyslog重启,就会发送全部日志 --待研究
客户端: uat-web02:/var/log/nginx# echo "scan-cccc21231">>scan.log uat-web02:/var/log/ng ...
- 【转】Linux中断处理学习笔记
原文网址:http://www.cnblogs.com/GT_Andy/archive/2011/06/21/2086100.html 1.Linux中断的注册与释放: 在<linux/inte ...
- bzoj3997[TJOI2015]组合数学
http://www.lydsy.com/JudgeOnline/problem.php?id=3997 偏序集,看上一篇随笔. 我们要求最少路径覆盖,可以等价于求最大独立集. 我们要找到一个权值和最 ...
- cf581C Developing Skills
Petya loves computer games. Finally a game that he's been waiting for so long came out! The main cha ...
- 读<<如何阅读一本书>>乱七八糟的笔记1
阅读层次 第一层:基础阅读 第二层:检视阅读 系统化略读 第三层:分析阅读 第四层:主题阅读(比较阅读) 第二层:检视阅读 1.有系统的粗读或略读 (1)先看书名页,然后如果有序就先看序 (2)研究目 ...
- C++中的基类和派生类
转载自:http://www.cnblogs.com/sujz/archive/2011/05/12/2044365.html 派生类的继承方式总结: 继承方式 说明 public 基类的public ...
- MapReduce优化一(改变切片大小和Shuffle过程Reduce占用堆大小)
/*为防止处理超大作业时超时,将io时间设为1小时 * <property> <name>dfs.datanode.soc ...
- React Native 初识
Facebook 在 React.js Conf 2015 大会上推出了基于 JavaScript 的开源框架 React Native,本中文教程翻译自 React Native 官方文档. Rea ...