【BZOJ1468】Tree
Description
Input
Output
Sample Input
1 6 13
6 3 9
3 5 7
4 1 3
2 4 20
4 7 2
10
Sample Output
#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的更多相关文章
- 点分治【bzoj1468】 Tree
点分治[bzoj1468] Tree Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边 ...
- 【BZOJ-1468】Tree 树分治
1468: Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1025 Solved: 534[Submit][Status][Discuss] ...
- 【BZOJ1468】Tree [点分治]
Tree Time Limit: 10 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description 给你一棵TREE,以及这棵树上边的距 ...
- 【POJ3237】Tree 树链剖分+线段树
[POJ3237]Tree Description You are given a tree with N nodes. The tree's nodes are numbered 1 through ...
- 【BZOJ】【2631】Tree
LCT 又一道名字叫做Tree的题目…… 看到删边加边什么的……又是动态树问题……果断再次搬出LCT. 这题比起上道[3282]tree的难点在于需要像线段树维护区间那样,进行树上路径的权值修改&am ...
- 【Luogu1501】Tree(Link-Cut Tree)
[Luogu1501]Tree(Link-Cut Tree) 题面 洛谷 题解 \(LCT\)版子题 看到了顺手敲一下而已 注意一下,别乘爆了 #include<iostream> #in ...
- 【BZOJ3282】Tree (Link-Cut Tree)
[BZOJ3282]Tree (Link-Cut Tree) 题面 BZOJ权限题呀,良心luogu上有 题解 Link-Cut Tree班子提 最近因为NOIP考炸了 学科也炸了 时间显然没有 以后 ...
- 【AtCoder3611】Tree MST(点分治,最小生成树)
[AtCoder3611]Tree MST(点分治,最小生成树) 题面 AtCoder 洛谷 给定一棵\(n\)个节点的树,现有有一张完全图,两点\(x,y\)之间的边长为\(w[x]+w[y]+di ...
- 【HDU5909】Tree Cutting(FWT)
[HDU5909]Tree Cutting(FWT) 题面 vjudge 题目大意: 给你一棵\(n\)个节点的树,每个节点都有一个小于\(m\)的权值 定义一棵子树的权值为所有节点的异或和,问权值为 ...
随机推荐
- [改善Java代码]正确使用String,StringBuffer,StringBuilder
CharSequence接口有三个实现类与字符串有关:String,StringBuffer,StringBuffer.虽然它们都与字符串有关,但是其处理机制是不同的. String类是不可改变的量, ...
- [改善Java代码]线程优先级只使用三个等级
线程的优先级(priority)决定了线程获得CPU运行的机会,优先级越高获得的运行机会越大,优先级越低获得的机会越小.Java的线程有10个级别(准确的说是11个级别,级别为0的线程是JVM,应用程 ...
- poj 2057 树形dp 贪心
思路:设sum[i],le[i],back[i],worm[i]分别表示以i为根节点需要的完成步数,叶子节点数,失败回退步数,以及i是否有虫. #include<iostream> #in ...
- Android异常:异步任务导致的窗口句柄泄漏问题
05-05 10:36:41.009: E/WindowManager(4243): Activity com.tao.MyActivity has leaked window com.android ...
- EF 实体+ Newtonsoft.Json 输出JSON 时动态忽略属性的解决方法
最近的项目采用的是 ASP.NET mvc 4.0 + entity framework 5.0 ,后台以JSON形式抛出数据是借助于Newtonsoft.Json , 要想忽略的属性前面添加特性 ...
- webkit,HTML5头部标签
大家都知道在移动前端开发中添加一些webkit专属的HTML5头部标签,帮助浏览器更好解析html代码,更好地将移动web前端页面表现出来.本文整理一些HTML5头部<meta>标签常用的 ...
- this指针在不同情况下的指代
说不同情况了吧,首先要分有几种情况使用this,然后再说分别指代什么 1)如果是一般标签下函数调用,this指代全局对象,也就是window对象或者document对象 2)如果在嵌套函数中被嵌套的 ...
- MyBatis(3.2.3) - Configuring MyBatis using XML, typeAliases
In the SQL Mapper configuration file, we need to give the fully qualified name of the JavaBeans for ...
- MongoDB - Installing MongoDB on Windows
1. 在 http://www.mongodb.org/downloads 选择下载所需的版本. 2. 执行 msi 安装包,可通过 Custom 选项,选择安装目录. 3. 创建数据目录.Mongo ...
- 【转载】Android设计中的.9.png
转载自:腾讯ISUX (http://isux.tencent.com/android-ui-9-png.html) 在Android的设计过程中,为了适配不同的手机分辨率,图片大多需要拉伸或者压 ...