BZOJ 1468: Tree
Description
真·树,问距离不大于 \(k\) 的点对个数.
Sol
点分治.
同上.
Code
/**************************************************************
Problem: 1468
User: BeiYu
Language: C++
Result: Accepted
Time:832 ms
Memory:3804 kb
****************************************************************/ #include<cstdio>
#include<utility>
#include<vector>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std; typedef pair< int,int > pr;
#define mpr make_pair
#define debug(a) cout<<#a<<"="<<a<<" "
const int N = 40005; int n,m,k,rt,sz,ans;
vector<pr> g[N];
int s[N],f[N],b[N];
vector<int> d;
int q[N],h,t; inline int in(int x=0,char ch=getchar()){ while(ch>'9' || ch<'0') ch=getchar();
while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x; }
void GetRoot(int u,int fa){
s[u]=1,f[u]=0;
for(int i=0,v;i<g[u].size();i++) if((v=g[u][i].first)!=fa && !b[v]){
GetRoot(v,u),s[u]+=s[v],f[u]=max(f[u],s[v]);
}f[u]=max(f[u],sz-s[u]);if(f[u]<f[rt]) rt=u;
}
void GetDeep(int u,int fa,int dep){
d.push_back(dep),s[u]=1;
for(int i=0,v;i<g[u].size();i++) if((v=g[u][i].first)!=fa && !b[v])
GetDeep(v,u,dep+g[u][i].second),s[u]+=s[v];
}
int Calc(int u,int w){
d.clear(),GetDeep(u,u,w);
sort(d.begin(),d.end());
int res=0;
for(int l=0,r=d.size()-1;l<r;) if(d[l]+d[r]<=k) res+=r-l,l++;else r--;
return res;
}
void GetAns(int u){
ans+=Calc(u,0);b[u]=1;
for(int i=0,v;i<g[u].size();i++) if(!b[v=g[u][i].first])
ans-=Calc(v,g[u][i].second),f[0]=sz=s[v],GetRoot(v,rt=0),GetAns(rt);
}
int main(){
// freopen("in.in","r",stdin);
n=in();
for(int i=1,u,v,w;i<n;i++) u=in(),v=in(),w=in(),g[u].push_back(mpr(v,w)),g[v].push_back(mpr(u,w));
k=in();
f[rt=0]=sz=n;
GetRoot(1,0);
GetAns(rt);
cout<<ans<<endl;
return 0;
}
BZOJ 1468: Tree的更多相关文章
- bzoj 1468 Tree(点分治模板)
1468: Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1527 Solved: 818[Submit][Status][Discuss] ...
- 【刷题】BZOJ 1468 Tree
Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是 ...
- bzoj 1468 Tree 点分
Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1972 Solved: 1101[Submit][Status][Discuss] Desc ...
- BZOJ.1468.Tree(点分治)
BZOJ1468 POJ1741 题意: 计算树上距离<=K的点对数 我们知道树上一条路径要么经过根节点,要么在同一棵子树中. 于是对一个点x我们可以这样统计: 计算出所有点到它的距离dep[] ...
- BZOJ 1468 Tree 【模板】树上点分治
#include<cstdio> #include<algorithm> #define N 50010 #define M 500010 #define rg registe ...
- bzoj 2212 Tree Rotations
bzoj 2212 Tree Rotations 考虑一个子树 \(x\) 的左右儿子分别为 \(ls,rs\) .那么子树 \(x\) 内的逆序对数就是 \(ls\) 内的逆序对数,\(rs\) 内 ...
- 【BZOJ】1468: Tree(点分治)
http://www.lydsy.com/JudgeOnline/problem.php?id=1468 分治真是一门高大上的东西... 好神... 树分治最好资料是:qzc的<分治算法在树的路 ...
- [bzoj 1468][poj 1741]Tree [点分治]
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
- 【BZOJ 1468】Tree 点分治
点分治$O(nlogn)$ 坚持到月考结束后新校就剩下我一个OIer,其他人早已停课了,老师估计懒得为我一个人开机房门,让我跟班主任说了一声,今晚就回到了老校,开始了自己都没有想到会来的这么早的停课生 ...
随机推荐
- sphinx :undefined reference to `libiconv' 报错解决办法
sphinx :undefined reference to `libiconv' 报错解决办法 2013-11-30 21:45:39 安装sphinx时不停报错...郁闷在make时报错,错误 ...
- 10月20日MySQL数据库作业解析
设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表 ...
- Linux学习笔记<四>
<1>shutdown -h now 立刻进行关机 shutdown -r now/reboor 现在重新启动计算机 <2>尽量避免用root用户登陆,用普通用户登陆后换成ro ...
- C#----使用WindowsMediaPlayer 同时播放多个声音
使用Windows Media Player 其实就是使用组件AxWindowsMediaPlayer. 添加两个引用:Interop.WMPLib.dll和AxInterop.WMPLib.dll. ...
- paramiko模拟ansible远程执行命令
主模块 #!/usr/bin/env python from multiprocessing import Process import paramiko import time import sys ...
- 使用commons-fileupload包进行大文件上传注意事项
项目中使用 commons-fileupload-1.2.1.jar 进行大文件上传. 测试了一把,效果很不错. 总结如下: 必须设置好上传文件的最大阀值 final long MAX_SIZE = ...
- yii2 Pjax的使用
有两个例子:刷新时间和数据显示排序 1.刷新时间 (1)控制器中的方法:Time public function actionTime() { return $this->render('tim ...
- Yii2创建多界面主题(Theme)
Yii2界面主题上的设计总体上和Yii1.x保持一致,区别在于两个地方: 1. 由于Yii2引入了独立的视图(View)类,因此界面主题(Theme)也交由视图来管理: 2. 视图文件和Web资源在目 ...
- memcached的最佳实践方案(转)
基本问题 1.memcached的基本设置 1)启动Memcache的服务器端 # /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.200 ...
- “mybatis 中使用foreach 传
为了帮助网友解决“mybatis 中使用foreach 传”相关的问题,中国学网通过互联网对“mybatis 中使用foreach 传”相关的解决方案进行了整理,用户详细问题包括:mybatismap ...