题目链接


Solution

同 \(NOIP2007\) 树网的核 .

令 \(dist_u\) 为以 \(u\) 为根节点的子树中与 \(u\) 的最大距离.

\(~~~~dis_u\) 为 \(u\) 到直径中没有包括区间的一端的距离.

\(~~~~s\) 为直径.

题意很明确,要求直径上的一段区间使得 \(Max(dist_u(u\epsilon s),dis_u)\) 最小.

考虑 \(O(n)\) ,直接在直径上尺取就好了...

考虑 \(O(nlogn)\) ,直接二分+枚举起点就好了.

然而数据过水,直接打的 \(O(n^2)\) ,结果\(A\)了.


Code

#include<bits/stdc++.h>
using namespace std;
const int maxn=1008;
struct sj{
int to,next,w;
}a[maxn*2];
int head[maxn],size;
int n,s,x,y,w;
int read()
{
int f=1,w=0; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){w=w*10+ch-'0';ch=getchar();}
return f*w;
}
void add(int x,int y,int w)
{
a[++size].to=y;
a[size].next=head[x];
head[x]=size;a[size].w=w;
} int last,num,now,cnt,v[maxn],bcnt;
int ans[maxn],maxx,road[maxn];
int dist[maxn],b[maxn],ansb[maxn];
int sum[maxn];
void dfs(int x)
{
v[x]=1;
road[++cnt]=x;
for(int i=head[x];i;i=a[i].next)
{
int tt=a[i].to;
if(!v[tt])
{
now+=a[i].w;
b[++bcnt]=a[i].w;
dfs(tt); cnt--;bcnt--;
now-=a[i].w;
}
}
if(now>maxx)
{
num=cnt; last=x; maxx=now;
for(int i=1;i<=cnt;i++)
ans[i]=road[i],ansb[i]=b[i];
}
} void getdist(int x)
{
v[x]=1;
maxx=max(maxx,now);
for(int i=head[x];i;i=a[i].next)
{
int tt=a[i].to;
if(!v[tt])
{
now+=a[i].w;
getdist(tt);
now-=a[i].w;
}
}
} int main()
{
n=read(); s=read();
for(int i=1;i<n;i++)
{
x=read(); y=read(); w=read();
add(x,y,w); add(y,x,w);
}
dfs(1);
memset(v,0,sizeof(v));
maxx=0; cnt=0; now=0;
dfs(last);
memset(v,0,sizeof(v));
for(int i=1;i<=num;i++)v[ans[i]]=1;
for(int i=2;i<=num;i++)sum[i]=sum[i-1]+ansb[i-1];
for(int i=1;i<=num;i++)
{maxx=0,getdist(ans[i]),dist[i]=maxx;} int q[maxn]={0},kk=192608173;
int h=1,t=0;
for(int i=1;i<=num;i++)
{
int fuck=-1;
for(int j=i;j<=num;j++)
{
fuck=max(fuck,dist[j]);
if(sum[j]-sum[i]>s)break;
kk=min(kk,max(max(sum[i],sum[num]-sum[j]),fuck));
}
}
cout<<kk<<endl;
}

[SDOI2011] 消防 (树的直径,尺取法)的更多相关文章

  1. 【SDOI2011 第2轮 DAY1】消防 -[树的直径+树链剖分][解题报告]

    [SDOI2011 第2轮 DAY1]消防 题面: SDOI2011 第2轮 DAY1]消防 时间限制 : 20000 MS 空间限制 : 565536 KB 问题描述 时限\(2s\) 某个国家有\ ...

  2. UVA 11990 `Dynamic'' Inversion CDQ分治, 归并排序, 树状数组, 尺取法, 三偏序统计 难度: 2

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  3. 离散化+线段树/二分查找/尺取法 HDOJ 4325 Flowers

    题目传送门 题意:给出一些花开花落的时间,问某个时间花开的有几朵 分析:这题有好几种做法,正解应该是离散化坐标后用线段树成端更新和单点询问.还有排序后二分查找询问点之前总花开数和总花凋谢数,作差是当前 ...

  4. bzoj 2282 [Sdoi2011]消防(树的直径,二分)

    Description 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超越宇宙的热情,所以这个国家 ...

  5. [SDOI2011]消防(树的直径)

    [SDOI2011]消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超越宇宙的热情, ...

  6. [SDOI2011]消防(贪心,图论,树的直径)

    [SDOI2011]消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超越宇宙的热情, ...

  7. hdu 4123 Bob’s Race 树的直径+rmq+尺取

    Bob’s Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Probl ...

  8. luogu1712 区间 (尺取法+线段树)

    先把区间按照长度从小到大排序,然后用尺取法来做 大概就是先一点一点把区间算上 直到某个点被覆盖了m次,然后一点一点把最前面的区间扔掉,直到没有点被覆盖m次,这样反复做(相当于是它选择的区间左右端点在那 ...

  9. 2018.08.17 bzoj4653: [Noi2016]区间(线段树+尺取法)

    传送门 将坐标离散化之后直接用尺取法(双指针)+线段树维护. 其实就是说只要目前所有点的被覆盖次数是大于等于m的就移动左指针删除区间更新答案,否则移动右指针加入区间更新答案. 话说忘记排序以及建树的时 ...

随机推荐

  1. [学习笔记] Markdown语法备忘

    Markdown语法总结 标题 # 这是一级标题 ## 这是二级标题 ### 这是三级标题 #### 这是四级标题 ##### 这是五级标题 ###### 这是六级标题 注意#后面要加空格 字体 ** ...

  2. java的IO机制

    BIO.NIO.AIO -----> Block-IO : inputStream 和OutputStream,Reader和Writer 1个连接,启动一个线程,这样导致很大的线程开销 NIO ...

  3. db2的定时备份

    定时任务: db2.bat db2cmd -i -w db2_backup.bat exit db2_backup.bat db2 connect to TEST db2 force applicat ...

  4. 编译-LAMP基于fastcgi

    前言 最近没更新新篇幅了,今天就来点干活,过多的也不说了下面着手干!干!干! 准备环境 centos7.5 apr-1.6.3.tar.gz  apr-util-1.6.1.tar.gz      h ...

  5. python入门:最基本的用户登录用户登录,三次错误机会

    #!/usr/bin/env python # -*- coding:utf-8 -*- #用户登录,三次错误机会 """ 导入getpass,给x赋值为1,while真 ...

  6. vue.js笔记1.0

    事件: 事件冒泡行为: 1.@click="show($event)" show:function (ev) { ev.cancelBubble=true; } 2.@click. ...

  7. LeetCode(226)Invert Binary Tree

    题目 分析 交换二叉树的左右子树. 递归非递归两种方法实现. AC代码 class Solution { public: //递归实现 TreeNode* invertTree(TreeNode* r ...

  8. LeetCode(278)First Bad Version

    题目 You are a product manager and currently leading a team to develop a new product. Unfortunately, t ...

  9. ACM-ICPC 2018 徐州赛区网络预赛 I. Characters with Hash

    Mur loves hash algorithm, and he sometimes encrypt another one's name, and call him with that encryp ...

  10. Linux学习-Linux 主机上的用户讯息传递

    查询使用者: w, who, last, lastlog 如果你想要知道目前已登入在系统上面的用户呢?可以透过 w 或 who 来查询喔!如下范例所示: [root@study ~]# w 01:49 ...