#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define rg register
#define _ 40010
using namespace std;
int n,record[_],num_of_edges,size[_],dfn[_],dfns,F[_],root,dis[_];//size[]不多说,F[]是i点儿子中最大子树大小
bool passed[_];
int stack[_],top,ans,k;
struct pp
{
int next,to,w;
}edge[_<<];
inline int read()
{
rg int save=,w=;rg char q=getchar();
while(q<''||q>''){if(q=='-')w=-;q=getchar();}
while(q>=''&&q<='')save=(save<<)+(save<<)+q-'',q=getchar();
return save*w;
}
void getroot(rg int i,rg int fa,rg int s)
{
size[i]=,F[i]=,dfn[i]=++dfns;
for(rg int j=record[i];j;j=edge[j].next)
{
rg int to=edge[j].to;
if(to!=fa&&!passed[to])
{
getroot(to,i,s);
F[i]=max(F[i],size[to]);
size[i]+=size[to];
}
}
F[i]=max(F[i],s-size[i]);
root=F[i]<F[root]?i:root;
}
void comput(rg int i,rg int dad)
{
stack[++top]=dis[i];//stack直接把边权记下来,无需记点
for(rg int j=record[i];j;j=edge[j].next)
{
rg int to=edge[j].to;
if((!passed[to])&&dad!=to)
{
dis[to]=dis[i]+edge[j].w;
comput(to,i);
}
}
}
inline int doit(rg int i,rg int d)//数出 该联通块中若使所有路径强制经过重心,<=k的有多少 (d为初始距离(去重用))
{
top=;dis[i]=d;
comput(i,);//计算各点到该重心的路径长
sort(stack+,stack+top+);
rg int l=,r=top,result=;
while(l<r)
if(stack[l]+stack[r]<=k)result+=r-l,l++;
else r--;
return result;
}
void dfs(rg int i,rg int all)
{
ans+=doit(i,);
passed[i]=;
for(rg int j=record[i];j;j=edge[j].next)
{
rg int to=edge[j].to;
if(!passed[to])
{
rg int s;
/* ☆☆☆*/ ans-=doit(to,edge[j].w);//去掉那些 在to的子树加上了[i->to的边权]被算作i的贡献的路径(这些路径本不存在)
if(dfn[i]<dfn[to])s=size[to];
else s=all-size[i];
root=;dfns=;
getroot(to,i,s);
// printf("%d\n",root);
dfs(to,s);
}
}
}
inline void add(rg int from,rg int to,rg int ww)
{
edge[++num_of_edges]=(pp){record[from],to,ww};
record[from]=num_of_edges;
}
int main()
{
n=read();
// cout<<"___\n"<<n<<endl;
rg int i,j;
for(i=;i<n;++i)
{
rg int x=read(),y=read(),ww=read();
add(x,y,ww),add(y,x,ww);
// cout<<x<<' '<<y<<' '<<ww<<endl;
}
k=read();
// cout<<k<<endl;
root=;F[]=n;//操作 "root=F[i]<F[root]?i:root" 需要
getroot(,,n);
dfs(root,n);
printf("%d\n",ans);
return ;
}

洛谷 P4178 Tree的更多相关文章

  1. 点分治模板(洛谷P4178 Tree)(树分治,树的重心,容斥原理)

    推荐YCB的总结 推荐你谷ysn等巨佬的详细题解 大致流程-- dfs求出当前树的重心 对当前树内经过重心的路径统计答案(一条路径由两条由重心到其它点的子路径合并而成) 容斥减去不合法情况(两条子路径 ...

  2. POJ1471 Tree/洛谷P4178 Tree

    Tree P4178 Tree 点分治板子. 点分治就是直接找树的重心进行暴力计算,每次树的深度不会超过子树深度的\(\frac{1}{2}\),计算完就消除影响,找下一个重心. 所以伪代码: voi ...

  3. 洛谷P4178 Tree (点分治)

    题目描述 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K 输入输出格式 输入格式:   N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下 ...

  4. 洛谷 P4178 Tree —— 点分治

    题目:https://www.luogu.org/problemnew/show/P4178 这道题要把 dep( dis? ) 加入一个 tmp 数组里,排序,计算点对,复杂度很美: 没有写 sor ...

  5. 洛谷P4178 Tree (算竞进阶习题)

    点分治 还是一道点分治,和前面那道题不同的是求所有距离小于等于k的点对. 如果只是等于k,我们可以把重心的每个子树分开处理,统计之后再合并,这样可以避免答案重复(也就是再同一个子树中出现路径之和为k的 ...

  6. 2018.07.20 洛谷P4178 Tree(点分治)

    传送门 又一道点分治. 直接维护子树内到根的所有路径长度,然后排序+双指针统计答案. 代码如下: #include<bits/stdc++.h> #define N 40005 using ...

  7. [洛谷P4178]Tree

    题目大意:给一棵树,问有多少条路径长度小于等于$k$ 题解:点分治 卡点:无 C++ Code: #include <cstdio> #include <algorithm> ...

  8. [洛谷P4178] Tree (点分治模板)

    题目略了吧,就是一棵树上有多少个点对之间的距离 \(\leq k\) \(n \leq 40000\) 算法 首先有一个 \(O(n^2)\) 的做法,枚举每一个点为起点,\(dfs\) 一遍可知其它 ...

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

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

随机推荐

  1. thinkphp中的exp查询

    今天遇到一个问题,就是在vendor表中查询出vendor_id = vendor_f_id的数据,其实使用原生的sql语句是非常简单的: select * from vendor where ven ...

  2. Workbox使用策略

    1.什么是Workbox Strategies? 当service workers 首次被引入时,可以设定一组常见的缓存策略. 缓存策略是一种模式,用于确定service workers 在收到fet ...

  3. python面试题之迭代器和生成器的区别

    1 迭代器是一个更抽象的概念,任何对象,如果它的类有next方法和iter方法返回自己本身.对于string.list.dict.tuple等这类容器对象,使用for循环遍历是很方便的.在后台for语 ...

  4. Eclipse连接MySQL数据库(傻瓜篇)

    我的环境:MySQL:mysql-essential-5.1.51-win32 jdbc驱动:我已经上传到csdn上一个:http://download.csdn.net/source/3451945 ...

  5. 从一个Activity打开另外一个Activity

    public class MainActivity extends Activity { /** Called when the activity is first created. */ @Over ...

  6. Jquery查找界面Html元素的方法(持续更新)

    1. 根据id来获取:$("#id") 2.根据class来获取:$(".class") 3.根据name来获取:$("[name=??]" ...

  7. 十万级百万级数据量的Excel文件导入并写入数据库

    一.需求分析 最近接到一个需求,导入十万级,甚至可能百万数据量的记录了车辆黑名单的Excel文件,借此机会分析下编码过程; 首先将这个需求拆解,发现有三个比较复杂的问题: 问题一:Excel文件导入后 ...

  8. 简单认识php

    1.输出语句: echo 'hello world'; 2.声明变量用 $ 符号 $uname = 'andy'; 3. php 拼接字符串用 点(.) //设置中文编码 header("c ...

  9. 怎么更新 WIN10里的SMBv1协议

    控制面板 ---启用或关闭Windows功能---打开SMBv1服务:

  10. js使用childnodes获取子节点时多了text节点

    当我们获取标签的节点时如果使用childnodes发现它会把空格和回车都算着节点,明明里面才有3个节点,结果显示5个,而且childnodes[0]="text" 在IE浏览器中没 ...