题目:https://www.luogu.org/problemnew/show/P4178

这道题要把 dep( dis? ) 加入一个 tmp 数组里,排序,计算点对,复杂度很美;

没有写 sort 竟然还有50分!

虽然调了很久不过第一次用对拍找出了错误!

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int const maxn=,inf=0x3f3f3f3f;
int n,hd[maxn],ct,to[maxn<<],nxt[maxn<<],w[maxn<<],tmp[maxn],l,r;
int sum,siz[maxn],dep[maxn],mx,rt;
ll ans,K;
bool vis[maxn];
void add(int x,int y,int z){to[++ct]=y; nxt[ct]=hd[x]; w[ct]=z; hd[x]=ct;}
void getrt(int x,int fa)
{
siz[x]=; int nmx=;
for(int i=hd[x],u;i;i=nxt[i])
{
if(to[i]==fa||vis[to[i]])continue;
getrt(u=to[i],x);
siz[x]+=siz[u]; nmx=max(nmx,siz[u]);
}
nmx=max(nmx,sum-siz[x]);
if(nmx<mx)mx=nmx,rt=x;
}
void getdep(int x,int fa)
{
tmp[++r]=dep[x];
for(int i=hd[x];i;i=nxt[i])
{
if(to[i]==fa||vis[to[i]])continue;
dep[to[i]]=dep[x]+w[i];
getdep(to[i],x);
}
}
ll calc(int x,int w)
{
dep[x]=w; r=; getdep(x,);
l=; ll ret=;
sort(tmp+l,tmp+r+);//!!!
while(l<=r)
{
if(tmp[l]+tmp[r]<=K)ret+=r-l,l++;//l -> l+1,l+2,...,r
else r--;
}
return ret;
}
void work(int x)
{
ans+=calc(x,); vis[x]=;
for(int i=hd[x];i;i=nxt[i])
{
if(vis[to[i]])continue;
ans-=calc(to[i],w[i]);
sum=siz[to[i]]; mx=inf;
// getrt(to[i],x);
getrt(to[i],);
work(rt);
}
}
int main()
{
scanf("%d",&n);
for(int i=,x,y,w;i<n;i++)
{
scanf("%d%d%d",&x,&y,&w);
add(x,y,w); add(y,x,w);
}
scanf("%lld",&K);
sum=n; mx=inf; getrt(,);
work(rt);
printf("%lld\n",ans);
return ;
}

洛谷 P4178 Tree —— 点分治的更多相关文章

  1. 洛谷P4178 Tree (点分治)

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

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

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

  3. POJ1471 Tree/洛谷P4178 Tree

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

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

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

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

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

  6. 洛谷 4178 Tree——点分治

    题目:https://www.luogu.org/problemnew/show/P4178 点分治.如果把每次的 dis 和 K-dis 都离散化,用树状数组找,是O(n*logn*logn),会T ...

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

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

  8. [洛谷P4178]Tree

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

  9. 洛谷 P4178 Tree

    #include<iostream> #include<cstdlib> #include<cstdio> #include<cmath> #inclu ...

随机推荐

  1. Linux下查看CPU信息、机器型号等硬件信息命令

    Linux下查看CPU信息.机器型号等硬件信息命令 编写一个bash脚本: vim info.sh #!/bin/bash cat /etc/issue echo "____________ ...

  2. 参考整理papers(一)

    https://blog.csdn.net/qq_14845119/article/details/82219246 整理了OCR的论文,可以参考一下.还有一些相关论文 论文(poster):Scen ...

  3. P1060 开心的金明(洛谷,动态规划递推,01背包轻微变形题)

    题目链接:P1060 开心的金明 基本思路: 基本上和01背包原题一样,不同点在于这里要的是最大重要度*价格总和,我们之前原题是 f[j]=max(f[j],f[j-v[i]]+p[i]); 那么这里 ...

  4. python 配置文件 ConfigParser模块

    ConfigParser模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 来看一个好多软件的常见文档格式如下 [DEFAULT] Se ...

  5. Datatable 插入一行数据到第一行

    var t = $('#passwdHOST').DataTable({ 'searching': true, 'ordering': false, 'autoWidth': false, dom: ...

  6. SocketServer 网络服务框架

    SocketServer简化了网络服务器的编写.它有4个类:TCPServer,UDPServer,UnixStreamServer,UnixDatagramServer.这4个类是同步进行处理的,另 ...

  7. Vuex实践小记

    1.目录结构 2.开始(安装vuex) npm install vuex --save 3.编辑store/index.js(创建一个Vuex.store状态管理对象) import Vue from ...

  8. 洛谷 2048 BZOJ 2006 [NOI2010]超级钢琴

    [题解] 贪心题.设五元组(mx,pos,l,r1,r2)表示最大值为mx,取得最大值的区间右端点为pos,区间左端点为l,区间右端点的可选区间为[r1,r2]. 每次从堆里拎出最大值,然后把这个区间 ...

  9. [ural1057][Amount of Degrees] (数位dp+进制模型)

    Discription Create a code to determine the amount of integers, lying in the set [X; Y] and being a s ...

  10. 转载 - Python里面关于 模块 和 包 和 __init__.py 的一些事

    出处:http://www.cnblogs.com/tqsummer/archive/2011/01/24/1943273.html python中的Module是比较重要的概念.常见的情况是,事先写 ...