题意:

  带权树,求距离小于k的点对数目。

SOL:

  参考http://blog.csdn.net/jiangshibiao/article/details/25738041解决了题意问题。。。

  代码是看着iwtwiioi巨巨打的。。。因为查错然后改得几乎一模一样。。。

  当然这种分治问题自然还是要安利qzc巨巨的论文= =名字忘了。。。

  其实还是很明朗的方法,就是觉得树上容斥有点6.

  恩好像就没什么了。

Code:

  

/*==========================================================================
# Last modified: 2016-03-11 11:50
# Filename: t2.cpp
# Description:
==========================================================================*/
#define me AcrossTheSky
#include <cstdio>
#include <cmath>
#include <ctime>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> #include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector> #define lowbit(x) (x)&(-x)
#define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++)
#define FORP(i,a,b) for(int i=(a);i<=(b);i++)
#define FORM(i,a,b) for(int i=(a);i>=(b);i--)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
#define ls(a,b) (((a)+(b)) << 1)
#define rs(a,b) (((a)+(b)) >> 1)
#define getlc(a) ch[(a)][0]
#define getrc(a) ch[(a)][1] #define maxn 100000
#define maxm 100000
#define pi 3.1415926535898
#define _e 2.718281828459
#define INF 1070000000
using namespace std;
typedef long long ll;
typedef unsigned long long ull; template<class T> inline
void read(T& num) {
bool start=false,neg=false;
char c;
num=0;
while((c=getchar())!=EOF) {
if(c=='-') start=neg=true;
else if(c>='0' && c<='9') {
start=true;
num=num*10+c-'0';
} else if(start) break;
}
if(neg) num=-num;
}
/*==================split line==================*/ const int N=40005;
int ihead[N], cnt, K;
struct Edge{
int next, to, w;
}e[N<<1];
void add(int u, int v, int w) {
e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt].w=w;
e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].to=u; e[cnt].w=w;
} int dep[N], d[N], cdep, ans, mn;
int root, sz[N], vis[N];
void getroot(int x, int fa, int sum) {
sz[x]=1; int y, mx=0;
rdm(x, i) if(!vis[y=e[i].to] && e[i].to!=fa) {
getroot(y, x, sum);
sz[x]+=sz[y];
mx=max(mx, sz[y]);
}
mx=max(mx, sum-mx);
if(mx<mn) mn=mx, root=x;
}
void getdep(int x, int fa) {
dep[++cdep]=d[x]; int y; //printf("x:%d\tfa:%d\tdep:%d\n", x, fa, dep[x]);
rdm(x, i) if(!vis[y=e[i].to] && e[i].to!=fa) {
d[y]=d[x]+e[i].w;
getdep(y, x);
}
}
int cal(int x, int last=0) {
cdep=0; d[x]=last;
getdep(x, -1);
int ret=0, front=1, tail=cdep;
sort(dep+1, dep+1+cdep);
while(front<tail) {
while(front<tail && dep[tail]+dep[front]>K) --tail;
ret+=tail-front;
++front;
}
return ret;
}
void dfs(int x, int all) {
vis[x]=1; int y;
ans+=cal(x); //printf("root:%d\n", x);
rdm(x, i) if(!vis[y=e[i].to]) {
ans-=cal(y, e[i].w);
int s=sz[y]>sz[x]?all-sz[x]:sz[y];
root=0; mn=INF; getroot(y, x, s);
dfs(root, s);
}
} int main() {
int n;
read(n);
FORP(i,1,n-1) {
int u,v,w;
read(u); read(v); read(w);
add(u,v,w);
}
read(K); mn=INF;
getroot((n+1)>>1, -1, n);
dfs(root, n);
printf("%d\n", ans);
return 0;
}

BZOJ 1468 & 点分治的更多相关文章

  1. BZOJ 1468 树分治

    求出子树的重心后求出它每个子节点的距离,排序后就可以统计距离小于等于K的点对的个数了,但是会在同一子树内重复,然后在每个子树里面减去小于等于K的点对个数就可以了. #include <iostr ...

  2. 【BZOJ 1468】Tree 点分治

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

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

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

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

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

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

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

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

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

  7. BZOJ 1468: Tree

    Description 真·树,问距离不大于 \(k\) 的点对个数. Sol 点分治. 同上. Code /********************************************* ...

  8. bzoj 1468

    大概思路:树点分治,重心树中每个重心维护一个总的平衡树,树中保存属于该重心的点到该重心的距离,然后对于去掉该重心后形成的子树分别再保存一份. 用这种方式实现的话,还可以支持修改与多次查询,每次操作都是 ...

  9. bzoj 4025 二分图 分治+并查集/LCT

    bzoj 4025 二分图 [题目大意] 有n个点m条边,边会在start时刻出现在end时刻消失,求对于每一段时间,该图是不是一个二分图. 判断二分图的一个简单的方法:是否存在奇环 若存在奇环,就不 ...

随机推荐

  1. 标准BT.656并行数据结构

    转自网络,感谢原作者和转载者. 还有参考:百科http://baike.baidu.com/link?url=bqBT3S7pz_mRJoQE7zkE0K-R1RgQ6FmHNOZ0EjhlSAN_o ...

  2. vsftp详细配置(转)

    详细配置转载来自以下链接: http://yuanbin.blog.51cto.com/363003/108262 vsftp源码下载(vsftpd-3.0.2.tar.gz): http://dow ...

  3. Android -- android:configChanges

    原文:http://jingyan.baidu.com/article/2fb0ba4056b25700f2ec5faf.html 从Android 3.2(API 13),如果你想阻止程序在运行时重 ...

  4. jQuery - 2.jQuery选择器

    1.id 选择器 2.标签选择器 3.类选择器 4.复合选择器 5.层次选择器 JQuery的迭代   JQuery选择器 JQuery选择器用于查找满足条件的元素,比如可以用$("#控件I ...

  5. c#将http调用返回额json中的有关中文的unicode转换为中文(转)

    转转地址:http://www.cnblogs.com/promise-7/archive/2012/11/05/2755515.html 中文转Unicode:HttpUtility.UrlEnco ...

  6. java创建线程的几种方式

    1.继承Thread类 /** * @author Ash * @date: 2016年8月6日 下午10:56:45 * @func: 通过继承Thread类来实现多线程 * @email 4086 ...

  7. tornado使用(Mac)

    安装需求 Tornado 在 Python 2.5, 2.6, 2.7 中都经过了测试.要使用 Tornado 的所有功能,你需要安装 PycURL (7.18.2 或更高版本) 以及 simplej ...

  8. 記錄一次CRS-0184: Cannot communicate with the CRS daemon的解決

    1. 描述: 使用crs_stat –t 命令查看rac服務,直接報CRS-0184: Cannot communicate with the CRS daemon.錯誤 但是奇怪的是我們的DB是沒有 ...

  9. QQ的账号登录及api操作

    .qq.php <?php /** * PHP Library for qq.com * * @author */ class qqPHP { function __construct($app ...

  10. 基于MATLAB的adaboost级联形式的人脸检测实现

    很早之前就做过一些关于人脸检测和目标检测的课题,一直都没有好好总结出来,趁着这个机会,写个总结,希望所写的内容能给研究同类问题的博友一些见解和启发!!博客里面涉及的公式太繁琐了,直接截图了. 转载请注 ...