中心节点就是树的中心,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. 使用GetLogicalDriveStrings获取卷标

    #include <windows.h> #include <stdio.h> #define BUFSIZE 512 int main() { TCHAR szTemp[BU ...

  2. ASP.NET自学之路(转载)

    第一步 掌握一门NET面向对象语言,C#或VB.NET 我强烈反对在没系统学过一门面向对象(OO)语言的前提下去学ASP.NET. ASP.NET是一个全面向对象的技术,不懂OO,那绝对学不下去! 第 ...

  3. java线程池 多线程搜索文件包含关键字所在的文件路径

    文件读取和操作类 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; publi ...

  4. Java中this、static关键字的内存图解

    Java中的关键字有很多,abstract  default  goto*  null  switch  boolean  do  if  package  nchronzed  break  dou ...

  5. popToViewController

    看到群里有人问popToViewController的用法 就写了下了 希望能帮到有需要的人 [self.navigationController popToViewController:[self. ...

  6. vue props传值方法

    <template> <div class="hello"> <ul> <li v-for="(item, index) in ...

  7. C++ 标准模板库介绍(STL)

    1. STL 基本介绍 C++ STL(标准模板库)是惠普实验室开发的一系列软件的统称,是一套功能强大的 C++ 模板类.STL的目的是为了标准化组件,这样就不用重新开发,让后来者可以使用现成的组件, ...

  8. Layui表格之多列合并展示

    前言: 当我们在使用Layui的时候,有时表格中的列比较多,展示出来肯定是有问题的,这样就不得不舍弃一些列不展示,不展示是一种解决方案,但是更好的解决方案应该是合并展示. 这里的展示不是合并单元格,合 ...

  9. Redis 压缩存储的配置

    如题,redis是采用了ziplist 元素在不足一定数量时采用压缩存储 hash: zset: list: 如上图所示: ziplist-entries:最大元素数量(即存储了多少个元素) zipl ...

  10. 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 ...