●CodeForce 293E Close Vertices
题链:
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的更多相关文章
- codeforces 293E Close Vertices
题目链接 正解:点分治+树状数组. 点分治板子题,直接点分以后按照$w$排序,扫指针的时候把$w$合法的路径以$l$为下标加入树状数组统计就行了. 写这道题只是想看看我要写多久..事实证明我确实是老年 ...
- CF 293E Close Vertices——点分治
题目:http://codeforces.com/contest/293/problem/E 仍旧是点分治.用容斥,w的限制用排序+两个指针解决, l 的限制就用树状数组.有0的话就都+1,相对大小不 ...
- CodeForces 293E Close Vertices 点分治
题目传送门 题意:现在有一棵树,每条边的长度都为1,然后有一个权值,求存在多少个(u,v)点对,他们的路劲长度 <= l, 总权重 <= w. 题解: 1.找到树的重心. 2.求出每个点到 ...
- 强连通分量&hdu_1269&Codeforce 369D
强连通分量 标签: 图论 算法介绍 还记得割点割边算法吗.回顾一下,tarjan算法,dfs过程中记录当前点的时间戳,并通过它的子节点的low值更新它的low,low值是这个点不通过它的父亲节点最远可 ...
- Codeforce - Street Lamps
Bahosain is walking in a street of N blocks. Each block is either empty or has one lamp. If there is ...
- 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 ...
- Codeforce Round #216 Div2
e,还是写一下这次的codeforce吧...庆祝这个月的开始,看自己有能,b到什么样! cf的第二题,脑抽的交了错两次后过了pretest然后system的挂了..脑子里还有自己要挂的感觉,果然回头 ...
- 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 ...
- cf293E Close Vertices(树分治+BIT)
E. Close Vertices You've got a weighted tree, consisting of n vertices. Each edge has a non-negative ...
随机推荐
- 在VS2017下配置OpenGL
这个方法适合初学者使用,较为简单方便. 第一,你的VS2017一定要安装了C/C++开发组件 可以打开Visual Studio Installer来查看 另外,确定你有安装NuGet包管理器,在单个 ...
- Java 中 compareTo方法问题
compareTo方法原理:先读取出字符串的第一个“字母”进行比较,比较的方法是ascii码表的值(字符所对应的十进制值),如果前面的大那么返回1,后面的大返回-1:此位置相同,继续比较下一位,直到最 ...
- 手把手教你 LabVIEW 串口仪器控制——VISA 驱动下载安装篇
仪器控制,核心在于 VISA 函数..有些仪器可能不需要 VISA,有自己的 DLL 什么的,我就管不着. 正常情况下,大家安装的 LabVIEW,都是不带 VISA 驱动 ...
- 前端面试题之html
1.简述<!DOCTYPE> 的作用,标准模式和兼容模式各有什么区别? <!DOCTYPE> 位于文档的第一行,告知浏览器使用哪种规范. 如果不写DOCTYPE,浏览器会进入混 ...
- SAN LUN Mapping出错导致文件系统共享冲突,数据恢复成功
[用户单位] 中国联通某分公司[数据恢复故障描述] SUN 光纤存储系统,中心存储为6枚300G硬盘组成的RAID6,划分为若干LUN,MAP到不同业务的服务器上,服务器上运行SUN SOLAR ...
- ( 转 ) 聊一聊C#的Equals()和GetHashCode()方法
聊一聊C#的Equals()和GetHashCode()方法 博客创建一年多,还是第一次写博文,有什么不对的地方还请多多指教. 关于这次写的内容可以说是老生长谈,百度一搜一大堆.大神可自行绕路. ...
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'like '%逸%'' at line 1
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...
- angular2 学习笔记 の 移动端开发 ( 手势 )
更新 : 2018-01-31 (hammer 的坑) hammer 的 pinch 在某种情况下会自动触发 panEnd,很奇葩. 解决方法就是记入时间呗 refer : https://githu ...
- Linux上 ps 命令的用法
ps a 显示现行终端机下的所有程序,包括其他用户的程序.2)ps -A 显示所有程序. 3)ps c 列出程序时,显示每个程序真正的指令名称,而不包含路径,参数或常驻服务的标示. 4)ps -e 此 ...
- C# 读取网页JSON数据
场景描述: 公司和别的系统需要对接,现在对方提供一个网址,数据都是json字符串,我需要对json数据进行处理. 提供的json数据如下格式 一.读取网址中的json数据 public string ...