ZOJ 3684 Destroy 树的中心
中心节点就是树的中心,2遍dfs求到树的直径。而中心一定在直径上,顺着直径找到中心就够了。
然后能够一遍树形DP找到最小值或者二分+推断是否訪问到叶子节点。
#include <iostream>
#include<vector>
#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
int next;
int power;
int length;
}t;
vector<node> tree[10005];
int st,ed,maxs,n;
void dfs1(int now,int fa,int sum)
{
if(sum>maxs)
{
maxs=sum;
st=now;
}
for(int i=0;i<tree[now].size();i++)
{
int to=tree[now][i].next;
int length=tree[now][i].length;
if(to!=fa)
{
dfs1(to,now,sum+length);
}
}
}
struct node2
{
int fa;
int len;
}tzf[10005];
int top,num;
void dfs2(int now,int fa,int sum)
{
if(sum>maxs)
{
maxs=sum;
ed=now;
}
for(int i=0;i<tree[now].size();i++)
{
int to=tree[now][i].next;
int length=tree[now][i].length;
if(to!=fa)
{
tzf[to].fa=now;
tzf[to].len=length;
dfs2(to,now,sum+length);
}
}
}
bool cal(int now,int fa,int lim)
{
if(tree[now].size()==1)
{
return false;
}
for(int i=0;i<tree[now].size();i++)
{
int to=tree[now][i].next;
int power=tree[now][i].power;
if(to!=fa&&power>lim)
{
if(!cal(to,now,lim)) return false;
}
}
return true;
}
int main()
{
int maxx;
int a,b,c,d;
while(~scanf("%d",&n))
{
maxx=0;
for(int i=1;i<=n;i++) tree[i].clear();
for(int i=1;i<n;i++)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
maxx=max(maxx,d);
t.length=c;;
t.power=d;
t.next=b;
tree[a].push_back(t);
t.next=a;
tree[b].push_back(t);
}
maxs=0;
dfs1(1,-1,0); maxs=0;
tzf[st].fa=-1;
dfs2(st,-1,0); int now=ed,mins=0x3f3f3f3f;
int zx;
int sum=0; while(now!=-1)
{
int k=max(sum,maxs-sum);
if(k<mins)
{
mins=k;
zx=now;
}
sum+=tzf[now].len;
now=tzf[now].fa;
}
int l=0,r=maxx;
int ans=0;
while(l<=r)
{
int mid=(l+r)>>1;
if(cal(zx,-1,mid))
{
ans=mid;
r=mid-1;
}
else
{
l=mid+1;
}
}
printf("%d\n",ans);
}
return 0;
}
/*
7
1 2 8 2
1 3 2 2
3 6 4 0
2 4 3 0
2 5 10 0
5 7 12 0 */
ZOJ 3684 Destroy 树的中心的更多相关文章
- ZOJ 3684 Destroy
Destroy Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 36 ...
- zoj 3820 Building Fire Stations 树的中心
Building Fire Stations Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge ...
- 2014 Super Training #9 E Destroy --树的直径+树形DP
原题: ZOJ 3684 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3684 题意: 给你一棵树,树的根是树的中心(到其 ...
- zoj3820 Building Fire Stations 树的中心
题意:n个点的树,给出n-1条边,每条边长都是1,两个点建立防火站,使得其他点到防火站的最远距离最短. 思路:比赛的时候和队友一开始想是把这两个点拎起来,使得层数最少,有点像是树的中心,于是就猜测是将 ...
- Codeforces Round #260 (Div. 1) C. Civilization 树的中心+并查集
题目链接: 题目 C. Civilization time limit per test1 second memory limit per test256 megabytes inputstandar ...
- ZOJ - 2112 主席树套树状数组
题意:动态第k大,可单点更新,操作+原数组范围6e4 年轻人的第一道纯手工树套树 静态第k大可以很轻易的用权值主席树作差而得 而动态第k大由于修改第i个数会影响[i...n]棵树,因此我们不能在原主席 ...
- ZOJ 3279-Ants(线段树)
传送门:zoj 3279 Ants Ants Time Limit: 2 Seconds Memory Limit: 32768 KB echo is a curious and cleve ...
- ZOJ 3911 线段树
题意:有N个数字,M个操作,然后回答每个Q开头的询问 操作形式: A val pos:在pos位置上+val Q l r:询问l~r之间有多少个质数 R val l r:把l~r之间的数字替换成val ...
- zoj 3888 线段树 ***
卡n^2,用线段树降到nlogn 记录每个点上所覆盖线段的次小值,保证能有两条路径能走 #include<cstdio> #include<iostream> #include ...
随机推荐
- ARP是如何工作的?
我们知道,当我们在浏览器里面输入网址时,DNS服务器会自动把它解析为IP地址,浏览器实际上查找的是IP地址而不是网址.那么IP地址是如何转换为第二层物理地址(即MAC地址)的呢? 在局域网中,这是通过 ...
- docker数据卷的管理和使用
数据卷的使用,数据库可以保证如果容器出现问题但是数据不丢失的作用,比如MySQL/date下的数据 或者Nginx根目录下的index.html 查看数据卷 [root@docker ~]# dock ...
- Luogu P4299 首都 LCT
既然是中文题目,这里便不给题意. 分析: 这个题的做法据说是启发式合并? 但是我不会啊…… 进入正题,LCT是怎样做掉这道题的.记得在前面的一篇<大融合>的题解中,介绍过LCT维护子树信息 ...
- 还在为百度网盘下载速度太慢烦恼?chrome浏览器插件帮你解决!
百度网盘已然成为分享型网盘中一家独大的“大佬”了.时代就是这样不管你喜不喜欢,上网总会遇到些百度网盘共享的文件需要下载.然而,百度网盘对免费用户的限速已经到了“感人”的地步了,常常十多KB/秒的速度真 ...
- python链家网高并发异步爬虫and异步存入数据
python链家网二手房异步IO爬虫,使用asyncio.aiohttp和aiomysql 很多小伙伴初学python时都会学习到爬虫,刚入门时会使用requests.urllib这些同步的库进行单线 ...
- Spring Boot 2(一):【重磅】Spring Boot 2.0权威发布
就在今天Spring Boot2.0.0.RELEASE正式发布,今天早上在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误, ...
- 杭电 2111 Saving HDU (贪心)
Description 话说上回讲到海东集团面临内外交困,公司的元老也只剩下XHD夫妇二人了.显然,作为多年拼搏的商人,XHD不会坐以待毙的. 一天,当他正在苦思冥想解困良策的时候,突然想到了自己 ...
- os系统下安装Python2和Python3
一.下载Xcode工具 1.在App Store 里面下载并安装Xcode 2.安装好Xcode后就打开它,首次进入会有一些LicenceAgreement,点同意就是了,然后就进入到 这个界面: 3 ...
- swift -从相册中选择照片并上传
选择本地图片并上传是应用开发中一个比较常见的功能. 原文出自:www.hangge.com 转载请保留原文链接:http://www.hangge.com/blog/cache/det ...
- servlet页面没有跳转
Boolean b = userService.selectByParams(user);if (b) { req.getSession().setAttribute("loginname& ...