中心节点就是树的中心,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. 2.10.2 section元素

    section元素 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> & ...

  2. jquery 应用

    http://www.jq22.com/ gwj6396668@163.com

  3. sql 触发器 针对一张表数据写入 另一张表 的增删改

    ALTER TRIGGER [dbo].[tri_test2] ON [dbo].[student] for INSERT,DELETE,UPDATEAS BEGIN if not exists (s ...

  4. CSS三栏布局的四种方法

    总括: 不管是三栏布局还是两栏布局都是我们在平时项目里经常使用的,也许你不知道什么事三栏布局什么是两栏布局但实际已经在用,或许你知道三栏布局的一种或两种方法,但实际操作中也只会依赖那某一种方法,本文具 ...

  5. 51nod 1013 3的幂的和 - 快速幂&除法取模

    题目地址:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1013 Konwledge Point: 快速幂:https:/ ...

  6. luogu 2257 YY的GCD

    题目描述: 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对. 题解: 代码: #include<cstdio> # ...

  7. 当执行计划中出现BITMAP CONVERSION TO ROWIDS关键字时,需要注意了。

    前言 前些天优化了一些耗费buffers较多的SQL,但系统CPU降低的效果不明显,于是又拉了awr报告,查看了SQL ordered by Gets排名前列的SQL. 分析 SQL代码: selec ...

  8. 零基础入门学习Python(23)--递归:这帮小兔崽子

    知识点 我们都知道兔子繁殖能力是惊人的,如下图: 我们可以用数学函数来定义: 假设我们需要求出经历了20个月后,总共有多少对小兔崽子? 迭代实现 def fab(n): n1 = 1 n2 = 1 n ...

  9. 将一个list中的元素的某一属性取出来单独放到一个list里面

    有很多时候我们会遇到这样的场景,就是要将一个list中的某一个元素中的某一属性单独拿出来放在一个新的list里面,这中时候,我们就可以用以下的方法来进行实现: List<DTO> item ...

  10. 一种RC滤波电路的验证

    在做电源的时候,在开关管的D极经常是出现不想看到的尖峰脉冲.以CCFL推挽式缓冲电路为准,我与一个同学杨进行了相应的验证. 其中的出来的现象和反思如下: 1,加上电阻和电容串联的滤波的确能将尖峰脉冲消 ...