设为首页 加入收藏

TOP

ZOJ 3820 Building Fire Stations 二分+BFS
2015-07-20 17:28:54 来源: 作者: 【 】 浏览:4
Tags:ZOJ 3820 Building Fire Stations二分 BFS


二分+BFS:

根据 http://blog.renren.com/blog/240107793/937020122 两个加油站一定在树的直径上。。。

二分长度L , bfs找到直径的两个端点,在距离直径端点L的地方建加油站,然后bfs检查。。。


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

Author: YU, Xiaoyao; ZHUANG, Junyuan
Source: The 2014 ACM-ICPC Asia Mudanjiang Regional Contest



#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        using namespace std; const int maxn=200200; const int INF=0x3f3f3f3f; struct Edge { int to,next; }edge[maxn*2]; int Adj[maxn],Size; void init() { memset(Adj,-1,sizeof(Adj)); Size=0; } void addedge(int u,int v) { edge[Size].to=v; edge[Size].next=Adj[u]; Adj[u]=Size++; } int n; int dist[maxn],pre[maxn]; int bfs(int u) { queue
       
         q; q.push(u); dist[u]=0; pre[u]=0; while(!q.empty()) { u=q.front(); q.pop(); for(int i=Adj[u];~i;i=edge[i].next) { int v=edge[i].to; if(dist[v]>dist[u]+1) { dist[v]=dist[u]+1; pre[v]=u; q.push(v); } } } return u; } bool check(int& s,int& e,int L) { memset(dist,63,sizeof(dist)); s=bfs(1); for(int i=0;i
        
         L) return false; return true; } int nextInt() { int ret=0; bool ok=false; char ch; while(ch=getchar()) { if(ch>='0'&&ch<='9') { ret=ret*10+ch-'0'; ok=true; } else if(ok==true) break; } return ret; } int main() { int T_T; scanf("%d",&T_T); while(T_T--) { init(); scanf("%d",&n); for(int i=0;i
         
          



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇BZOJ 1798: [Ahoi2009]Seq 维护序.. 下一篇Bound Service的三种方式(Binder..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·About - Redis (2025-12-26 08:20:56)
·Redis: A Comprehens (2025-12-26 08:20:53)
·Redis - The Real-ti (2025-12-26 08:20:50)
·Bash 脚本教程——Li (2025-12-26 07:53:35)
·实战篇!Linux shell (2025-12-26 07:53:32)