codeforces 293E Close Vertices
正解:点分治+树状数组。
点分治板子题,直接点分以后按照$w$排序,扫指针的时候把$w$合法的路径以$l$为下标加入树状数组统计就行了。
写这道题只是想看看我要写多久。。事实证明我确实是老年选手了,这种傻逼题写+调竟然用了$40min$。。
#include <bits/stdc++.h>
#define il inline
#define RG register
#define ll long long
#define lb(x) (x & -x)
#define N (300005) using namespace std; struct edge{ int nt,to,dis; }g[N];
struct data{ int l,w; }st[N]; int head[N],vis[N],dis[N],len[N],son[N],sz[N],c[N],l,w,n,num,top;
ll ans; il int gi(){
RG int x=,q=; RG char ch=getchar();
while ((ch<'' || ch>'') && ch!='-') ch=getchar();
if (ch=='-') q=-,ch=getchar();
while (ch>='' && ch<='') x=x*+ch-'',ch=getchar();
return q*x;
} il void insert(RG int from,RG int to,RG int dis){
g[++num]=(edge){head[from],to,dis},head[from]=num; return;
} il int cmp(const data &a,const data &b){ return a.w<b.w; } il void add(RG int x,RG int v){
if (!x) c[]+=v; else for (;x<=n;x+=lb(x)) c[x]+=v; return;
} il int query(RG int x){
RG int res=c[]; for (;x;x^=lb(x)) res+=c[x]; return res;
} il void getrt(RG int x,RG int p,RG int &rt){
son[x]=,sz[x]=;
for (RG int i=head[x],v;i;i=g[i].nt){
v=g[i].to; if (v==p || vis[v]) continue;
getrt(v,x,rt),sz[x]+=sz[v],son[x]=max(son[x],sz[v]);
}
son[x]=max(son[x],son[]-sz[x]);
if (son[rt]>=son[x]) rt=x; return;
} il void getdis(RG int x,RG int p){
st[++top]=(data){len[x],dis[x]},sz[x]=;
for (RG int i=head[x],v;i;i=g[i].nt){
v=g[i].to; if (v==p || vis[v]) continue;
len[v]=len[x]+,dis[v]=dis[x]+g[i].dis;
getdis(v,x),sz[x]+=sz[v];
}
return;
} il void calc(RG int rt,RG int llim,RG int wlim,RG int fg){
len[rt]=llim,dis[rt]=wlim,getdis(rt,top=);
sort(st+,st+top+,cmp); RG int p=;
for (RG int i=top;i;--i){
while (p<=top && st[p].w+st[i].w<=w) add(st[p++].l,);
if (l>=st[i].l) ans+=fg*query(l-st[i].l);
}
for (RG int i=;i<p;++i) add(st[i].l,-); return;
} il void solve(RG int x,RG int S){
RG int rt=; son[]=S,getrt(x,,rt),vis[rt]=,calc(rt,,,);
for (RG int i=head[rt];i;i=g[i].nt)
if (!vis[g[i].to]) calc(g[i].to,,g[i].dis,-);
for (RG int i=head[rt];i;i=g[i].nt)
if (!vis[g[i].to]) solve(g[i].to,sz[g[i].to]);
return;
} int main(){
#ifndef ONLINE_JUDGE
freopen("vertices.in","r",stdin);
freopen("vertices.out","w",stdout);
#endif
n=gi(),l=gi(),w=gi();
for (RG int i=,p,d;i<=n;++i)
p=gi(),d=gi(),insert(p,i,d),insert(i,p,d);
solve(,n),cout<<(ans-n)/; return ;
}
codeforces 293E Close Vertices的更多相关文章
- CodeForces 293E Close Vertices 点分治
题目传送门 题意:现在有一棵树,每条边的长度都为1,然后有一个权值,求存在多少个(u,v)点对,他们的路劲长度 <= l, 总权重 <= w. 题解: 1.找到树的重心. 2.求出每个点到 ...
- Codeforces 293E 点分治+cdq
Codeforces 293E 传送门:https://codeforces.com/contest/293/problem/E 题意: 给你一颗边权一开始为0的树,然后给你n-1次操作,每次给边加上 ...
- ●CodeForce 293E Close Vertices
题链: http://codeforces.com/contest/293/problem/E题解: 点分治,树状数组 大致思路和 POJ 1741 那道点分治入门题相同, 只是因为多了一个路径的边数 ...
- CF 293E Close Vertices——点分治
题目:http://codeforces.com/contest/293/problem/E 仍旧是点分治.用容斥,w的限制用排序+两个指针解决, l 的限制就用树状数组.有0的话就都+1,相对大小不 ...
- Connecting Vertices CodeForces - 888F (图论,计数)
链接 大意: 给定邻接表表示两点是否可以连接, 要求将图连成树, 且边不相交的方案数 n范围比较小, 可以直接区间dp $f[l][r]$表示答案, $g[l][r]$表示区间[l,r]全部连通且l, ...
- [ CodeForces 1065 B ] Vasya and Isolated Vertices
\(\\\) \(Description\) 求一个\(N\)个点\(M\)条边的无向图,点度为 \(0\) 的点最多和最少的数量. \(N\le 10^5,M\le \frac {N\times ( ...
- codeforces 459E
codeforces 459E E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabyte ...
- Codeforces 734E. Anton and Tree 搜索
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...
- Educational Codeforces Round 6 E. New Year Tree dfs+线段树
题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...
随机推荐
- HTML的map-area的使用
使用背景 在把设置图转成页面的时候,时常会遇到这种情况:一张小图片上有好多个可以点击的小图标,按常规的处理方法是把这一个一个的小图切出来,然后每个加个a标签进行跳转,但是这样会非常的浪费时间,而且会增 ...
- 微信小程序开发总结(详细)
转载: 小程序开发总结(详细) 这段时间一直在做小程序,总结下.后续可能会不断更新,努力写仔细点,争取让人看到就能会写. 一,页面结构. 这基本是小程序的标准目录结构.我们从上到下解释下:pages文 ...
- log4j.properties配置模板
# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml! # For all oth ...
- MySQL · 关系模型的基本术语
关系:整个二维表 关系名:表格名称 元组:行数据(记录) 属性:列数据(字段/分量) 属性名:列名称(字段名) 主键:唯一确定元组的属性组(关键字) 域:属性的取值范围 关系模式:关系的描述,表示为: ...
- SpringBoot+Mybatis整合入门(一)
SpringBoot+Mybatis 四步整合 第一步 添加依赖 springBoot+Mybatis相关依赖 <!--springBoot相关--> <parent> < ...
- 3 springboot配置文件
springboot 是习惯优于配置,抛弃SSM中繁琐的配置内容,大量的基础配置由框架的完成.所以我们现在没有用任何的配置文件,可以快速的将项目运行起来. 我们找到web模块中的application ...
- Java - HashMap值大于128问题
class Solution { public boolean wordPattern(String pattern, String str) { HashMap<Character,Integ ...
- vue项目中引入bootstrap
(1)引入Jquery文件,需要在bootstrap.min.js 之前引入. 1.npm install jquery --save-dev 2. plugins: [ new webpack.Pr ...
- web 学习资源
学习 https://skills.bugbank.cn/ https://github.com/JnuSimba/MiscSecNotes 靶场 http://skysec.top/2018/01/ ...
- 【Python】Java程序员学习Python(八)— 基本类型的基本运算
这一篇待写,毕竟基本运算都是通用的.