题意:给一棵树,问你最多能找到几个组合(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. sitecore系统教程之架构概述

    Sitecore体验数据库(xDB)从实时大数据存储库中的所有通道源收集所有客户交互.它连接交互数据,为每个客户创建全面,统一的视图,并使营销人员可以使用数据来管理客户的实时体验. xDB架构非常灵活 ...

  2. sitecore系统教程之体验编辑器

    体验编辑器是一个WYSIWYG编辑器,允许您直接在页面上轻松更改项目.您可以编辑页面上可见的所有项目 - 文本,图形,徽标,链接等. 在体验编辑器中,您可以编辑当前项目的字段以及页面上呈现的任何项目的 ...

  3. Lua逻辑操作符

    [1]逻辑操作符and.or和not 应用示例: ) ) -- nil ) -- false ) ) ) ) ) ) ) print(not nil) -- ture print(not false) ...

  4. win7 64位 python启动报错:无法启动此程序,因为计算机中丢失api-ms-win-crt-process-l1-1-0.dll

    安装python3.7,安装成功后,在cmd窗口输入python检查是否安装成功,报错:无法启动此程序,因为计算机中丢失api-ms-win-crt-process-l1-1-0.dll 在网上查询了 ...

  5. python自定义方法处理日志文件

    从命令行界面拷贝的内容包含过个">>>",函数的作用是用正则把每两个">>>"之间的字符取出来,然后把包含“Tracebac ...

  6. php Allocator Jemalloc TCMalloc那个内存分配器比较好?

    php Allocator Jemalloc TCMalloc那个内存分配器比较好? php一键安装脚本可以选择是否安装内存优化 You have 3 options for your Memory ...

  7. Symfony2 学习笔记之系统路由

    mfony2 学习笔记之系统路由   漂亮的URL绝对是一个严肃的web应用程序必须做到的,这种方式使index.php?article_id=57这类的丑陋URL被隐藏,由更受欢迎的像 /read/ ...

  8. 浏览器从输入URL到页面加载显示完成全过程解析

    一 浏览器查找域名对应的 IP 地址(域名解析的过程,先进行缓存的查看): 1.在浏览器中输入www.qq.com域名,操作系统会先检查自己本地的hosts文件是否有这个网址映射关系,如果有,就先调用 ...

  9. right spindle supply short to gnd-- compact version

    hardware guy found that the R1004 lead to this error, but this error should not be checked, because ...

  10. FileReader 获取图片base64数据流 并 生成图片

    <?php if(isset($_GET['upload']) && $_GET['upload'] == 'img'){ if(isset($_GET['stream_type ...