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. echosp 销量排行 新增实际价格

    找到lib_goods.php第147行,代码 $sql = 'SELECT g.goods_id, g.goods_name, g.shop_price,g.goods_thumb, SUM(og. ...

  2. 集合 ArrayList

    /* * 功能:演示java集合的用法:ArrayList */ package com.jihe; //先引入一个包 import java.util.ArrayList; public class ...

  3. C语言strdup函数

    static RD_INLINE RD_UNUSED char *rd_strdup(const char *s) { #ifndef _MSC_VER char *n = strdup(s); #e ...

  4. [百度地图] MultiZMap 修改使用;

    MultiZMap修改说明 MultiZMap 是基于百度地图API 封装的一些常用功能类库:主要以prototype方式实现:并且实现了一些辅助的功能,比如添加 Marker 功能,事件管理等: 以 ...

  5. Junit初级编码(一)第一个Junit测试程序

    序,Junit测试是单元测试的一个框架,提供了很多方法,供我们快速开展单元测试.目前最新版本JAR包为4.12,官网地址为http://junit.org/ 一.第一个Junit测试程序 1 去官网下 ...

  6. Html书写规范

    #cnblogs_post_body ol { padding-left: 0px; } body { line-height: 1.6; } body, th, td, button, input, ...

  7. Windows Phone 开发环境的搭建

    1. 系统 系统:Windows 7(32 位).Windows 7(64 位).Windows Vista SP2(32 位)和 Windows Vista(64 位)或者更高版本. 不支持 :Wi ...

  8. windows下PHP+Mysql+Apache环境搭建

    Apache版本:httpd-2.2.22-win32-x86-openssl-                   下载地址:http://pan.baidu.com/s/1sjuL4RV PHP版 ...

  9. 论在Windows下远程连接Ubuntu

       Ubuntu下1:下载xrdp   sudo apt-get install xrdp 2: urs/share/applications 下找到  远程桌面 设置成这样 Windows下 1; ...

  10. 北美IT公司大致分档

    北美IT公司大致分档(from mitbbs.com) 第一档: Uber, Snapchat, Airbnb, Dropbox, Pinterest 第二档:Facebook, LinkedIn, ...