题意:给一棵树,问你最多能找到几个组合(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. 54. Spiral Matrix(剑指offer 19)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  2. Day7 错误和异常

    一.异常 1.异常基础 1.为了让我们的代码在出现异常的时候,整个项目依然是可以正常运行的,所以我们引入了异常处理机制! 2.在编程过程中为了增加友好性,在程序出现bug时一般不会将错误信息显示给用户 ...

  3. golang linux安装

    go在linux下的安装: [root@localhost src]# wget https://storage.googleapis.com/golang/go1.8.1.linux-amd64.t ...

  4. xlrd、xlwt

    一个公司内,销售或者人事都是使用excel来记录员工的信息,所以介绍可操作excel文件的xlrd.xlwt模块. 其中xlrd模块实现对excel文件内容读取,xlwt模块实现对excel文件的写入 ...

  5. 2017-2018-2 20165215 实验二 Java面向对象程序设计

    20165215 实验二 Java面向对象程序设计 一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:张家佳 学号:20165215 指导教师:娄嘉鹏 实验日期:2018年4月16日 ...

  6. Codeforce 791A - Bear and Big Brother

    Bear Limak wants to become the largest of bears, or at least to become larger than his brother Bob. ...

  7. Robot Framework 自动化测试--部署篇

    一.产品介绍 Robot Framework是一个基于Python的,可扩展的关键字驱动的测试自动化框架.它是为了端 到端的验收测试(End-To-End Acceptance Test)以及验收测试 ...

  8. [转载]C# 常用日期时间函数(老用不熟)

    原博地址:http://www.jb51.net/article/20181.htm --DateTime 数字型 System.DateTime currentTime=new System.Dat ...

  9. 一起学习在 Ubuntu 上授予和移除 sudo 权限

    如你所知,用户可以在 Ubuntu 系统上使用 sudo 权限执行任何管理任务.在 Linux 机器上创建新用户时,他们无法执行任何管理任务,直到你将其加入 sudo 组的成员.在这个简短的教程中,我 ...

  10. JS 和 Jquery 的一些常用效果

    https://www.cnblogs.com/beiz/tag/%E7%BD%91%E9%A1%B5%E5%B8%B8%E8%A7%81%E6%95%88%E6%9E%9C/   北执