POJ 1987 Distance Statistics
http://poj.org/problem?id=1987
题意:给一棵树,求树上有多少对节点满足距离<=K
思路:点分治,我们考虑把每个距离都存起来,然后排序,一遍扫描计算一下,注意还要减掉自己加自己的方案。而且,我们还要去掉走到同一个子树的方案。复杂度:O(nlog^2n)
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
int tot,go[],first[],next[];
ll st[],val[];
int sum,son[],root,n,F[],c[];
int pd[],sz,vis[];
ll dis[];
int cnt,K,ans;
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 insert(int x,int y,int z){
tot++;
go[tot]=y;
next[tot]=first[x];
first[x]=tot;
val[tot]=z;
}
void add(int x,int y,int z){
insert(x,y,z);insert(y,x,z);
}
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);
son[x]+=son[pur];
F[x]=std::max(F[x],son[pur]);
}
F[x]=std::max(F[x],sum-son[x]);
if (F[x]<F[root]) root=x;
}
void bfs(int x){
int h=,t=;c[]=x;pd[x]=sz;dis[x]=;
while (h<=t){
int now=c[h++];
for (int i=first[now];i;i=next[i]){
int pur=go[i];
if (vis[pur]||pd[pur]==sz) continue;
pd[pur]=sz;
dis[pur]=dis[now]+val[i];
c[++t]=pur;
st[++cnt]=dis[pur];
}
}
std::sort(st+,st++cnt);
int j=cnt,res=,Cnt=;
for (int i=;i<=t;i++){
while (j>&&st[i]+st[j]>K) j--;
if (st[i]+st[j]<=K) res+=j;
if (st[i]+st[i]<=K) Cnt++;
}
res-=Cnt;
ans+=res/;
}
int del(int x,int Dis){
dis[x]=Dis;sz++;
int h=,t=;cnt=;st[cnt]=Dis;
pd[x]=sz;c[]=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]+val[i];
st[++cnt]=dis[pur];
pd[pur]=sz;
c[++t]=pur;
}
}
int j=cnt,res=,Cnt=;
std::sort(st+,st++cnt);
for (int i=;i<=t;i++){
while (j>&&st[i]+st[j]>K) j--;
if (st[i]+st[j]<=K) res+=j;
if (st[i]+st[i]<=K) Cnt++;
}
res-=Cnt;
return res/;
}
void solve(int x){
vis[x]=;++sz;
cnt=;st[cnt]=;
bfs(x);
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (vis[pur]) continue;
ans-=del(pur,val[i]);
}
int Cnt=sum;
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (vis[pur]) continue;
if (son[pur]>son[x]) sum=Cnt-son[x];
else sum=son[pur];
root=;
findroot(pur,x);
solve(root);
}
}
int main(){
int m;
char s[];
scanf("%d%d\n",&n,&m);
for (int i=;i<n;i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
scanf("%s",s+);
}
scanf("%d\n",&K);
F[]=0x7fffffff;
root=;sum=n;
findroot(,);
solve(root);
printf("%d\n",ans);
}
POJ 1987 Distance Statistics的更多相关文章
- POJ 1987 Distance Statistics 树分治
Distance Statistics Description Frustrated at the number of distance queries required to find a ...
- POJ 1987 Distance Statistics(树的点分治)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 上场CF的C题是一个树的分治... 今天刚好又 ...
- POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)
POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...
- BZOJ_3365_[Usaco2004 Feb]Distance Statistics 路程统计&&POJ_1741_Tree_点分治
BZOJ_3365_[Usaco2004 Feb]Distance Statistics 路程统计&&POJ_1741_Tree_点分治 Description 在得知了自己农 ...
- POJ.1986 Distance Queries ( LCA 倍增 )
POJ.1986 Distance Queries ( LCA 倍增 ) 题意分析 给出一个N个点,M条边的信息(u,v,w),表示树上u-v有一条边,边权为w,接下来有k个询问,每个询问为(a,b) ...
- POJ 1986 Distance Queries LCA两点距离树
标题来源:POJ 1986 Distance Queries 意甲冠军:给你一棵树 q第二次查询 每次你问两个点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + d ...
- POJ 1987 BZOJ 3365 Distance Statistics 树的分治(点分治)
题目大意:(同poj1741,刷一赠一系列) CODE: #include <cstdio> #include <cstring> #include <iostream& ...
- 【poj1987】 Distance Statistics
http://poj.org/problem?id=1987 (题目链接) 题意 给出一棵树,求树上距离不超过K的点对个数. Solution 点分治,同poj1741. 代码 // poj1987 ...
- POJ 1986 Distance Queries(Tarjan离线法求LCA)
Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 12846 Accepted: 4552 ...
随机推荐
- stm32开发笔记一:使用固件库在RealView-MDK中新建工程(上)
很久没有碰单片机了,两年了吧,因为项目需要,最近入手一块红牛的开发板,核心为STM32F103ZE.虽然以前做过大概半年的stm32的开发,现在天天在.net平台下写代码,已经忘记的差不多,恰逢周末, ...
- c++ map与 qt QMap insert 区别
当插入相同key的字段时, c++ map 会保留原来的字段, QMap 则会取代原来的字段.
- EntityFramwork6 在项目中的应用实例
在项目开发中使用的ROM大多采用EntityFramwork去完成,下边给出最新的EntityFramwork在项目中的应用实例 : 一.更新EntityFramwork 在安装.NetFramwor ...
- yii [error] [exception.CHttpException.404] exception 'CHttpException' with message 'Unable to resolve the request "favicon.ico".'
yii使用中,发现runtime文件夹下出现这个错误信息 解决办法:在生成的APP程序根目录下建.htaccess文件(前提是需要开启apache重写,具体如何开,查资料咯) 然后配置如下 <I ...
- codevs 4650 破损的键盘(链表)
/* 之前一直不重视链表 (好吧说实话主要是看着板子都是指针就怂了T.T) 这道题比较基础 应用了链表的思想 数组模拟指针 遇到的问题就是跑着跑着光标跳到前面或者跳到后面 我们用next储存每个点下一 ...
- iis7 发布mvc 遇到的HTTP错误 403.14-Forbidden Web 服务器被配置为不列出此目录的内容
iis 7上发布mvc报错:403.14-Forbidden Web 服务器被配置为不列出此目录的内容 提示里面的解决方法是: 如果不希望启用目录浏览,请确保配置了默认文档并且该文件存在. 使用 II ...
- CSS优先级总结(转载)
样式的优先级 多重样式(Multiple Styles):如果外部样式.内部样式和内联样式同时应用于同一个元素,就是使多重样式的情况. 一般情况下,优先级如下: (外部样式)External styl ...
- 前端CSS兼容的一些思路
半夜睡不着觉,起来写第一博. 近段时间,公司要给一个网站产品增加一个换色功能,安排我负责该事项. 之前参与过一些定制项目,是基于该产品的二次开发,说实话里面的前端结构很混乱.所以第一步就是将html前 ...
- Android实用代码块
通过反射实现Menu显示图标 @Override public boolean onCreateOptionsMenu(Menu menu) { setIconEnable(menu, true); ...
- 趣味PAT--循环-19. 币值转换(20)
One visible minute on the stage is attributed to ten years of invisible practice off the stage. &quo ...