题目链接

luogu P4178 Tree

题解

点分治

代码

// luogu-judger-enable-o2
#include<cstdio>
#include<algorithm>
inline int read() {
int x = 0,f = 1 ;
char c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = getchar();
return x * f;
}
#define LL long long
const int maxn = 80007;
struct node {
int v,w,next;
} edge[maxn << 1];
int head[maxn],num = 0;
inline void add_edge(int u,int v,int w) {
edge[++ num].v = v; edge[num].next = head[u];edge[num]. w= w; head[u] = num;
}
int n,K;
int root = 0,f[maxn],siz[maxn],g[maxn];
bool vis[maxn];
int tot;
void getroot(int x,int fa) {
siz[x] = 1; int num = 0; f[x] = 0;
for(int i = head[x];i;i = edge[i].next) {
int v = edge[i].v;
if(v == fa || vis[v]) continue;
getroot(v,x);
siz[x] += siz[v]; f[x] = std::max(f[x],siz[v]);
}
f[x] = std::max(f[x],tot - siz[x]);
if(f[root] > f[x]) root = x;
}
int l,r;
int nod[maxn],dis[maxn];
void getdis(int x,int fa) {
nod[++ r] = dis[x];
for(int i = head[x];i;i = edge[i].next) {
int v = edge[i].v;
if(v == fa || vis[v]) continue;
dis[v] = dis[x] + edge[i].w;
getdis(v,x);
}
}
LL calc(int x,int y) {
dis[x] = y;
r = 0,l = 1;
getdis(x,0);
LL ret = 0;
std::sort(nod + 1,nod + r + 1);
for(;l < r;)
if(nod[l] + nod[r] <= K) ret += r - l,l ++;
else r --;
return ret;
}
LL ans = 0;
void solve(int x) {
ans += calc(x,0);
vis[x] = true;
for(int i = head[x];i; i = edge[i].next) {
int v = edge[i].v;
if(vis[v]) continue;
ans -= calc(v,edge[i].w);
tot = siz[v]; root = 0;
getroot(v,0);
solve(root);
}
}
int main() {
f[0] = 0x3f3f3f3f;
n = read();
for(int u,v,w,i = 1;i < n;++ i) {
u = read(),v = read(),w = read();
add_edge(u,v,w); add_edge(v,u,w);
}
K = read();
tot = n;
getroot(1,0);
solve(root);
printf("%lld\n",ans);
return 0;
}

luogu P4178 Tree的更多相关文章

  1. [Luogu P4178]Tree (点分治+splay)

    题面 传送门:https://www.luogu.org/problemnew/show/P4178 Solution 首先,长成这样的题目一定是淀粉质跑不掉了. 考虑到我们不知道K的大小,我们可以开 ...

  2. [Luogu P4178]Tree 题解(点分治+平衡树)

    题目大意 给定一棵树,边带权,问有多少点对满足二者间距离$\leq K$,$n \leq 40000$. 题解 点分治专题首杀!$Jackpot!$ (本来看着题意比较简单想捡个软柿子捏,结果手断了… ...

  3. 【题解】[P4178 Tree]

    [题解]P4178 Tree 一道点分治模板好题 不知道是不是我见到的题目太少了,为什么这种题目都是暴力开值域的桶QAQ?? 问点对,考虑点分治吧.直接用值域树状数组开下来,统计的时候直接往树状数组里 ...

  4. POJ1471 Tree/洛谷P4178 Tree

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

  5. 洛谷 P4178 Tree —— 点分治

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

  6. 洛谷P4178 Tree (点分治)

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

  7. P4178 Tree(点分治)

    题面要求小于等于K的路径数目,我么很自然的想到点分治(不会的就戳我) 这道题的统计答案与模板题不一样的地方是由等于K到小于等于K 那么我们可以把每一个子节点到当前根(重心)的距离排序,然后用类似双指针 ...

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

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

  9. P4178 Tree

    最简单的点分治 淀粉质的思想: “分而治之”,缩小问题规模,合并求解: #include<cstdio> #include<cmath> #include<cstring ...

随机推荐

  1. Jetson tx1 安装ROS

    注意,是 Jetson TX1 系统版本: R24.2 参考链接: https://www.youtube.com/watch?v=-So2P0kRYsk

  2. CodeForces Contest #1137: Round #545 (Div. 1)

    比赛传送门:CF #1137. 比赛记录:点我. 每次都自闭的 div1 啊,什么时候才能上 IM 呢. [A]Skyscrapers 题意简述: 有一个 \(n\times m\) 的矩阵 \(a_ ...

  3. Pytorch 入门之Siamese网络

    首次体验Pytorch,本文参考于:github and PyTorch 中文网人脸相似度对比 本文主要熟悉Pytorch大致流程,修改了读取数据部分.没有采用原作者的ImageFolder方法:   ...

  4. Ubuntu14.04+caffe+CPU

    刚刚在上篇博客记录了windows10下GPU版本caffe的安装,正准备跑跑论文里的代码,发现好多命令都是.sh命令,这是linux系统的脚本文件.不能直接在windows下运行,于是我想把.sh转 ...

  5. AT91RM9200---定时器简介

    1.前言 系统定时器模块集成了3个不同的定时器 一个周期性间隔的定时器,用来为操作系统设置时基 一个看门狗定时器,可用于软件死锁时进行系统复位 一个实时时钟计数器用来记录流逝的时间 系统定时器时钟 这 ...

  6. Ajax jsonp 跨域请求实例

    跨域请求 JSONP的缺点则是:它只支持GET请求而不支持POST等其它类型的HTTP请求:它只支持跨域HTTP请求这种情况,不能解决不同域的两个页面之间如何进行JavaScript调用的问题. $. ...

  7. 玩树莓派(raspberry pi) 2/3 raspbian的遇到的一些问题

    raspberry pi买回来玩了一段时间,现在就记录一下入门遇到的一些问题吧. 首先是烧写镜像,和安装电脑系统是一样的道理. 先要有一个制作一个U启动盘.先将SD卡格式化,再用Win32DiskIm ...

  8. vue修饰符学习

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  9. CentOS配置通过DHCP的方式动态获取IP

    修改/etc/sysconfig/network NETWORKING=yes 修改/etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 ONBO ...

  10. Linear Algebra(未完待续)

    [矩阵消元] The result of multiplying a matrix by some vector is a combination of the columns of the matr ...