程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> zoj3820 Building Fire Stations 樹的直徑+二分

zoj3820 Building Fire Stations 樹的直徑+二分

編輯:C++入門知識

zoj3820 Building Fire Stations 樹的直徑+二分


 

 

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

題意:給一顆樹,選兩個點作為消防隊,使得其他點到這兩個點的最大距離最小,每個點選最近的消防隊。

題解:如果是一個點肯定是樹直徑的重點(重心),兩個點的話不難想到肯定也是在直徑上!這個其實反證法很好想。

具體證明可以見出題人的嚴格證明:http://blog.renren.com/blog/240107793/937020122

所以先bfs找出直徑,然後我們二分答案,找距離直徑端點等於mid的兩個點再bfs。

據說可以找出直徑中點分出兩個子樹再分別找兩個子樹直徑中心就是答案,,這樣寫了幾發一直沒過,改成二分就過了233.。

代碼:

 

/**
 * @author neko01
 */
//#pragma comment(linker, /STACK:102400000,102400000)
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define clr(a) memset(a,0,sizeof a)
#define clr1(a) memset(a,-1,sizeof a)
#define dbg(a) printf(%d
,a)
typedef pair pp;
const double eps=1e-8;
const double pi=acos(-1.0);
const int N=200005;
vectorg[N];
int dist[N];
int dist1[N];
int pre[N];
vectorans;
int n;
void bfs(int s,int &x,int &l)   
{
    clr1(dist);
    clr1(pre);
    dist[s]=0;
    x=s,l=0;
    queueq;
    q.push(s);
    int u,v;
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        for(int i=0;il)
                {
                    l=dist[v];
                    x=v;
                }
            }
        }
    }
}
void bfs1(int s,int dist[])
{
    dist[s]=0;
    queueq;
    q.push(s);
    int u,v;
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        for(int i=0;imid)
            return false;
    }
    return true;
}
int main()
{
    int t;
    scanf(%d,&t);
    while(t--)
    {
        int u,v;
        scanf(%d,&n);
        for(int i=1;i<=n;i++)
        {
            g[i].clear();
        }
        ans.clear();
        for(int i=1;i>1;
            if(check(mid,ans1,ans2))
            {
                res=mid;
                r=mid-1;
            }
            else l=mid+1;
        }
        check(res,ans1,ans2);
        if(ans1==ans2) ans2=ans2%n+1;
        printf(%d %d %d
,res,ans1,ans2);
    }
    return 0;
}


 

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved