题面

传送门:https://www.luogu.org/problemnew/show/P4178


Solution

首先,长成这样的题目一定是淀粉质跑不掉了。

考虑到我们不知道K的大小,我们可以开一个splay来统计比某个数小的数的数量。

具体做法等我开淀粉质讲解的坑再满满填(咕)


Code

#include<iostream>
#include<vector>
#include<cstdio>
using namespace std;
long long read()
{
long long x=0,f=1; char c=getchar();
while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}
while(isdigit(c)){x=x*10+c-'0';c=getchar();}
return x*f;
}
const int N=40000+100;
struct road
{
long long w;
int to;
road(int A,long long B)
{
to=A,w=B;
}
};
struct SBT
{
#define root son[0][1]
int fa[N],son[N][2],w[N],size[N],cnt[N],to;
inline void update(int now)
{
size[now]=size[son[now][0]]+size[son[now][1]]+cnt[now];
}
inline void rotate(int x,int type)
{
int y=fa[x],z=fa[y];
fa[x]=z,son[z][y==son[z][1]]=x;
son[y][!type]=son[x][type],fa[son[x][type]]=y;
son[x][type]=y,fa[y]=x;
update(y),update(x);
}
void splay(int now,int to)
{
while(fa[now]!=to)
{
if(now==son[fa[now]][fa[now]==son[fa[fa[now]]][1]] and fa[fa[now]]!=to)
rotate(fa[now],now==son[fa[now]][0]),
rotate(now,now==son[fa[now]][0]);
else
rotate(now,now==son[fa[now]][0]);
}
}
inline void InitTree()
{
root=to=0;
}
inline void init(int now)
{
fa[now]=son[now][0]=son[now][1]=size[now]=w[now]=0;
}
void Insert(int num)
{
if(root==0)
{
root=++to;
init(root);
fa[root]=0;
w[root]=num,cnt[root]=1,update(root);
return;
}
int now=root,last=root;
while(now!=0)
{
if(w[now]==num)
{
cnt[now]++;
splay(now,0);
return;
}
last=now,now=son[now][num>w[now]];
}
now=++to,init(now);
w[now]=num,cnt[now]=1;
fa[now]=last,son[last][num>w[last]]=now;
update(now),splay(now,0);
}
int Query(int num)
{
int now=root,t_ans=0;
while(now!=0)
{
if(num>=w[now])
{
if(w[now]>=w[t_ans]) t_ans=now;
now=son[now][1];
}
else
now=son[now][0];
}
if(t_ans==0) return 0;
splay(t_ans,0);
return size[son[root][0]]+cnt[root];
}
#undef root
}sbt;
vector <road> e[N];
long long n,K;
bool vis[N],t_vis[N],done[N];
int size[N],cnt,root;
int GetSize(int now)
{
t_vis[now]=true;
size[now]=1;
for(int i=0;i<int(e[now].size());i++)
if(t_vis[e[now][i].to]==false and vis[e[now][i].to]==false)
size[now]+=GetSize(e[now][i].to);
t_vis[now]=0;
return size[now];
}
void GetRoot(int now)
{
t_vis[now]=true,size[now]=1;
bool OK=true;
for(int i=0;i<int(e[now].size());i++)
if(t_vis[e[now][i].to]==false and vis[e[now][i].to]==false)
{
GetRoot(e[now][i].to);
size[now]+=size[e[now][i].to];
if(size[e[now][i].to]>cnt/2)
OK=false;
}
if(cnt-size[now]>cnt/2) OK=false;
if(OK==true) root=now;
t_vis[now]=0;
}
int ans;
void dfs2(int now,long long dis,int type)
{
t_vis[now]=true;
if(type==1 and K-dis>=0)
ans+=sbt.Query(K-dis);
else if(type==2)
sbt.Insert(dis);
for(int i=0;i<int(e[now].size());i++)
if(t_vis[e[now][i].to]==false and done[e[now][i].to]==true)
dfs2(e[now][i].to,dis+e[now][i].w,type);
t_vis[now]=false;
}
void dfs(int now)
{
//cerr<<now<<endl;
vis[now]=true;
vector <road> son;
for(int i=0;i<int(e[now].size());i++)
if(vis[e[now][i].to]==false)
{
cnt=GetSize(e[now][i].to);
GetRoot(e[now][i].to);
son.push_back(e[now][i]);
dfs(root);
}
sbt.InitTree();
sbt.Insert(0);
for(int i=0;i<int(son.size());i++)
dfs2(son[i].to,son[i].w,1),dfs2(son[i].to,son[i].w,2);
done[now]=true;
}
int main()
{
freopen("4178.in","r",stdin); n=read();
for(int i=1;i<=n;i++)
e[i].reserve(4);
for(int i=1;i<n;i++)
{
long long s=read(),t=read(),w=read();
e[s].push_back(road(t,w));
e[t].push_back(road(s,w));
}
K=read(); cnt=GetSize(1);
GetRoot(1);
dfs(root); printf("%d",ans);
return 0;
}

[Luogu P4178]Tree (点分治+splay)的更多相关文章

  1. luogu P4178 Tree

    题目链接 luogu P4178 Tree 题解 点分治 代码 // luogu-judger-enable-o2 #include<cstdio> #include<algorit ...

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

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

  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 点分治

    思路:点分治 提交:1次 题解: 要求权值和\(\leq K\) 的路径,我们可以类比点分治的模板,把长为\(len\)是否存在,改为\(len\)的路径的条数,并用用树状数组维护前缀和,这样就可以求 ...

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

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

  7. POJ1471 Tree/洛谷P4178 Tree

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

  8. 【题解】[P4178 Tree]

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

  9. 【Luogu】P2596书架(Splay)

    题目链接 通过这题我加深了对Splay的理解,原来Splay的子树也是可以接来接去接到别的点上的,而不是只能旋转qwq 具体接的办法就是swap大法. 对于Top操作我们把当前节点Splay到根,然后 ...

随机推荐

  1. Android作业0930

    1.使用ListView和Adapter实现购物商城 Android 布局文件 <?xml version="1.0" encoding="utf-8"? ...

  2. Python练习题 042:Project Euler 014:最长的考拉兹序列

    本题来自 Project Euler 第14题:https://projecteuler.net/problem=14 ''' Project Euler: Problem 14: Longest C ...

  3. 票房和口碑称霸国庆档,用 Python 爬取猫眼评论区看看电影《我和我的家乡》到底有多牛

    今年的国庆档电影市场的表现还是比较强势的,两名主力<我和我的家乡>和<姜子牙>起到了很好的带头作用. <姜子牙>首日破 2 亿,一举刷新由<哪吒之魔童降世&g ...

  4. 更简易的机器学习-pycaret的安装和环境初始化

    1.安装 pip install pycaret 在谷歌colab中还要运行: from pycaret.utils import enable_colab enable_colab() 2.获取数据 ...

  5. Java虚拟机诊断利器

    Java虚拟机诊断利器  

  6. DX12龙书 02 - DirectXMath 库中与向量有关的类和函数

    0x00 需要用到的头文件 #include <DirectXMath> #include <DirectXPackedVector.h> using namespace Di ...

  7. mac电脑上安装appium报错:Failed at the appium-chromedriver@4.25.1 postinstall script.

    mac电脑安装appium,装好node.js后,使用命令:npm install appium@1.18.0,安装appium,报如下错误 ``` ERR! errno1 ERR! appium-c ...

  8. 网站搭建-云服务器ECS的使用

    1. 查看购买的云服务器实例,重置密码 2. 查找IP进行查看,此时网页时不存在的,开始配置: 3. 登录putty或其他终端,进行网页搭建,先按教程走一遍,然后再做个性化处理: #安装Apache ...

  9. Request对象基础应用实例代码一

    输入用户名:<br><input type="text" name="yhm"><br><br>输入密码:< ...

  10. Mac 每次都要执行source ~/.bash_profile 后,配置的环境变量才生效

    问题: 自己在 ~/.bash_profile 中配置环境变量, 可是每次重启终端后配置的不生效.需要重新执行 : $source ~/.bash_profile后,才会生效. 原因: 自己是在bas ...