Codeforces 161D Distance in Tree(树的点分治)
题目大概是,给一棵树,统计距离为k的点对数。
不会DP啊。。点分治的思路比较直观,啪啪啪敲完然后AC了。具体来说是这样的:
- 树上任何两点的路径都可以看成是一条过某棵子树根的路径,即任何一条路径都可以由一个子树到达根的一条或两条路径组成
- 就可以分治累加各个结点为根的子树的统计数目
- 对于各个子树可以这样统计:假设这个子树的根有a、b、c...若干个孩子,开一个数组cnt[i]记录有几个结点到根结点为i,依次处理a、b、c...结点及各自以下的结点,处理的时候根据当前的cnt数组统计数目,处理完后把新的数目更新到cnt数组。这样直接就能不重复地统计数目了
- 时间复杂度O(nlogn)
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF (1<<30)
#define MAXN 55555
struct Edge{
int v,next;
}edge[MAXN<<];
int NE,head[MAXN];
void addEdge(int u,int v){
edge[NE].v=v; edge[NE].next=head[u];
head[u]=NE++;
}
bool vis[MAXN];
int size[MAXN];
void getSize(int u,int fa){
size[u]=;
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].v;
if(v==fa || vis[v]) continue;
getSize(v,u);
size[u]+=size[v];
}
}
int cen,mini;
void getCentre(int u,int fa,int &tot){
int res=tot-size[u];
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].v;
if(v==fa || vis[v]) continue;
getCentre(v,u,tot);
res=max(res,size[v]);
}
if(res<mini){
mini=res;
cen=u;
}
}
int getCentre(int u){
mini=INF;
getSize(u,u);
getCentre(u,u,size[u]);
return cen;
}
int n,k,cnt[],tmp[],ans;
void dfs(int u,int fa,int dist){
if(dist>k) return;
ans+=cnt[k-dist];
++tmp[dist];
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].v;
if(v==fa || vis[v]) continue;
dfs(v,u,dist+);
}
}
void conquer(int u){
memset(cnt,,sizeof(cnt));
cnt[]=;
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].v;
if(vis[v]) continue;
memset(tmp,,sizeof(tmp));
dfs(v,v,);
for(int i=; i<=k; ++i) cnt[i]+=tmp[i];
}
}
void divide(int u){
u=getCentre(u);
vis[u]=;
conquer(u);
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].v;
if(vis[v]) continue;
divide(v);
}
}
int main(){
memset(head,-,sizeof(head));
int a,b;
scanf("%d%d",&n,&k);
for(int i=; i<n; ++i){
scanf("%d%d",&a,&b);
addEdge(a,b);
addEdge(b,a);
}
divide();
printf("%d",ans);
return ;
}
Codeforces 161D Distance in Tree(树的点分治)的更多相关文章
- Codeforces 161D Distance in Tree(树型DP)
题目链接 Distance in Tree $k <= 500$ 这个条件十分重要. 设$f[i][j]$为以$i$为子树,所有后代中相对深度为$j$的结点个数. 状态转移的时候,一个结点的信息 ...
- 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 树上点分治
链接:https://codeforces.com/contest/161/problem/D 题意:给一个树,求距离恰好为$k$的点对是多少 题解:对于一个树,距离为$k$的点对要么经过根节点,要么 ...
- Codeforces 161D Distance in Tree
题目大意:给出一棵n个节点的树,统计树中长度为k的路径的条数(1<=n<=50000 , 1<=k<=500) 思路:树分治! #include<cstdio> # ...
- CodeForces 161D Distance in Tree【树形DP】
<题目链接> 题目大意:一颗无向无环树,有n个顶点,求其中距离为k的点对数是多少,(u,v)与(v,u)为同一点对. #include <cstdio> #include &l ...
- CodeForces 160D - Distance in Tree 树型DP
题目给了512MB的空间....用dp[k][i]代表以k为起点...往下面走(走直的不打岔)i步能有多少方案....在更新dp[k][i]过程中同时统计答案.. Program: #include& ...
- CodeChef - PRIMEDST Prime Distance On Tree 树分治 + FFT
Prime Distance On Tree Problem description. You are given a tree. If we select 2 distinct nodes unif ...
- 【poj1741】Tree 树的点分治
题目描述 Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dis ...
- hdu 4812 D Tree(树的点分治)
D Tree Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others) Total ...
随机推荐
- RootKit学习之 IDT Hook
0x00 前言 IDT(Interrupt Descriptor Table)中断描述符表,中断就是停下现在的活动,去完成新的任务.一个中断可以起源于软件或硬件.比如,出现页错误,调用IDT中的0x ...
- String的内存分配
1.String类是final类不能被继承 2.String str="abc"的内部工作 (1)先在栈中定 一个名为str的String类的引用变量 String str: (2 ...
- MySQL数据库InnoDB引擎下服务器断电数据恢复
说明: 线上的一台MySQL数据库服务器突然断电,造成系统故障无法启动,重新安装系统后,找到之前的MySQL数据库文件夹. 问题: 通过复制文件的方式对之前的MySQL数据库进行恢复,发现在程序调用时 ...
- GIT的标准文档 使用和服务介绍
http://www.kancloud.cn/kancloud/how-to-use-github/42192 1. 探索GitHub 熟悉Git的人几乎都知道并喜欢GitHub,反过来GitHub也 ...
- CheckBoxList1复选框
循环绑定数据的两个方法: List<string> LIColl = new List<string>(); protected void Page_Load(object s ...
- nested exception is org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 56; cvc-complex-type.2.4.c通配符的匹配很全面, 但无法找到元素 'dubbo:application' 的声明
严重: Exception sending context initialized event to listener instance of class org.springframework.we ...
- 搭建CAS单点登录服务器
最近公司的一个项目需要用到单点登录的功能,之前对单点登录了解得不多.于是网上找了下单点登录的解决方案,发现CAS是个不错的解决方案.于是搭个环境测试了一下.这里记录下测试的详细步骤. 官网:http: ...
- Android Studio "diamond operator is not supported" 处理方法
低版本的android编译环境是不支持使用java7语法的,如果使用了,就会产生上述问题,如果你的android环境较新,那么可以使用以下方法: 在build.gradle的android标签下加入以 ...
- mysql 表空间
开启了Innodb的innodb_file_per_table这个参数之后[innodb_file_per_table = 1],也就是启用InnoDB的独立表空间模式,便于管理.此时,在新建的inn ...
- js将map转换成数组
/** * map转数组. * * @param {Map}map * map对象 * @return 数组 */ Share.map2Ary = function(map) { var list = ...