题目大意:

树上找到有多少条路径的边权值和>=k

这里在树上进行点分治,需要找到重心保证自己的不会出现过于长的链来降低复杂度

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <algorithm> using namespace std;
#define N 10005
int n , m , k , first[N]; struct Edge{
int y , next , d;
Edge(){}
Edge(int y , int next , int d):y(y),next(next),d(d){}
}e[N<<]; void add_edge(int x , int y , int d)
{
e[k] = Edge(y , first[x] , d);
first[x] = k++;
} int sz[N] , dis[N] , f[N] , d[N] , cnt , root , ret;
bool use[N]; void find_root(int u , int fa , int size)
{
sz[u] = , f[u] = ;
int v;
for(int i=first[u] ; ~i ; i=e[i].next){
if(use[v=e[i].y] || v==fa) continue;
find_root(v , u , size);
sz[u] += sz[v] ;
f[u] = max(f[u] , sz[v]);
}
f[u] = max(f[u] , size-sz[u]);
if(f[u]<f[root]) root = u;
} void dfs(int u , int fa)
{
d[cnt++] = dis[u];
sz[u] = ;
int v;
for(int i=first[u] ; ~i ; i=e[i].next){
if(use[v=e[i].y] || v==fa) continue;
dis[v] = dis[u]+e[i].d;
if(dis[v]>m) continue;
dfs(v , u);
sz[u] += sz[v];
}
} int cal(int u , int val)
{
dis[u] = val , cnt=;
dfs(u , );
sort(d , d+cnt);
int ret = ;
for(int l= , r=cnt- ; l<r ; )
if(d[l]+d[r]<=m) ret+=r-l++;
else r--;
return ret;
} void solve(int u)
{
ret+=cal(u , );
use[u] =true;
int v;
for(int i=first[u] ; ~i ; i=e[i].next){
if(use[v=e[i].y]) continue;
ret -= cal(v , e[i].d);
find_root(v , root= , sz[v]);
solve(root);
}
} int main()
{
// freopen("in.txt" , "r" , stdin);
int x,y,d;
while(scanf("%d%d" , &n , &m) , n+m)
{
memset(first , - , sizeof(first));
k = ;
for(int i= ; i<n ; i++){
scanf("%d%d%d" , &x , &y , &d);
add_edge(x , y , d);
add_edge(y , x , d);
}
memset(use , , sizeof(use));
ret= , f[] = 1e9;
find_root( , root= , n);
solve(root);
printf("%d\n" , ret);
}
}

POJ 1741 树的点分治的更多相关文章

  1. poj 1741 树的点分治(入门)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18205   Accepted: 5951 Description ...

  2. POJ 1741 树分治

    题目链接[http://poj.org/problem?id=1741] 题意: 给出一颗树,然后寻找点对(u,v)&&dis[u][v] < k的对数. 题解: 这是一个很经典 ...

  3. poj 1741 树的分治

    思路:这题我是看 漆子超<分治算法在树的路径问题中的应用>写的. 附代码: #include<iostream> #include<cstring> #includ ...

  4. POJ 1741 Tree 树上点分治

    题目链接:http://poj.org/problem?id=1741 题意: 给定一棵包含$n$个点的带边权树,求距离小于等于K的点对数量 题解: 显然,枚举所有点的子树可以获得答案,但是朴素发$O ...

  5. POJ 1741 Tree (点分治)

                                                                        Tree Time Limit: 1000MS   Memory ...

  6. poj 1741 Tree(点分治)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15548   Accepted: 5054 Description ...

  7. POJ 1741 树上 点的 分治

    题意就是求树上距离小于等于K的点对有多少个 n2的算法肯定不行,因为1W个点 这就需要分治.可以看09年漆子超的论文 本题用到的是关于点的分治. 一个重要的问题是,为了防止退化,所以每次都要找到树的重 ...

  8. POJ 1741 树上的点分治

    题目大意: 找到树上点对间距离不大于K的点对数 这是一道简单的练习点分治的题,注意的是为了防止点分治时出现最后分治出来一颗子树为一条直线,所以用递归的方法求出最合适的root点 #include &l ...

  9. poj 1741 Tree(树的点分治)

    poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...

随机推荐

  1. C#_abstract的用法

    /// <summary> /// 抽像类 /// </summary> public abstract class Hello { private string msg = ...

  2. android下调用C,JNI调用

    1.eclipse中声明调用的C函数  com.example.test.MainActivity public native long RucMyfunction(int w,int h,int[] ...

  3. spring 编程式事务管理和声明式事务管理

    编程式事务管理 Spring 的编程式事务管理概述 在 Spring 出现以前,编程式事务管理对基于 POJO 的应用来说是唯一选择.用过 Hibernate 的人都知道,我们需要在代码中显式调用be ...

  4. qq被冻结怎么激活

    原文章http://jingyan.baidu.com/article/ce436649f43d4d3773afd3f2.html 一些QQ用户可能遇到过QQ被冻结的情况吧!不用着急,小编分享QQ被冻 ...

  5. java 多线程5(创建方式)

    实现Runnable接口: 问题1:Runnable实现类的对象是线程对象吗? 答:不是,该对象只不过是实现了Runnable接口的对象而已,只有是Thread或Thread的子类才是线程对象. 问题 ...

  6. 整理的一些模版LCS(连续和非连续)

    对于连续的最大串,我们称之为子串....非连续的称之为公共序列.. 代码: 非连续连续 int LCS(char a[],char b[],char sav[]){ int lena=strlen(a ...

  7. mvc伪静态<三> IIS配置

    上一篇已经已经讲述了mvc伪静态的代码实现. 下面以IIS 7.5为例演示一下IIS如何配置才能在服务器显示.html的伪静态 一.进入IIS,选择处理程序映射 二添加脚本映射 三根据你的处理程序的版 ...

  8. boxplot

    x1 = rand(20,6); x2 = .5+rand(20,6); x3 = randn(20,6); x = [x1;x2;x3]; x = x(:); g1 = [ones(size(x1) ...

  9. mysql 添加字段、删除字段、调整字段顺序 转

    ALTER TABLE — 更改表属性添加字段: alter table `user_movement_log`Add column GatewayId int  not null default 0 ...

  10. SqlHelper帮助类

    数据库连接字符串//Data Source=.;Initial Catalog=Test1;User Id=sa;Password=123456; public static class SqlHel ...