2014牡丹江区域赛B(树的直径)ZOJ3820

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

题意:给了一棵树,要求选两个点作为 消防队,使得其它点到这两个 消防队的最大距离尽量小,每个点只会选择离它近的 消防队

思路:首先求树的直径,然后在从直径的中点将树分成两颗子树

            然后两颗子树分别求一遍直径即可,两颗子树的直径中点就是答案

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         #include 
        
          using namespace std; const int MAXN = 200010; int head[MAXN]; int edge[MAXN<<1]; int next[MAXN<<1]; int esz; int n; void addedge(int u,int v) { edge[esz]=v; next[esz]=head[u]; head[u]=esz++; } void init() { memset(head,-1,sizeof(head)); esz=0; } int q[MAXN]; int vis[MAXN]; int fa[MAXN]; int bfs(int s,int flag) { int rear=0,front=0; int e=s; memset(vis,0,sizeof(vis)); vis[flag]=1; q[rear++]=s; vis[s]=1; fa[s]=-1; int ans=1; while(front 
         
           ans){ e=v; ans=vis[v]; } q[rear++]=v; } } return e; } void solve(int s,int e,int &center1,int &center2,int &Ans) { int vex1=bfs(s,e); int vex2=bfs(vex1,e); int stk[MAXN]; int top=0; while(vex2!=-1){ stk[top++]=vex2; vex2=fa[vex2]; } center1=stk[top/2]; Ans=0; Ans=max(Ans,top/2); vex1=bfs(e,s); vex2=bfs(vex1,s); top=0; while(vex2!=-1){ stk[top++]=vex2; vex2=fa[vex2]; } center2=stk[top/2]; Ans=max(Ans,top/2); } int main() { int t; scanf("%d",&t); while(t--){ scanf("%d",&n); init(); for(int i=1;i 
          
            center2)swap(center1,center2); printf("%d %d %d\n",Ans,center1,center2); } return 0; } 
           
          
         
       
      
      
     
     
    
    
   
   
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值