题目链接

正解:点分治+树状数组。

点分治板子题,直接点分以后按照$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的更多相关文章

  1. CodeForces 293E Close Vertices 点分治

    题目传送门 题意:现在有一棵树,每条边的长度都为1,然后有一个权值,求存在多少个(u,v)点对,他们的路劲长度 <= l, 总权重 <= w. 题解: 1.找到树的重心. 2.求出每个点到 ...

  2. Codeforces 293E 点分治+cdq

    Codeforces 293E 传送门:https://codeforces.com/contest/293/problem/E 题意: 给你一颗边权一开始为0的树,然后给你n-1次操作,每次给边加上 ...

  3. ●CodeForce 293E Close Vertices

    题链: http://codeforces.com/contest/293/problem/E题解: 点分治,树状数组 大致思路和 POJ 1741 那道点分治入门题相同, 只是因为多了一个路径的边数 ...

  4. CF 293E Close Vertices——点分治

    题目:http://codeforces.com/contest/293/problem/E 仍旧是点分治.用容斥,w的限制用排序+两个指针解决, l 的限制就用树状数组.有0的话就都+1,相对大小不 ...

  5. Connecting Vertices CodeForces - 888F (图论,计数)

    链接 大意: 给定邻接表表示两点是否可以连接, 要求将图连成树, 且边不相交的方案数 n范围比较小, 可以直接区间dp $f[l][r]$表示答案, $g[l][r]$表示区间[l,r]全部连通且l, ...

  6. [ CodeForces 1065 B ] Vasya and Isolated Vertices

    \(\\\) \(Description\) 求一个\(N\)个点\(M\)条边的无向图,点度为 \(0\) 的点最多和最少的数量. \(N\le 10^5,M\le \frac {N\times ( ...

  7. codeforces 459E

    codeforces 459E E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabyte ...

  8. Codeforces 734E. Anton and Tree 搜索

    E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...

  9. 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 ...

随机推荐

  1. Spring Aop之@Before、@After、@Around、@AfterReturning

    在项目中使用到了@Aspect注解,故研究了下与其配套的几个注解,将测试结果记录下来 import org.aspectj.lang.JoinPoint; import org.aspectj.lan ...

  2. favi.icon是什么?

    各大网站都有属于自己的图标 概念解释: ico是Icon file的缩写,是Windows的图标文件格式的一种 1.制作ico格式的图片一个上传到服务器的根目录图片保存为favicon.ico 2.制 ...

  3. Head First Python学习笔记2——文件与异常

    文件处理 1.用open()就可以打开文件,但是请注意:文件里有中文请设置编码,如 :open("filepath","r",encoding="ut ...

  4. [日常] go语言圣经-获取URL练习题

    1.主要使用net/http和io/ioutil包 2.http.Get函数是创建HTTP请求的函数,resp这个结构体中,Body字段包括一个可读的服务器响应流 3.ioutil.ReadAll函数 ...

  5. MyBatis 常用写法

    MyBatis 常用写法 1.forEach 循环   forEach 元素的属性主要有 item, idnex, collection, open, separator, close. collec ...

  6. POJ 2480 Longge's problem 欧拉函数—————∑gcd(i, N) 1<=i <=N

    Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6383   Accepted: 2043 ...

  7. hdu 1043 八数码问题

    Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  8. PHP通用分页类page.php[仿google分页]

    <?php /** ** 通用php分页类.(仿Google样式) ** 只需提供记录总数与每页显示数两个参数.(已附详细使用说明..) ** 无需指定URL,链接由程序生成.方便用于检索结果分 ...

  9. FFmpeg实现将图片转换为视频

    ##名称:ffmpeg实现将图片转换为视频 ##平台:ubuntu(已经安装好了ffmpeg工具) ##日期:2017年12月10日 简介: 因为学习需要,需要将连续图片转换成视频,昨天和今天早上用o ...

  10. Spring Boot—12URL映射

    package com.sample.smartmap.controller; import java.util.List; import org.springframework.beans.fact ...