C++之路进阶——bzoj1468(tree)

| F.A.Qs | Home | Discuss | ProblemSet | Status | Ranklist | Contest | ModifyUser gryz2016 | Logout | 捐赠本站 |
|---|
1468: Tree
Time Limit: 10 Sec Memory Limit: 64 MB
Submit: 774 Solved: 412
[Submit][Status][Discuss]
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
HINT
题解:
点分。。。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#define maxn 400100 using namespace std; int n,m,root,sum,ans,cnt,head[maxn],son[maxn],f[maxn],vis[maxn],deep[maxn],d[maxn]; struct ss
{
int to;
int next;
int w;
}e[maxn]; void insert(int u,int v,int w)
{
e[++cnt].to=v; e[cnt].next=head[u]; e[cnt].w=w; head[u]=cnt;
e[++cnt].to=u; e[cnt].next=head[v]; e[cnt].w=w; head[v]=cnt;
} void getroot(int x,int fa)
{
son[x]=;
f[x]=;
for (int i=head[x];i;i=e[i].next)
if (!vis[e[i].to]&&e[i].to!=fa)
{
getroot(e[i].to,x);
son[x]+=son[e[i].to];
f[x]=max(f[x],son[e[i].to]);
}
f[x]=max(f[x],sum-son[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)
if (!vis[e[i].to]&&e[i].to!=fa)
{
d[e[i].to]=d[x]+e[i].w;
getdeep(e[i].to,x);
}
} int cal(int x,int now)
{
d[x]=now;
deep[]=;
getdeep(x,);
sort(deep+,deep+deep[]+);
int t=,l,r;
for(l=,r=deep[];l<r;)
{
if(deep[l]+deep[r]<=m){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)
if(!vis[e[i].to])
{
ans-=cal(e[i].to,e[i].w);
sum=son[e[i].to];
root=;
getroot(e[i].to,root);
work(root);
}
} int main()
{
scanf("%d",&n);
for (int i=;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
insert(u,v,w);
}
scanf("%d",&m);
sum=n;
f[]=0x7fffffff;
getroot(,);
work();
printf("%d",ans);
}
C++之路进阶——bzoj1468(tree)的更多相关文章
- POJ1741 Tree + BZOJ1468 Tree 【点分治】
POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...
- easyui 进阶之tree的常见操作
前言 easyui是一种基于jQuery的用户界面插件集合,它为创建现代化,互动,JavaScript应用程序,提供必要的功能,完美支持HTML5网页的完整框架,节省网页开发的时间和规模.非常的简单易 ...
- bzoj1468 Tree
最经典的点分治题目,在递归子树的时候减去在算父亲时的不合法方案. #include<iostream> #include<cstdio> #include<cstring ...
- C++之路进阶codevs1269(匈牙利游戏)
1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description ...
- C++之路进阶——优先队列优化最短路径算法(dijkstra)
一般的dijkstra算法利用贪心的思想,每次找出最短边,然后优化到其他点的的距离,我们还采用贪心思路,但在寻找最短边进行优化,之前是双重for循环,现在我们用优先队列来实现. 代码解释: //样例程 ...
- BZOJ1468:Tree(点分治)
Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是 ...
- BZOJ1468: Tree & BZOJ3365: [Usaco2004 Feb]Distance Statistics 路程统计
[传送门:BZOJ1468&BZOJ3365] 简要题意: 给出一棵n个点的树,和每条边的边权,求出有多少个点对的距离<=k 题解: 点分治模板题 点分治的主要步骤: 1.首先选取一个点 ...
- shell进阶之tree、pstree、lsof命令详解
一.tree命令详解: 主要功能是创建文件列表,将所有文件以树的形式列出来 -a 显示所有文件和目录. -A 使用ASNI绘图字符显示树状图而非以ASCII字符组合. -C 在文件和目录清单加上色彩, ...
- C++之路进阶——HDU1880(魔咒词典)
---恢复内容开始--- New~ 欢迎参加2016多校联合训练的同学们~ 魔咒词典 Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 3 ...
随机推荐
- 【转】 Build a RESTful Web service using Jersey and Apache Tomcat 2009
Build a RESTful Web service using Jersey and Apache Tomcat Yi Ming Huang with Dong Fei Wu, Qing GuoP ...
- 基于java工程开发RMI服务端
ServiceRegist.java import java.rmi.Remote; import java.rmi.RemoteException; public interface Service ...
- php学习笔记 [预定义数组(超全局数组)]
<?php 预定义数组: * 自动全局变量---超全局数组 * * 1.包含了来自WEB服务器,客户端,运行环境和用户输入的数据 * 2.这些数组比较特别 * 3.全局范围内自动生效,都可以直 ...
- nrf51822裸机教程-IIC
关于IIC总线的核心有以下几点: :时钟线高电平期间必须保持数据线不变. :时钟线低电平期间可以改变数据. :时钟线和数据线上都要接上拉电阻,以使总线不工作时,两根线的电平都处于高电平状态. :每个传 ...
- 【C】漫谈C语言随机数
来说说C语言如何产生随机数. 有人会说这不简单?time() + srand() + rand() 3个函数不就OK了吗? 是的,不过,我们还是来看看原理比较好,也就是随机数是如何产生的. 这不无聊. ...
- Java中重点关键词的区分
1.final, finally, finalize的区别final-修饰符(关键字)如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承. 因此一个类不能既被声明为 abs ...
- Arcgis for Javascript API下类似于百度搜索A、B、C、D marker的实现方式
原文:Arcgis for Javascript API下类似于百度搜索A.B.C.D marker的实现方式 多说无益,首先贴两张图让大家看看具体的效果: 图1.百度地图搜索结果 图2.Arcgis ...
- 实验一 Java开发环境的熟悉-刘蔚然
一.实验内容 1. 使用JDK编译.运行简单的Java程序 2. 使用Eclipse 编辑.编译.运行.调试Java程序 要求: 完成实验.撰写实验报告 统计PSP时间 二.实验过程 使用JDK编译. ...
- How to Move Magento to a New Server or Domain
Magento is one of the fastest growing eCommerce platforms on the internet and with its recent acquis ...
- 5分钟弄懂Docker!
http://www.csdn.net/article/2014-07-02/2820497-what%27s-docker 关注点:1.DOCKER和VM的架构区别 2.Docker 的容器利用了 ...