题意:给一棵树,问你最多能找到几个组合(u,v),使得两点距离不超过k。

思路:点分治,复杂度O(nlogn*logn)。看了半天还是有点模糊。

显然,所有满足要求的组合,连接这两个点,他们必然经过他们的最小公共子树。

参考:【poj1741】Tree 树的点分治

代码:

#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef long long ll;
const int maxn = + ;
const int seed = ;
const ll MOD = 1e9 + ;
const int INF = 0x3f3f3f3f;
using namespace std;
struct Edge{
int v, w, next;
}edge[maxn << ];
int dis[maxn], sz[maxn], maxv[maxn];
//到root距离,子树大小(包括自己),最大孩子
int tot, num, ans, n, k, Max, root, head[maxn]; //root重心
bool vis[maxn];
void addEdge(int u, int v, int w){
edge[tot].v = v;
edge[tot].w = w;
edge[tot].next = head[u];
head[u] = tot++;
} //子树大小
void dfs_sz(int u, int pre){
sz[u] = ;
maxv[u] = ;
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].v;
if(v == pre || vis[v]) continue;
dfs_sz(v, u);
sz[u] += sz[v];
if(maxv[u] < sz[v])
maxv[u] = sz[v];
}
} //找以u为根的子树的重心
void dfs_root(int r, int u, int pre){
maxv[u] = max(maxv[u], sz[r] - sz[u]);
//sz[r]-sz[u]是u上面部分的树的尺寸,跟u的最大孩子比,找到最大孩子的最小差值节点
if(maxv[u] < Max){
Max = maxv[u];
root = u;
}
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].v;
if(v == pre || vis[v]) continue;
dfs_root(r, v, u);
}
} //离重心距离
void dfs_dis(int u, int d, int pre){
dis[num++] = d;
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].v;
if(v == pre || vis[v]) continue;
dfs_dis(v, d + edge[i].w, u);
}
} //经过u的满足条件的组合的数量
int cal(int u, int d){
int ret = ;
num = ;
dfs_dis(u, d, -);
sort(dis, dis + num);
int i = , j = num - ;
while(i < j){
while(dis[i] + dis[j] > k && i < j)
j--;
ret += j - i;
//i到i+1~j满足
i++;
}
return ret;
} void dfs(int u){
Max = n;
dfs_sz(u, -);
dfs_root(u, u, -);
ans += cal(root, );
vis[root] = true;
for(int i = head[root]; i != -; i = edge[i].next){
int v = edge[i].v;
if(!vis[v]){
ans -= cal(v, edge[i].w);
dfs(v);
}
}
} void init(){
tot = ans = ;
memset(head, -, sizeof(head));
memset(vis, false, sizeof(vis));
} int main(){
while(scanf("%d%d", &n, &k) && n + k){
init();
int u, v, w;
for(int i = ; i < n - ; i++){
scanf("%d%d%d", &u, &v, &w);
addEdge(u, v ,w);
addEdge(v, u, w);
}
dfs();
printf("%d\n", ans);
}
return ;
}

POJ1741 Tree(树分治——点分治)题解的更多相关文章

  1. POJ1741——Tree(树的点分治)

    1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-11-17 1 ...

  2. 【poj1741】Tree 树的点分治

    题目描述 Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dis ...

  3. hdu 4812 D Tree(树的点分治)

    D Tree Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total ...

  4. [poj1741][tree] (树/点分治)

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

  5. POJ1741 Tree 树分治模板

    http://poj.org/problem?id=1741   题意:一棵n个点的树,每条边有距离v,求该树中距离小于等于k的点的对数.   dis[y]表示点y到根x的距离,v代表根到子树根的距离 ...

  6. POJ1741(SummerTrainingDay08-G 树的点分治)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 23380   Accepted: 7748 Description ...

  7. hdu4812-D Tree (树的点分治)

    昨天学了下树分治,今天补这道题,还是太不熟练了,写完之后一直超时.后来查出好多错= =比如v,u写倒了,比如+写成了取最值,比如....爆int...查了两个多小时的错..哭...(没想到进首页了 h ...

  8. 【POJ 1741】 Tree (树的点分治)

    Tree   Description Give a tree with n vertices,each edge has a length(positive integer less than 100 ...

  9. 树上点对统计poj1741(树的点分治)

    给定一棵树,边上有权值,要统计有多少对点路径的权值和<=k 分治算法在树的路径中的应用 这个论文里面有分析. 任意两点的路径,要么过根结点,要么在子树中.如果在子树中,那么只要递归处理就行了. ...

  10. POJ 1741 Tree(树的点分治,入门题)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21357   Accepted: 7006 Description ...

随机推荐

  1. sqlserver 用一个表的值 更新另一个表

    update cas set cas.DocumentHeaderIdOfTransferredForForm = apply.Id from dbo.CaseTransfer cas join db ...

  2. Js闭包学习笔记

    好多内容摘抄了大神的博客内容,只为分享记录.如有冒犯,请见谅 参考文章 http://www.cnblogs.com/libin-1/p/5962269.html http://www.cnblogs ...

  3. linux下安装mysql等信息

    1.安装 apt-get update;// 第一次的时候,你更新一下你的软件包的源地址数据; apt-get install mysql-server 2.账号登陆 mysql -h localho ...

  4. html5 随机数函数

    function selec(low,high){var ch=high-low+1;return Math.floor(Math.random()*ch+low);}for (var i = 0; ...

  5. codefroces 266

    D题说的是 你选定一个区间如[l r] 将这个区间内的每个数都加上1,然后求将这整个整个序列都变成h的方案数有多少种 没有一个位置会有超过1次方[  或者放 ] 考虑当前位置放的是什么 有5种 - 不 ...

  6. SLAM学习笔记

    ORB_SLAM2源码: 获得旋转矩阵,来自这里:http://www.cnblogs.com/shang-slam/p/6406584.html 关于Covisibility图来自:http://b ...

  7. python SQLite说一点点, python使用数据库需要注意的几点

    SQLite是一种嵌入式数据库,它的数据库就是一个文件.由于SQLite本身是C写的,而且体积很小,所以,经常被集成到各种应用程序中,甚至在iOS和Android的App中都可以集成. Python就 ...

  8. ResourceBundle与Properties读取配置文件

    ResourceBundle与Properties的区别在于ResourceBundle通常是用于国际化的属性配置文件读取,Properties则是一般的属性配置文件读取. ResourceBundl ...

  9. TCP协议的三次握手

    TCP协议是面向连接的通信协议,即在传输数据前先在发送端和接收端建立逻辑连接,然后再传输数据,它提供了两台计算机之间可靠无差错的数据传输. l  IP地址:用来唯一表示我们自己的电脑的,是一个网络标示 ...

  10. python之常量和变量

    局部和全局变量: # name='lhf' # def change_name(): # # global name # name='帅了一比' # print('change_name',name) ...