【题目分析】

这貌似是做过第三道以Tree命名的题目了。

听说树分治的代码都很长,一直吓得不敢写,有生之年终于切掉这题。

点分治模板题目。自己YY了好久才写出来。

然后1A了,开心o(* ̄▽ ̄*)ブ

【代码】

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define maxn 20005
#define inf 0x3f3f3f3f
using namespace std; int n,k,h[maxn],to[maxn],ne[maxn],w[maxn],en,cnt;
int a[maxn],b[maxn],ban[maxn],siz[maxn],size,root;
int mx[maxn],now,now_mx,tot; void init(){en=cnt=0;memset(h,-1,sizeof h);memset(ban,0,sizeof ban);}
void add(int a,int b,int c){to[en]=b;w[en]=c;ne[en]=h[a];h[a]=en++;}
void rd(){for(int i=1;i<n;++i){int a,b,c;scanf("%d%d%d",&a,&b,&c);add(a,b,c);add(b,a,c);}} void dfs_size(int o,int fa)
{
siz[o]=1;
mx[o]=0;
for (int i=h[o];i>=0;i=ne[i])
if (to[i]!=fa&&!ban[to[i]]){
dfs_size(to[i],o);
siz[o]+=siz[to[i]];
mx[o]=max(mx[o],siz[to[i]]);
}
} void dfs_root(int o,int fa)
{
int now=max(mx[o],size-siz[o]);
if (now<now_mx) now_mx=now,root=o;
for (int i=h[o];i>=0;i=ne[i]) if (to[i]!=fa&&!ban[to[i]]) dfs_root(to[i],o);
} void dfs_dist(int o,int fa,int dist)
{
a[++tot]=dist;
for (int i=h[o];i>=0;i=ne[i])
if (to[i]!=fa&&!ban[to[i]])
dfs_dist(to[i],o,dist+w[i]);
} int cal()
{
int ret=0,j=tot;
sort(a+1,a+tot+1);
for (int i=1;i<=tot;++i)
{
while (a[j]+a[i]>k&&j) j--;
ret+=j;
if (j>i) ret--;
}
return ret/2;
} void dfs(int o)
{
dfs_size(o,0);
size=siz[o];
now_mx=inf;
dfs_root(o,0);
ban[root]=1;
for (int i=h[root];i>=0;i=ne[i])
{
if (!ban[to[i]])
{
tot=0;
dfs_dist(to[i],root,w[i]);
cnt-=cal();
}
}
tot=0;
dfs_dist(root,0,0);
cnt+=cal();
for (int i=h[root];i>=0;i=ne[i])
if (!ban[to[i]])
dfs(to[i]);
} int main()
{
while (scanf("%d%d",&n,&k)!=EOF&&n&&k)
{
init();rd();dfs(1);
printf("%d\n",cnt);
}
}

  

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

  1. POJ 1741.Tree 树分治 树形dp 树上点对

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24258   Accepted: 8062 Description ...

  2. POJ 1741 Tree 树分治

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

  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. POJ 1741 Tree(点分治点对<=k)

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

  5. [poj 1741]Tree 点分治

    题意 求树上距离不超过k的点对数,边权<=1000 题解     点分治.     点分治的思想就是取一个树的重心,这种路径只有两种情况,就是经过和不经过这个重心,如果不经过重心就把树剖开递归处 ...

  6. POJ - 1741 - Tree - 点分治 模板

    POJ-1741 题意: 对于带权的一棵树,求树中距离不超过k的点的对数. 思路: 点分治的裸题. 将这棵树分成很多小的树,分治求解. #include <algorithm> #incl ...

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

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

  8. POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量

    POJ 1741. Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 34141   Accepted: 11420 ...

  9. POJ 1741 Tree 求树上路径小于k的点对个数)

                                                                                                 POJ 174 ...

随机推荐

  1. uvm_misc——杂货铺(miscellaneous)

    uvm_misc 是个很有意思的文件夹,本质上就是个UVM的杂货铺,包含一些很重要的class, task, function, 但不知道给归类到哪儿,所以,uvm_misc就很好地承担了这个任务,几 ...

  2. Java中枚举类型Enum的一种使用方式

    枚举类定义如下: public enum Status { SCUUESS("1", "成功"), FAILED("2", "失败 ...

  3. Java的jdbc调用SQL Server存储过程Bug201906131120

    如果要查询结果,第一行使用set nocount on;可能可以解决问题.

  4. ArcMap所有Command GUID

    The information in this topic is useful if you're trying to programmatically find a built-in command ...

  5. Java异常 Exception类及其子类

    C语言时用if...else...来控制异常,Java语言所有的异常都可以用一个类来表示,不同类型的异常对应不同的子类异常,每个异常都对应一个异常类的对象. Java异常处理通过5个关键字try.ca ...

  6. viewDidLoad、loadView

    一.loadView永远不要主动调用这个函数.view controller会在view的property被请求并且当前view值为nil时调用这个函数.如果你手动创建view,你应该重载这个函数,且 ...

  7. linux设备驱动程序 - 待解决问题记录

    1.每个模式都有自己的内存映射,也即自己的地址空间?(P26) http://www.cnblogs.com/wuchanming/p/4360277.html (不知道是不是,没时间看)

  8. docker 运行tomcat 并部署 java web项目

    以下tomcat官方镜像中tomcat:7 和tomcat:8的目录. CATALINA_BASE: /usr/local/tomcat CATALINA_HOME: /usr/local/tomca ...

  9. 我的Python分析成长之路8

    Numpy数值计算基础 Numpy:是Numerical Python的简称,它是目前Python数值计算中最为基础的工具包,Numpy是用于数值科学计算的基础模块,不但能够完成科学计算的任而且能够用 ...

  10. Python 基本数据类型 (二) - 字符串

    str.expandtabs([tabsize]): str类型的expandtabs函数,有一个可选参数tabsize(制表符大小) 详细来说,expandtabs的意思就是,将字符串中的制表符\t ...