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 ...
随机推荐
- 【转】Stack Overflow研发副总裁:.NET技术并不差,合适自己就好
摘要:在QCon纽约大会上, Stack Exchange的工程部副总裁David Fullerton深入解析了如何使用C#.MS SQL等技术支撑Stack Overflow网站的单块应用架构,这个 ...
- LeetCode刷题第二天
2.给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们 ...
- SpringBoot配置文件注入值数据校验
package com.hoje.springboot.bean; import org.springframework.beans.factory.annotation.Value; import ...
- Java - Float与Double类型比较
https://blog.csdn.net/wcxiaoych/article/details/42806313
- Spring学习手札(二)面向切面编程AOP
AOP理解 Aspect Oriented Program面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. 但是,这种说法有些片面,因为在软件工程中,AOP的价值体现的并 ...
- LeetCode CombinationSum
class Solution { public: vector<vector<int> > combinationSum(vector<int> &cand ...
- SQL事务在存储过程的应用
-- ============================================= -- Author: jf_ou -- Create date: 2017-08-22 -- Desc ...
- CSS之换行
在项目中,常常遇到一些问题,可以通过CSS来快速解决,比如受到布局影响会导致内容溢出,这个时候就可以使用CSS换行解决 自动换行: { word-wrap:break-word; } 强制不换行: { ...
- 矩阵分解---QR正交分解,LU分解
相关概念: 正交矩阵:若一个方阵其行与列皆为正交的单位向量,则该矩阵为正交矩阵,且该矩阵的转置和其逆相等.两个向量正交的意思是两个向量的内积为 0 正定矩阵:如果对于所有的非零实系数向量x ,都有 x ...
- Pwn With longjmp
前言 这个是 seccon-ctf-quals-2016 的一个题,利用方式还是挺特殊的记录一下. 题目链接 http://t.cn/RnfeHLv 正文 首先看看程序的安全措施 haclh@ubun ...