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) ...
随机推荐
- 【笔试&面试】C#中的程序集
1. C#中的程序集(Assembly) 答:程序集是包含一个或多个类型定义文件和资源文件的集合.它允许我们分离可重用类型的逻辑表示和物理表示. 程序集是一个可重用.可实施版本策略和安全策略 ...
- 百度搜索URL参数 搜索关键字
http://www.baidu.com/s?wd=关键字 wd(Keyword):查询的关键词: http://www.baidu.com/s?wd=关键字&cl=3 cl(Class):搜 ...
- [置顶] API相关工作过往的总结之整体介绍
此系列的总结文章,仅仅是我个人工作总结,有考虑不周之处还请各位同行多多指教. API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是 ...
- java笔记5之逻辑运算符以及&&与&的区别
1 &逻辑与:有false则false. |逻辑或:有true则true. ^逻辑异或:相同为false,不同为true. 举例:情侣关系 ...
- MYSQL常用操作函数的封装
1.mysql常用函数封装文件:mysql.func.php <?php /** * 连接MYSQL函数 * @param string $host * @param string $usern ...
- Filebeat中文指南
Filebeat中文指南 翻译自:https://www.elastic.co/guide/en/beats/filebeat/current/index.html 译者:kerwin 鸣谢:tory ...
- cdh4.1.2 hadoop和oozie集成问题
1.异常信息例如以下: Caused by: com.google.protobuf.ServiceException: java.net.ConnectException: Call From sl ...
- js控制select数据绑定下拉列表
JS代码段: <script type="text/javascript"> $(document).ready(function () { $("sele ...
- Myeclipse 中添加mysql的jdbc驱动
打开myeclipse后单击菜单栏中的myeclipse——>preferences 然后在Myeclipse Enterprise workbench下的Java Enterprise Pro ...
- jQuery的简单应用
时隔多日, 终于我又有时间来浏览些新知识了, 并不是偷懒什么的, 只是真的好忙, 看似闲暇的时间总是冒出一些模糊而又不得不做的事情, 今日终于我又有时间了, 可以看下jQuery了, 并根据自己的了 ...