Building Fire Stations


Time Limit: 5 Seconds     
Memory Limit: 131072 KB      Special Judge


Marjar University is a beautiful and peaceful place. There are N buildings and
N - 1 bidirectional roads in the campus. These buildings are connected by roads in such a way that there is exactly one path between any two buildings. By coincidence, the length of each road is 1 unit.

To ensure the campus security, Edward, the headmaster of Marjar University, plans to setup two fire stations in two different buildings so that firefighters are able to arrive at the scene of the fire as soon as possible whenever fires occur. That means
the longest distance between a building and its nearest fire station should be as short as possible.

As a clever and diligent student in Marjar University, you are asked to write a program to complete the plan. Please find out two proper buildings to setup the fire stations.

Input

There are multiple test cases. The first line of input contains an integer
T
indicating the number of test cases. For each test case:

The first line contains an integer N (2 <= N <= 200000).

For the next N - 1 lines, each line contains two integers Xi and
Yi. That means there is a road connecting building Xi and building
Yi (indexes are 1-based).

Output

For each test case, output three integers. The first one is the minimal longest distance between a building and its nearest fire station. The next two integers are the indexes of the two buildings selected to build the fire stations.

If there are multiple solutions, any one will be acceptable.

Sample Input

2
4
1 2
1 3
1 4
5
1 2
2 3
3 4
4 5

Sample Output

1 1 2
1 2 4


题意:
给你一颗树。选两个点做消防站,使得全部点到近期消防站的最大距离最小。

思路:
能够yy到两个点选在树的直径上,于是能够二分答案,答案为 mid 的时候两个端点就应该选  st+mid   和  ed-mid 这两个点 (st、ed是树的直径的端点),然后枚举中间的那些点是否满足条件来检验就够了。


代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define maxn 200005
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std; int n,m,ans;
int st,ed,dd,resu,resv;
vector<int>edge[maxn];
int dist[maxn],pre[maxn],city[maxn];
int indst[maxn],inded[maxn];
bool vis[maxn],app[maxn]; int bfs(int sx)
{
int i,j,u,v,res;
queue<int>q;
memset(dist,-1,sizeof(dist));
memset(pre,0,sizeof(pre));
dist[sx]=dd=0;
q.push(sx);
while(!q.empty())
{
u=q.front();
if(dist[u]>=dd)
{
res=u; dd=dist[u];
}
q.pop();
for(i=0;i<edge[u].size();i++)
{
v=edge[u][i];
if(dist[v]==-1)
{
pre[v]=u;
dist[v]=dist[u]+1;
q.push(v);
}
}
}
return res;
}
int bfs1(int sx)
{
int i,j,u,v,res=0;
queue<int>q;
city[sx]=0;
q.push(sx);
while(!q.empty())
{
u=q.front();
res=max(res,city[u]);
q.pop();
for(i=0;i<edge[u].size();i++)
{
v=edge[u][i];
if(!vis[v]&&!app[v])
{
app[v]=1;
city[v]=city[u]+1;
q.push(v);
}
}
}
return res;
}
bool isok(int mid)
{
int i,j,u,v,tu=indst[mid],tv=inded[mid],gap=dist[ed]-2*mid;
//printf("mid:%d tu:%d tv:%d gap:%d\n",mid,tu,tv,gap);
u=tv;
int num=0;
while(u!=tu)
{
if(city[u]+min(num,gap-num)>mid) return false ;
u=pre[u];
num++;
}
return true ;
}
void solve()
{
int i,j,u,v,num=0;
st=bfs(1);
ed=bfs(st);
// printf("st:%d ed:%d\n",st,ed);
memset(vis,0,sizeof(vis));
u=ed;
while(u)
{
inded[num]=u; indst[dist[ed]-num]=u;
num++;
vis[u]=1;
u=pre[u];
}
memset(app,0,sizeof(app));
u=ed;
while(u)
{
city[u]=bfs1(u);
//printf("u:%d city:%d\n",u,city[u]);
u=pre[u];
}
int le=0,ri=dist[ed]/2,mid;
while(le<=ri)
{
mid=(le+ri)>>1;
if(isok(mid))
{
ans=mid; resu=indst[mid]; resv=inded[mid];
if(resu==resv) resu=pre[resu];
ri=mid-1;
}
else le=mid+1;
}
printf("%d %d %d\n",ans,resu,resv);
}
int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++) edge[i].clear();
int u,v;
for(i=1;i<n;i++)
{
scanf("%d%d",&u,&v);
edge[u].push_back(v);
edge[v].push_back(u);
}
solve();
}
return 0;
}

1 1 2
1 2 4

zoj 3820 Building Fire Stations (二分+树的直径)的更多相关文章

  1. ZOJ Problem Set - 3820 Building Fire Stations 【树的直径 + 操作 】

    题目:problemId=5374" target="_blank">ZOJ Problem Set - 3820 Building Fire Stations 题 ...

  2. zoj 3820 Building Fire Stations(二分法+bfs)

    题目链接:zoj 3820 Building Fire Stations 题目大意:给定一棵树.选取两个建立加油站,问说全部点距离加油站距离的最大值的最小值是多少,而且随意输出一种建立加油站的方式. ...

  3. zoj 3820 Building Fire Stations 树的中心

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

  4. zoj 3820 Building Fire Stations(树上乱搞)

    做同步赛的时候想偏了,状态总是时好时坏.这状态去区域赛果断得GG了. 题目大意:给一棵树.让求出树上两个点,使得别的点到两个点较近的点的距离最大值最小. 赛后用O(n)的算法搞了搞,事实上这道题不算难 ...

  5. ZOJ 3820 Building Fire Stations 求中点+树的直径+BFS

    题意:给一棵树,要求找出两个点,使得所有点到这两个点中距离与自己较近的一个点的距离的最大值(所有点的结果取最大的值,即最远距离)最小. 意思应该都能明白. 解法:考虑将这棵树摆直如下: 那么我们可以把 ...

  6. ZOJ 3820:Building Fire Stations(树的直径 Grade C)

    题意: n个点的树,边长全为1,求找出两个点,使得树上离这两个点距离最远的那个点,到这两个点(中某个点就行)的距离最小. 思路: 求树直径,找中点,删除中间那条边(如果直径上点数为奇数,则删任何一侧都 ...

  7. ZOJ 3820 Building Fire Stations

    题意: 树上找两个点  使得其它点到这两点随意一点的距离的最大值最小 思路: 最大值最小  想到二分  在二分的基础上判定这个最大值是否可能 怎样判定这个问题就是怎样选那两个点的问题  非常明显  我 ...

  8. Building Fire Stations ZOJ - 3820 (二分,树的直径)

    大意: 给定树, 求两个点, 使得所有其他的点到两点的最短距离的最大值尽量小. 二分答案转为判定选两个点, 向外遍历$x$的距离是否能遍历完整棵树. 取直径两段距离$x$的位置bfs即可. #incl ...

  9. zoj3820 Building Fire Stations 树的中心

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

随机推荐

  1. java内存管理之垃圾回收及JVM调优

    GC(garbage Collector 垃圾收集器)作用:a.内存的动态分配:b.垃圾回收注:Java所承诺的自动内存管理主要是针对对象内存的回收和对象内存的分配. 一.垃圾标记 程序计数器.Jav ...

  2. 2015 Multi-University Training Contest 7 hdu 5371 Hotaru's problem

    Hotaru's problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. 洛谷1417 烹调方案 dp 贪心

    洛谷 1417 dp 传送门 挺有趣的一道dp题目,看上去接近于0/1背包,但是考虑到取每个点时间不同会对最后结果产生影响,因此需要进行预处理 对于物品x和物品y,当时间为p时,先加x后加y的收益为 ...

  4. (转)redis源代码分析 – event library

    每个cs程序尤其是高并发的网络服务端程序都有自己的网络异步事件处理库,redis不例外. 事件库仅仅包括ae.c.ae.h,还有3个不同的多路复用(本文仅描述epoll)的wrapper文件,事件库封 ...

  5. BZOJ 2005 [Noi2010]能量採集 (容斥)

    [Noi2010]能量採集 Time Limit: 10 Sec  Memory Limit: 552 MB Submit: 2324  Solved: 1387 [id=2005"> ...

  6. 零基础学HTML 5实战开发(第一季)

    開始学习html5了.趋势不得不学习啊,之前老毛说过落后就要挨打,如今是不学习就要被市场淘汰,被社会淘汰.喜欢挑战,喜欢冒险.来吧.csdn给我们提供了那么好的平台.用起来..零基础学HTML 5的实 ...

  7. 基础数位DP小结

    HDU 3555 Bomb dp[i][0] 表示含 i 位数的方案总和. sp[i][0] 表示对于位数为len 的 num 在区间[ 10^(i-1) , num/(10^(len-i)) ] 内 ...

  8. 2016 ICPC CAMP Recording

    等了好久终于等到今天 马上能和群巨们一起学习了 希望不要暴露我太弱的本质............ 北京不冷,就是风大~~~ 1.24 8点准时起床了,准备下楼吃早饭 (这个宾馆好多美美的空姐对面就是东 ...

  9. java中的输入输出<1>

    java中的输入输出基础(1) java中的IO支持通过java.io包下的类和接口来支持.在java.io包下主要包括输入.输出两种io流,每种输入.输出流又分为字节流和字符流. 字节流就是以字节为 ...

  10. springMVC接受参数总结

    springMVC接受参数分类及使用对应注解才能正确接受到参数,否则报400或者接受的参数值为null: 1.接受单个参数 @RequestParam 不需要转json串 2.接受一个实体 @Requ ...