题链:

http://codeforces.com/contest/293/problem/E
题解:

点分治,树状数组
大致思路和 POJ 1741 那道点分治入门题相同,
只是因为多了一个路径的边数限制,
所以在统计答案时,
要用数据结构维护一下在满足距离限制的情况下,有多少点也满足边数限制。
树状数组维护到当前的根(重心)的距离为x时的点的个数。
在calc函数中,记录dep[u]表示u到当前的根(重心)的边数,
然后统计u号点可以和多少点组成合法点对时,就查询树状数中有多少点满足到根的距离小于等于W-dep[u].(W是输入的边数限制)

代码:

#include<bits/stdc++.h>
#define MAXN 100005
using namespace std;
typedef pair<int,int>pii;
struct EDGE{
int ent;
int to[MAXN*2],nxt[MAXN*2],val[MAXN*2],head[MAXN];
void Reset(){ent=2; memset(head,0,sizeof(head));}
void Adde(int u,int v,int w){
to[ent]=v; val[ent]=w; nxt[ent]=head[u]; head[u]=ent++;
to[ent]=u; val[ent]=w; nxt[ent]=head[v]; head[v]=ent++;
}
}E;
struct BIT{
int val[MAXN],N;
int Lowbit(int x){return x&-x;}
void Reset(int n){
N=n+1; //memset(val,0,sizeof(val));
}
void Modify(int p,int x){
p++; for(;p<=N;p+=Lowbit(p)) val[p]+=x;
}
int Query(int p){
static int ans; ans=0; p++;
for(;p;p-=Lowbit(p)) ans+=val[p];
return ans;
}
}DT;
int N,K,W;
long long ANS;
int size[MAXN];
bool vis[MAXN];
void getroot(int u,int dad,int num,int &root,int &rootnum){
int maxnum=0; size[u]=0;
for(int i=E.head[u];i;i=E.nxt[i]){
if(E.to[i]==dad||vis[E.to[i]]) continue;
getroot(E.to[i],u,num,root,rootnum);
size[u]+=size[E.to[i]];
maxnum=max(maxnum,size[E.to[i]]);
}
size[u]++; maxnum=max(maxnum,num-size[u]);
if(maxnum<rootnum) root=u,rootnum=maxnum;
}
long long calc(int s,int len,int deep){
long long ret=0;
static pii A[MAXN];
static int dis[MAXN],dep[MAXN],reach[MAXN],rnt,ant;
static queue<int>Q;
ant=0; rnt++; Q.push(s);
dis[s]=len; reach[s]=rnt; dep[s]=deep;
while(!Q.empty()){
int u=Q.front(); Q.pop();
A[++ant]=make_pair(dis[u],dep[u]);
for(int i=E.head[u];i;i=E.nxt[i]){
int v=E.to[i];
if(reach[v]==rnt||vis[v]) continue;
dis[v]=dis[u]+E.val[i];
dep[v]=dep[u]+1;
reach[v]=rnt; Q.push(v);
}
}
sort(A+1,A+ant+1);
for(int i=1;i<=ant;i++) DT.Modify(A[i].second,1);
int l=1,r=ant;
while(l<=r){
if(A[l].first+A[r].first>K) DT.Modify(A[r].second,-1),r--;
else DT.Modify(A[l].second,-1),ret+=(W-A[l].second>=0?DT.Query(W-A[l].second):0),l++;
}
return ret;
}
void divide(int u){
int root=u,rootnum=size[u];
getroot(u,0,size[u],root,rootnum);
vis[root]=1;
ANS+=calc(root,0,0);
for(int i=E.head[root];i;i=E.nxt[i]) if(!vis[E.to[i]])
ANS-=calc(E.to[i],E.val[i],1);
for(int i=E.head[root];i;i=E.nxt[i]) if(!vis[E.to[i]])
divide(E.to[i]);
}
void read(int &x){
static int sign; static char ch;
x=0; sign=1; ch=getchar();
while(ch<'0'||'9'<ch){if(ch=='-')sign=-1;ch=getchar();}
while('0'<=ch&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
x=x*sign;
}
int main(){
read(N); read(W); read(K);
E.Reset(); DT.Reset(N); ANS=0;
for(int i=2,dad,w;i<=N;i++)
read(dad),read(w),E.Adde(dad,i,w);
size[1]=N; divide(1);
cout<<ANS<<endl;
return 0;
}

  

●CodeForce 293E Close Vertices的更多相关文章

  1. codeforces 293E Close Vertices

    题目链接 正解:点分治+树状数组. 点分治板子题,直接点分以后按照$w$排序,扫指针的时候把$w$合法的路径以$l$为下标加入树状数组统计就行了. 写这道题只是想看看我要写多久..事实证明我确实是老年 ...

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

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

  3. CodeForces 293E Close Vertices 点分治

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

  4. 强连通分量&hdu_1269&Codeforce 369D

    强连通分量 标签: 图论 算法介绍 还记得割点割边算法吗.回顾一下,tarjan算法,dfs过程中记录当前点的时间戳,并通过它的子节点的low值更新它的low,low值是这个点不通过它的父亲节点最远可 ...

  5. Codeforce - Street Lamps

    Bahosain is walking in a street of N blocks. Each block is either empty or has one lamp. If there is ...

  6. Codeforce#331 (Div. 2) A. Wilbur and Swimming Pool(谨以此题来纪念我的愚蠢)

    C time limit per test 1 second memory limit per test 256 megabytes input standard input output stand ...

  7. Codeforce Round #216 Div2

    e,还是写一下这次的codeforce吧...庆祝这个月的开始,看自己有能,b到什么样! cf的第二题,脑抽的交了错两次后过了pretest然后system的挂了..脑子里还有自己要挂的感觉,果然回头 ...

  8. dataStructure@ Find if there is a path between two vertices in a directed graph

    Given a Directed Graph and two vertices in it, check whether there is a path from the first given ve ...

  9. cf293E Close Vertices(树分治+BIT)

    E. Close Vertices You've got a weighted tree, consisting of n vertices. Each edge has a non-negative ...

随机推荐

  1. python 特殊方法实例

    import collections from random import choice card = collections.namedtuple('Card',['rank','suit']) # ...

  2. python 实现cm批量上传

    import requests import json import time import random url = 'http://cm.admin.xxxx.com/customer/aj_ad ...

  3. 前端之bootstrap模态框

    简介:模态框(Modal)是覆盖在父窗体上的子窗体.通常,目的是显示来自一个单独的源的内容,可以在不离开父窗体的情况下有一些互动.子窗体可提供信息.交互等. Modal简介 Modal实现弹出表单 M ...

  4. SpringBoot14 SpringBoot整合mybatis

    1 版本说明 springboot:2.0 jdk:1.8 2 创建springBoot项目 创建项目时勾选必要web,MySQL,mybatis相关依赖 创建完成后再pom文件中添加自动部署.lom ...

  5. Spring Security入门(3-4)Spring Security 异常处理、异常传递和异常获取

  6. .NET:持续进化的统一开发平台

    阅读文本大概需要 8 分钟. 标题使用的是进化这个词语,是因为 .NET 在不断的努力,也在不断的重构. 这篇文章的更多目的和意义在于科普,俗称"传教". # 持续进化的 .NET ...

  7. bootstrap时间区间设置方法

    我们在开发过程中经常有时间区间的要求,在多次"失败"及翻阅资料之后终于找到了适合我的方法,所以给大家分享出来. 基本需求为可以设置时间,设置时间区间,后一时间日期不可提前于前一时间 ...

  8. 浅谈 Mybatis中的 ${ } 和 #{ }的区别

    好了,真正做开发也差不多一年了.一直都是看别人的博客,自己懒得写,而且也不会写博客,今天就开始慢慢的练习一下写博客吧.前段时间刚好在公司遇到这样的问题. 一.举例说明 select * from us ...

  9. zipline-benchmarks.py文件改写

    改写原因:在这个模块中的 get_benchmark_returns() 方法回去谷歌财经下载对应SPY(类似于上证指数)的数据,但是Google上下载的数据在最后写入Io操作的时候会报一个恶心的编码 ...

  10. TR069网管协议应用在Android系统开发的前言

    随着智能平台的终端设备不断发展,迫切需要我们解决这些终端的管理问题,而现有的终端统一管理平台已经成熟,主要是基于tr069协议网管平台,比如华为的itms等终端管理平台.所以,这篇文章就是为了实现一种 ...