题链:

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. Beta冲刺NO.3

    Beta冲刺 第三天 1. 昨天的困难 1.昨天的困难主要集中在对Ajax的使用上,不熟悉这种语法,所以也就浪费了时间,导致昨天的批量删除没有完全完成. 2.由于之前的网页构造style很乱,导致修改 ...

  2. Beta版本敏捷冲刺每日报告——Day3

    1.情况简述 Beta阶段第三次Scrum Meeting 敏捷开发起止时间 2017.11.4 08:00 -- 2017.11.4 22:00 讨论时间地点 2017.11.4晚9:00,软工所实 ...

  3. Linux下硬盘分区

    1  fdisk -l查看硬盘及分区信息 我的系统(Archlinux)下的命令效果如下: 由上面的图片可以得知该系统只挂载了1个硬盘,命名为sda,其有2个主分区,sda1和sda2,至于为什么这么 ...

  4. 利用python处理文档中各字段出现的次数并排序

    import string path = 'waldnn' with open(path,'r') as text: words = [raw_word.strip(string.punctuatio ...

  5. 几款有用的AndroidStudio插件

    1.Android Parcelable code generator 顾名思义,这是个生成实现了Parcelable接口的代码的插件. 在你的类中,按下alt + insert键弹出插入代码的上下文 ...

  6. IIS 配置 FTP 网站

    在 服务器管理器 的 Web服务器IIS 上安装 FTP 服务 在 IIS管理器 添加FTP网站 配置防火墙规则 说明:服务器环境是Windows Server 2008 R2,IIS7.5. 1. ...

  7. wordpress | WP Mail SMTP使用QQ邮箱发布失败的解决办法

    在使用contact form 7插件时遇到邮件发送失败的问题,经过检查发现是因为服务器不支持mail()函数,判断是否支持mail()函数可以参考http://www.diyzhan.com/201 ...

  8. 说说Java代理模式

    代理实现可以分为静态代理和动态代理. 静态代理 静态代理模式其实很常见,比如买火车票这件小事:黄牛相当于是火车站的代理,我们可以通过黄牛买票,但只能去火车站进行改签和退票.在代码实现中相当于为一个委托 ...

  9. angular2 学习笔记 ( 4.0 初探 )

    目前是 4.0.0-rc.2. 刚好有个小项目要开发,就直接拿它来试水啦. 更新 cli 到最新版, 创建项目, 然后 follow https://github.com/angular/angula ...

  10. python与mongodb的交互 增删改差

    首先引入包: pip install pymongo需要用到如下对象: MongoClient对象:用于与MongoDB服务器建立连接 client=MongoClient('主机ip',端口) Da ...