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,其他人早已停课了,老师估计懒得为我一个人开机房门,让我跟班主任说了一声,今晚就回到了老校,开始了自己都没有想到会来的这么早的停课生 ...
随机推荐
- win8启动文件夹
进入C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp.鼠标右键选中粘贴,将软件快捷方式粘贴到启动目录 进入文件夹时路径可能是C: ...
- Spring MVC学习笔记——给Controller和视图传值
一.给Controller传值,值将显示在控制台 1.第一种:使用@RequestParam,改HelloController.java //RequestMapping表示用哪一个url来对应 @R ...
- Java数据结构——二叉树
前序遍历——根 左 右 中序遍历——左 根 右 后序遍历——左 右 根 //================================================= // File Name ...
- Linux下Memcache 安装和使用
Memcached是一种高性能的分布式内存对象缓存系统(memcached虽然称为“分布式”缓存服务器,但服务器端并没有“分布式”功能,其“分布式”由客户端函数库完成,成熟算法的为一致性Hash),用 ...
- Nvidia Nsight + .NET
https://devtalk.nvidia.com/default/topic/804306/nsight-4-5-can-t-debug-net-applications/ http://comm ...
- App Extension Today
App Extensions 是iOS8新开放的扩展机制,之后不断增加功能.App Extension Programming Guide: Today 不喜欢废话,直接上干货! 一:重要概 ...
- Apache日志配置详解(rotatelogs LogFormat)
logs/error_logCustomLog logs/access_log common--默认为以上部分 修改为如下: ErrorLog "|/usr/sbin/rotatelogs ...
- Javascript验证手机号码正则表达式
//验证手机正则 var flag = !!phone.match(/^(0|86|17951)?(13[0-9]|15[012356789]|17[0-9]|18[0-9]|14[0-9])[0-9 ...
- Linux关于添加硬盘的那些事儿:笔记
添加新硬盘:http://note.youdao.com/share/?id=8cf27602cdce36e1d4160f00e9004b00&type=note 关于添加硬盘的那些事儿: ...
- SQL like 模糊查询
SQL 模糊查询 在进行数据库查询时,有完整查询和模糊查询之分. 一般模糊查询语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式: ...