【题目分析】

这貌似是做过第三道以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. Python3实现自动备份

    需求 将重要文件备份到指定目录,存档文件名称为“当前日期.zip”. 前提 1) Windows系统 2) Python 3以上版本 旗舰版 #!usr/bin/python # -*- coding ...

  2. sql中的exsits和not exsits

    select * from table where exsits(sql语句) :  括号中sql语句有数据则返回这些相关id的数据集 select * from table where not ex ...

  3. CSS-学习笔记四

    1.*用于匹配任何的标记 2.>用于指定父子节点关系 3.E+F毗邻元素选择器,匹配所以紧随E元素之后的同级元素F 4.E~F匹配所以E元素之后的同级元素F 5.名称[表达式] [att=val ...

  4. COGS 2566. [51nod 1129] 字符串最大值

    ★★★   输入文件:string_maxval.in   输出文件:string_maxval.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 一个字符串的前缀是指包 ...

  5. 在Mac里给Terminal终端自定义颜色

    Mac里终端显示默认是一种颜色,太单调了. 然而我们可以自定义这些颜色显示.进入-目录,编辑文件.bash_profile, 输入如下内容: 第三行那些fxfxax看起来是不是像天书?实际上是有规律的 ...

  6. Thread源码分析-java8

    1.Thread特性分析 守护线程Daemon 定性:支持性线程,主要用于程序中后台调度以及支持性工作. 当JVM中不存在Daemon线程时,JVM将会退出. 将一个线程设定为Daemon的方法: 调 ...

  7. python在d盘,robotframework引入seleniumlibrary报错

    在*** setting*** 中引入库   Library  SeleniumLibrary  报错 unknown seleniumlibrary library ,try to use quic ...

  8. AEE加密解密

     from Crypto.Cipher import AESfrom binascii import b2a_hex, a2b_hex class AesHandler(object):    def ...

  9. SC || 关于java迭代中修改迭代集合的操作

    在通过for循环遍历整个List/Map等的时候,如果想要进行remove的操作,这时就更改了迭代集合,会出现错误 一种方法是如果只会remove一个可以remove后直接break 另一种是把集合先 ...

  10. 【NOIP2017提高A组冲刺11.8】购物

    这个范围对DP不友好,和CF的一道C题非常像,贪心+后悔. 先使用k个优惠券购买k个q最小的(钱不购买则退出),同时把这k个p[i]-q[i]放入小根堆,然后将剩下的n-k个按p升序排序,记小根堆堆顶 ...