中心节点就是树的中心,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 树的中心的更多相关文章

  1. ZOJ 3684 Destroy

    Destroy Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 36 ...

  2. zoj 3820 Building Fire Stations 树的中心

    Building Fire Stations Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge ...

  3. 2014 Super Training #9 E Destroy --树的直径+树形DP

    原题: ZOJ 3684 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3684 题意: 给你一棵树,树的根是树的中心(到其 ...

  4. zoj3820 Building Fire Stations 树的中心

    题意:n个点的树,给出n-1条边,每条边长都是1,两个点建立防火站,使得其他点到防火站的最远距离最短. 思路:比赛的时候和队友一开始想是把这两个点拎起来,使得层数最少,有点像是树的中心,于是就猜测是将 ...

  5. Codeforces Round #260 (Div. 1) C. Civilization 树的中心+并查集

    题目链接: 题目 C. Civilization time limit per test1 second memory limit per test256 megabytes inputstandar ...

  6. ZOJ - 2112 主席树套树状数组

    题意:动态第k大,可单点更新,操作+原数组范围6e4 年轻人的第一道纯手工树套树 静态第k大可以很轻易的用权值主席树作差而得 而动态第k大由于修改第i个数会影响[i...n]棵树,因此我们不能在原主席 ...

  7. ZOJ 3279-Ants(线段树)

    传送门:zoj 3279 Ants Ants Time Limit: 2 Seconds      Memory Limit: 32768 KB echo is a curious and cleve ...

  8. ZOJ 3911 线段树

    题意:有N个数字,M个操作,然后回答每个Q开头的询问 操作形式: A val pos:在pos位置上+val Q l r:询问l~r之间有多少个质数 R val l r:把l~r之间的数字替换成val ...

  9. zoj 3888 线段树 ***

    卡n^2,用线段树降到nlogn 记录每个点上所覆盖线段的次小值,保证能有两条路径能走 #include<cstdio> #include<iostream> #include ...

随机推荐

  1. 计算机中的CPU

    今天写一下计算机中最核心的一部分,就是计算机的大脑---CPU.CPU也就是中央处理器(Central Processing Unit).中央处理器是一块超大规模的集成电路,是一台计算机的运算核心(C ...

  2. Tomcat:Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.

    可能原因一: 在本地tomcat启动正常并且访问正常的项目放在服务器上tomcat报以上错误. 本地tomcat为7.0.68,服务器上为7.0.86 错误原因:服务器tomcat版本过高. 解决办法 ...

  3. PHP实现微信第三方登录的方法

    本文实例讲述了PHP版微信第三方实现一键登录及获取用户信息的方法.分享给大家供大家参考,具体如下: 注意,要使用微信在第三方网页登录是需要“服务号”才可以哦,所以必须到官方申请 一开始你需要进入微信公 ...

  4. Python之爬虫-京东商品

    Python之爬虫-京东商品 #!/usr/bin/env python # coding: utf-8 from selenium import webdriver from selenium.we ...

  5. python的学习之路(三)

    一.set集合#!/usr/bin/env python# *_*coding:utf-8 *_*# Author: harson old_dict = { "#1": {'hos ...

  6. LeetCode(63)Unique Paths II

    题目 Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. Ho ...

  7. LeetCode(50) Pow(x,n)

    题目 Implement pow(x, n). Show Tags Show Similar Problems 分析 一个不利用标准幂次函数的,求幂算法实现. 参考了一个很好的解析博客:Pow(x,n ...

  8. LeetCode(70) Climbing Stairs

    题目 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cli ...

  9. Discuz论坛广告横幅大图在百度app内无法显示,百度app默认开启了广告屏蔽

    问题由来 前段时间搭的一个Discuz论坛上挂了2个广告横幅,网站的话收录还不错,然后客户就反应百度app上无法看到横幅. 由于我没有下载百度app,看不到效果我将信将疑,因为电脑,手机浏览器都是ok ...

  10. 每天学点Python之collections

    每天学点Python之collections 内容摘抄自:<python大法好>的每天学点Python之collections collections模块在内置数据类型(dict.list ...