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的更多相关文章

  1. bzoj 1468 Tree(点分治模板)

    1468: Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1527  Solved: 818[Submit][Status][Discuss] ...

  2. 【刷题】BZOJ 1468 Tree

    Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是 ...

  3. bzoj 1468 Tree 点分

    Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1972  Solved: 1101[Submit][Status][Discuss] Desc ...

  4. BZOJ.1468.Tree(点分治)

    BZOJ1468 POJ1741 题意: 计算树上距离<=K的点对数 我们知道树上一条路径要么经过根节点,要么在同一棵子树中. 于是对一个点x我们可以这样统计: 计算出所有点到它的距离dep[] ...

  5. BZOJ 1468 Tree 【模板】树上点分治

    #include<cstdio> #include<algorithm> #define N 50010 #define M 500010 #define rg registe ...

  6. bzoj 2212 Tree Rotations

    bzoj 2212 Tree Rotations 考虑一个子树 \(x\) 的左右儿子分别为 \(ls,rs\) .那么子树 \(x\) 内的逆序对数就是 \(ls\) 内的逆序对数,\(rs\) 内 ...

  7. 【BZOJ】1468: Tree(点分治)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1468 分治真是一门高大上的东西... 好神... 树分治最好资料是:qzc的<分治算法在树的路 ...

  8. [bzoj 1468][poj 1741]Tree [点分治]

    Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...

  9. 【BZOJ 1468】Tree 点分治

    点分治$O(nlogn)$ 坚持到月考结束后新校就剩下我一个OIer,其他人早已停课了,老师估计懒得为我一个人开机房门,让我跟班主任说了一声,今晚就回到了老校,开始了自己都没有想到会来的这么早的停课生 ...

随机推荐

  1. 如何用Nsight调试C# OpenGL程序

    https://devtalk.nvidia.com/default/topic/804306/nsight-visual-studio-edition/nsight-4-5-can-t-debug- ...

  2. VirtualBox5中安装的CentOS6.7安装增强工具

    1.安装编译内核的相关组件 yum install kernel-devel gcc 2.安装VirtualBox增强功能 sh ./VBoxLinuxAdditions.run 3.重启系统 reb ...

  3. ecshop修改注册、增加手机

    1.去掉“用户名”注册 a.去掉提交 user_passport.dwt页面去掉 <input name="username" type="text" s ...

  4. xhprof安装使用

    安装: 到pecl官网下载xhprof的最新版:http://pecl.php.net/package/xhprof wget http://pecl.php.net/get/xhprof-0.9.4 ...

  5. 解决 linux下编译make文件报错“/bin/bash^M: 坏的解释器:没有那个文件或目录” 问题

    PS背景:我在公司做sdk 的pc端开发,所以经常会在win下编译通过之后跑到linux下再运行一次已确保能支持多平台. 今儿在win下跑完一程序,然后放到linux下跑的时候,我用指令: [plai ...

  6. return columns.All(new Func<string, bool>(list.Contains));

    internal static bool VerifyColumns(SqlConnection conn, string table, params string[] columns)        ...

  7. "Java 反序列化"过程远程命令执行漏洞

    一.漏洞描述   国外 FoxGlove 安全研究团队于2015年11月06日在其博客上公开了一篇关于常见 Java 应用如何利用反序列化操作进行远程命令执行的文章.原博文所提到的 Java 应用都使 ...

  8. Spring4学习笔记-AOP

    1.加入jar包 com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEAS ...

  9. Linux下的微秒级定时器: usleep, nanosleep, select, pselect

    Linux下的微秒级定时器: usleep, nanosleep, select, pselect 标签: linuxnulldelaystructdate 2012-02-07 23:29 4979 ...

  10. 再说vim的tab设置

    首先, vim的设置允许简写 // 单击一次tab,停靠, 停止在 4个spaces距离处 set tabstop=4 , 可以简写为: set ts=4 // 允许将tab转换为空格 turn ta ...