Description

给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K

Input

N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是k

Output

一行,有多少对点之间的距离小于等于k

Sample Input

7
1 6 13
6 3 9
3 5 7
4 1 3
2 4 20
4 7 2
10

Sample Output

5
 
又是一道点分治的题,方法同聪聪可可。
 #include<cstdio>
#include<algorithm>
using namespace std;
const int N=;
int deep[N],head[N],son[N],f[N],d[N],root,ans,n,sum,cnt,k;
bool vis[N];
struct ee{int to,next,w;}e[N*];
void insert(int u,int v,int w){
e[++cnt].to=v;e[cnt].next=head[u];e[cnt].w=w;head[u]=cnt;
} void getroot(int x,int fa){
son[x]=;f[x]=;
for (int i=head[x];i;i=e[i].next){
int v=e[i].to;
if (vis[v]||v==fa) continue;
getroot(v,x);
son[x]+=son[v];
f[x]=max(f[x],son[v]);
}
f[x]=max(f[x],sum-f[x]);
if (f[x]<f[root]) root=x;
} void getdeep(int x,int fa){
deep[++deep[]]=d[x];
for (int i=head[x];i;i=e[i].next){
int v=e[i].to;
if (vis[v]||v==fa) continue;
d[v]=d[x]+e[i].w;
getdeep(v,x);
}
} int cal(int x,int now){
int t=;
d[x]=now;deep[]=;
getdeep(x,);
sort(deep+,deep+deep[]+);
int l=,r=deep[];
for (;l<r;) if (deep[l]+deep[r]<=k){t+=r-l;l++;}
else r--;
return t;
} void work(int x){
ans+=cal(x,);
vis[x]=;
for (int i=head[x];i;i=e[i].next){
int v=e[i].to;
if (!vis[v]) {
ans-=cal(v,e[i].w);sum=son[v];root=;
getroot(v,);
work(root);
}
}
} int main(){
scanf("%d",&n);
int u,v,w;
for (int i=;i<n;i++){
scanf("%d%d%d",&u,&v,&w);
insert(u,v,w);
insert(v,u,w);
}
f[]=sum=n;
scanf("%d",&k);
getroot(,);
work(root);
printf("%d",ans);
}

【BZOJ1468】Tree的更多相关文章

  1. 点分治【bzoj1468】 Tree

    点分治[bzoj1468] Tree Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边 ...

  2. 【BZOJ-1468】Tree 树分治

    1468: Tree Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1025  Solved: 534[Submit][Status][Discuss] ...

  3. 【BZOJ1468】Tree [点分治]

    Tree Time Limit: 10 Sec  Memory Limit: 64 MB[Submit][Status][Discuss] Description 给你一棵TREE,以及这棵树上边的距 ...

  4. 【POJ3237】Tree 树链剖分+线段树

    [POJ3237]Tree Description You are given a tree with N nodes. The tree's nodes are numbered 1 through ...

  5. 【BZOJ】【2631】Tree

    LCT 又一道名字叫做Tree的题目…… 看到删边加边什么的……又是动态树问题……果断再次搬出LCT. 这题比起上道[3282]tree的难点在于需要像线段树维护区间那样,进行树上路径的权值修改&am ...

  6. 【Luogu1501】Tree(Link-Cut Tree)

    [Luogu1501]Tree(Link-Cut Tree) 题面 洛谷 题解 \(LCT\)版子题 看到了顺手敲一下而已 注意一下,别乘爆了 #include<iostream> #in ...

  7. 【BZOJ3282】Tree (Link-Cut Tree)

    [BZOJ3282]Tree (Link-Cut Tree) 题面 BZOJ权限题呀,良心luogu上有 题解 Link-Cut Tree班子提 最近因为NOIP考炸了 学科也炸了 时间显然没有 以后 ...

  8. 【AtCoder3611】Tree MST(点分治,最小生成树)

    [AtCoder3611]Tree MST(点分治,最小生成树) 题面 AtCoder 洛谷 给定一棵\(n\)个节点的树,现有有一张完全图,两点\(x,y\)之间的边长为\(w[x]+w[y]+di ...

  9. 【HDU5909】Tree Cutting(FWT)

    [HDU5909]Tree Cutting(FWT) 题面 vjudge 题目大意: 给你一棵\(n\)个节点的树,每个节点都有一个小于\(m\)的权值 定义一棵子树的权值为所有节点的异或和,问权值为 ...

随机推荐

  1. ORACLE临时表 转 学习用

    转:http://www.2cto.com/database/201210/163979.html 临时表:像普通表一样,有结构,但是对数据的管理上不一样,临时表存储事务或会话的中间结果集,临时表中保 ...

  2. poj 3250 栈应用

    #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #d ...

  3. .net 创建属于自己的log类

    实习到现在已经接近三个月了,由于是校企联合培养计划,所以没有工资,所幸公司对于我们这些实习生并没有什么要求,刚开始我还觉得要做点什么才能学得快,可是到了后来,发现公司安排给我们的任务并不紧要,也不算太 ...

  4. WCF配置文件详解 【转】

    ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...

  5. Java Concurrency - ScheduledThreadPoolExecutor

    The Executor framework provides the ThreadPoolExecutor class to execute Callable and Runnable tasks ...

  6. Jersey(1.19.1) - Representations and Java Types

    Previous sections on @Produces and @Consumes referred to MIME media types of representations and sho ...

  7. ASP多行多列显示代码

    <table width="98%" border="0" align="center"> <tr> <% S ...

  8. 和阿文一起学H5--设计稿尺寸全攻略

  9. C# DateTimePicker控件详解

    1.同时显示日期和时间 DateTimePicker dtp = new DateTimePicker(); dtp.Format = DateTimePickerFormat.Custom;dtp. ...

  10. Redirect and POST in ASP.NET

    http://www.codeproject.com/Articles/37539/Redirect-and-POST-in-ASP-NET