cf293E Close Vertices(树分治+BIT)
You've got a weighted tree, consisting of n vertices. Each edge has a non-negative weight. The length of the path between any two vertices of the tree is the number of edges in the path. The weight of the path is the total weight of all edges it contains.
Two vertices are close if there exists a path of length at most l between them and a path of weight at most w between them. Count the number of pairs of vertices v, u (v < u), such that vertices v and u are close.
The first line contains three integers n, l and w (1 ≤ n ≤ 105, 1 ≤ l ≤ n, 0 ≤ w ≤ 109). The next n - 1 lines contain the descriptions of the tree edges. The i-th line contains two integers pi, wi (1 ≤ pi < (i + 1), 0 ≤ wi ≤ 104), that mean that the i-th edge connects vertex (i + 1) and pi and has weight wi.
Consider the tree vertices indexed from 1 to n in some way.
Print a single integer — the number of close pairs.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
4 4 6
1 3
1 4
1 3
4
6 2 17
1 3
2 5
2 13
1 6
5 9
9
【思路】
树分治。大体思路和这道题相似。
不同的是有两个需要满足的条件,只需要把dis排序,扫描的同时用BIT维护dep的区间信息并统计答案即可。
【代码】
#include<map>
#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
#include<algorithm>
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; typedef long long LL;
const int N = 1e5+;
const int INF = 1e9+;
struct Edge {
int v,w;
Edge(int v=,int w=) :v(v),w(w){}
};
int n,W,L; LL ans;
int root,size,vis[N],siz[N],f[N],dis[N],dep[N],l1,l2;
pair<int,int> list[N];
vector<int> rec;
vector<Edge> g[N];
//BIT
int C[N];
void add(int x,int v) {
while(x<=n) C[x]+=v,x+=x&(-x);
}
int query(int x) {
int ans=;
while(x>) ans+=C[x],x-=x&(-x);
return ans;
}
//fenzhi
void getroot(int u,int fa) {
siz[u]=; f[u]=;
for(int i=;i<g[u].size();i++) {
int v=g[u][i].v;
if(v!=fa && !vis[v]) {
getroot(v,u);
siz[u]+=siz[v];
f[u]=max(f[u],siz[v]);
}
}
f[u]=max(f[u],size-siz[u]);
if(f[u]<f[root]) root=u;
}
void getdis(int u,int fa) {
list[++l1]=make_pair(dis[u],dep[u]);
for(int i=;i<g[u].size();i++) {
int v=g[u][i].v;
if(v!=fa && !vis[v]) {
dep[v]=dep[u]+;
dis[v]=dis[u]+g[u][i].w;
getdis(v,u);
}
}
}
LL getans(int l,int r) {
sort(list+l,list+r+);
LL res=; int j=l;
for(int i=r;i>=l;i--) {
while(j<=r && list[i].first+list[j].first<=W) {
add(list[j].second,);
rec.push_back(list[j].second);
j++;
}
if(list[i].first*<=W && list[i].second*<=L) res--;
res+=(LL)query(L-list[i].second);
}
return res/;
}
void clear() {
for(int i=;i<rec.size();i++) add(rec[i],-);
rec.clear();
}
void solve(int u) {
vis[u]=; l1=l2=;
LL S1=,S2=;
for(int i=(int)g[u].size()-;i>=;i--) {
int v=g[u][i].v;
if(!vis[v]) {
l2=l1+;
dep[v]=; dis[v]=g[u][i].w;
getdis(v,u);
clear();
S1+=getans(l2,l1);
}
}
FOR(i,,l1) //AT:根为终点
if(list[i].first<=W && list[i].second<=L) S2++;
clear(); //AT:clear
S2+=getans(,l1);
ans=ans+S2-S1;
for(int i=(int)g[u].size()-;i>=;i--) {
int v=g[u][i].v;
if(!vis[v]) {
size=siz[v]; root=;
getroot(v,-); solve(root);
}
}
} void read(int& x) {
char c=getchar(); int f=; x=;
while(!isdigit(c)) {if(c=='-')f=-;c=getchar();}
while(isdigit(c)) x=x*+c-'',c=getchar();
x*=f;
}
int main() {
//freopen("in.in","r",stdin);
//freopen("out.out","w",stdout);
read(n),read(L),read(W);
int u,v,w;
FOR(i,,n) {
read(u),read(v),read(w);
g[u].push_back(Edge(v,w));
g[v].push_back(Edge(u,w));
}
root=,f[]=INF,size=n;
getroot(,-); solve(root);
cout<<ans;
return ;
}
cf293E Close Vertices(树分治+BIT)的更多相关文章
- CF293E Close Vertices 点分治+树状数组
开始zz写了一个主席树,后来发现写个树状数组就行~ #include <cstdio> #include <vector> #include <algorithm> ...
- HDU 4812 D Tree 树分治+逆元处理
D Tree Problem Description There is a skyscraping tree standing on the playground of Nanjing Unive ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- POJ 1741.Tree 树分治 树形dp 树上点对
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24258 Accepted: 8062 Description ...
- poj 1744 tree 树分治
Tree Time Limit: 1000MS Memory Limit: 30000K Description Give a tree with n vertices,each ed ...
- poj 1741 楼教主男人八题之中的一个:树分治
http://poj.org/problem? id=1741 Description Give a tree with n vertices,each edge has a length(posit ...
- hdu-5977 Garden of Eden(树分治)
题目链接: Garden of Eden Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/ ...
- 【BZOJ-1468】Tree 树分治
1468: Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1025 Solved: 534[Submit][Status][Discuss] ...
- BZOJ 2152: 聪聪可可 树分治
2152: 聪聪可可 Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一 ...
随机推荐
- python3实现的web端json通信协议
之前有用python3实现过tcp协议的,后来又实现了http协议的通信,今天公司想做一个功能自动测试系统, 下午弄了一会,发现json格式的实现可以更简单一点,代码如下:简单解说一下,一般与服务器通 ...
- ubuntu下怎么合并windows下分割的zip包
cat ziptest.z* > google_bak.zip 点击打开链接http://blog.51yip.com/linux/988.html
- jekyll 的安装
静态网站生成器Jekyll 是一个简洁的.特别针对博客平台的 静态网站 生成器.它使用一个模板目录作为网站布局的基础框架,并在其上运行 Textile . Markdown 或 Liquid 标记语言 ...
- [C#]『CountdownEvent』任务并行库使用小计
System.Threading.CountdownEvent 是一个同步基元,它在收到一定次数的信号之后,将会解除对其等待线程的锁定. CountdownEvent 专门用于以下情况:您必须使用 ...
- 禁止选择文本和禁用右键 v2.0
禁止鼠标右键(注:在火狐浏览器没有起到效果作用) <script> function stop() { return false }; document.oncontextmenu = s ...
- PHP数组操作——获取数组最后一个值的方法
php开发过程中,可能经常需要对取出的数组要获取数组的最后健或值.在这里总结了三个方法,并且跟据他们三个方法在一些情况下如何使用的条件限制进行了说明. <?php $array=array(1, ...
- 使用PHP在共享内存中存储数据集
我们可以使用共享内存作为一种独特的存储选项,提供快速读/写操作和进程互操作性等优势. 对于 Web 应用程序,这意味着: 缓存存储(数据库查询.Web 服务数据.外部数据) 会话存储 应用程序之间的数 ...
- 限制apache错误日志大小
①配置错误日志 在http.conf配置: ErrorLog "| /opt/lampp/bin/rotatelogs /opt/lampp/logs/%Y_%m_%d_error_log ...
- 每天学点管理学知识——情绪ABC理论
什么是ABC理论 ABC理论(ABC Theory of Emotion)是由美国心理学家埃利斯创建的.就是认为激发事件A(activating event 的第一个英文字母)只是引发情绪和行为后果C ...
- 解决Android SDK Manager更新(一个更新Host的程序的原理实现和源码)
<ignore_js_op> 同学遇到了更新Android SDK的问题,而且Goagent现在也无法用来更新.就想到了用替代Host的方法,添加可用的谷歌地址来实现更新. ...