中心节点就是树的中心,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. 单页vue路由router

    Vue.js + vue-router 可以很简单的实现单页应用. <router-link> 是一个组件,该组件用于设置一个导航链接,切换不同 HTML 内容. to 属性为目标地址, ...

  2. 09C++指针

    指针 6.1 指针的概念 请务必弄清楚一个内存单元的地址与内存单元的内容这两个概念的区别.在程序中一般是通过变量名来对内存单元进行存取操作的.其实程序经过编译以后已经将变量名转换为变量的地址,对变量值 ...

  3. HTML5页面直接调用百度地图API,获取当前位置,直接导航目的地

    <!DOCTYPE html> <html lang="zh-cmn-Hans"> <meta charset="UTF-8"&g ...

  4. CodeForces - 1020C C - Elections(贪心+枚举)

    题目: 党派竞争投票 有n个人,m个党派,这n个人每个人有一个想要投的党派的编号Pi,如果想要这个人改变他的想法,那么就需要花费Ci元钱. 现在你是编号为1的党派,如果你想要赢(你的票数严格大于其他党 ...

  5. python3.6的requests库&HTMLTestRunner实现测试报告

    '''1. 在suite.addTest时,可以把需要的用例先写入一个列表list中,list当做addTest的参数:2. 在unittest.main(verbosity=2)中,默认为1,设置为 ...

  6. LeetCode 188. Best Time to Buy and Sell Stock IV (stock problem)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  7. [bzoj2005][Noi2010][能量采集] (容斥 or 欧拉函数)

    Description 栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可以采集太阳光的能量.在这些植物采集能量后, 栋栋再使用一个能量汇集机器把这些植物采集到的能量汇集到一起. 栋栋的植物种 ...

  8. nodejs的express框架创建https服务器

    一 openssl创建https私钥和证书 1.下载windows版openssl: http://slproweb.com/products/Win32OpenSSL.html Win64OpenS ...

  9. [OJ#40]后宫佳丽

    [OJ#40]后宫佳丽 试题描述 如果机房要关门了,或者有妹子在等你,你可以直接看最后一句话. Fyq 是一只饥渴的鸭子. Fyq 有一个充实的后宫,可惜他总是体力不支,为此他经常苦恼,总是想方设法让 ...

  10. hdu 1166 树状数组模板题

    #include<stdio.h> #include<string.h> #define N  51000 int  c[N],n; int number(int x) { r ...