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) ...
随机推荐
- BZOJ3314: [Usaco2013 Nov]Crowded Cows
3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 86 Solved: 61[Subm ...
- 【转】Thunderbird中配置签名
原文网址:https://support.mozilla.org/zh-CN/kb/Thunderbird%E4%B8%AD%E9%85%8D%E7%BD%AE%E7%AD%BE%E5%90%8D “ ...
- 你该学点HTML/CSS知识的9大理由
每个人都应该学写代码——这一观点简直就是铺天盖地地映入我们眼帘.或许你会莫名其妙,程序员学代码那是理所应当,但是作为一个作家.营销人员.财务工作者甚至是工人,为什么也需要学习代码呢? 好吧,下面我会告 ...
- codevs1906 最长递增子序列问题
题目描述 Description 给定正整数序列x1,..... , xn .(1)计算其最长递增子序列的长度s.(2)计算从给定的序列中最多可取出多少个长度为s的递增子序列.(3)如果允许在取出的 ...
- SPOJ694 -- DISUBSTR 后缀树组求不相同的子串的个数
DISUBSTR - Distinct Substrings Given a string, we need to find the total number of its distinct su ...
- [LeetCode] 179. Largest Number 解题思路
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- 【转】C++动态创建二维数组,二维数组指针
原作者博客:蒋国宝的IT技术博客 今天完成一道题目需要自己用指针创建一个二维的数组,不得不承认指针的确是恶心. int **result; ; ; result = new int*[row]; ; ...
- HDU-3661(贪心)
Problem Description In a factory, there are N workers to finish two types of tasks (A and B). Each t ...
- mysql命令行里的加载更多显示
mysql> pager morePAGER set to 'more'mysql> pager lessPAGER set to 'less'mysql> nopagerPAGER ...
- 初探R语言——R语言笔记
R语言使用 <- 赋值 # 作为注释符号 c()函数用于作为向量赋值,例如age<-c(1,2,3,4,5) mean()用于求向量的平均值 sd()求向量的标准差 cor(a,b)求a ...