zoj3820 Building Fire Stations 树的直径+二分

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3820

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 <cstdio>
#include <cstring>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
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\n",a)
typedef pair<int,int> pp;
const double eps=1e-8;
const double pi=acos(-1.0);
const int N=200005;
vector<int>g[N];
int dist[N];
int dist1[N];
int pre[N];
vector<int>ans;
int n;
void bfs(int s,int &x,int &l)   
{
    clr1(dist);
    clr1(pre);
    dist[s]=0;
    x=s,l=0;
    queue<int>q;
    q.push(s);
    int u,v;
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        for(int i=0;i<g[u].size();i++)
        {
            int v=g[u][i];
            if(dist[v]==-1)
            {
                dist[v]=dist[u]+1;
                q.push(v);
                pre[v]=u;
                if(dist[v]>l)
                {
                    l=dist[v];
                    x=v;
                }
            }
        }
    }
}
void bfs1(int s,int dist[])
{
    dist[s]=0;
    queue<int>q;
    q.push(s);
    int u,v;
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        for(int i=0;i<g[u].size();i++)
        {
            int v=g[u][i];
            if(dist[v]==-1)
            {
                dist[v]=dist[u]+1;
                q.push(v);
            }
        }
    }
}
bool check(int mid,int &ans1,int &ans2)
{
    int sz=ans.size();
    ans1=ans[mid];
    ans2=ans[sz-mid-1];
    clr1(dist);
    bfs1(ans1,dist);
    clr1(dist1);
    bfs1(ans2,dist1);
    for(int i=1;i<=n;i++)
    {
        if(min(dist[i],dist1[i])>mid)
            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<n;i++)
        {
            scanf("%d%d",&u,&v);
            g[u].pb(v);
            g[v].pb(u);
        }
        int x,y,ll;
        bfs(1,x,ll);
        bfs(x,y,ll);
        while(y!=-1)
        {
            ans.pb(y);
            y=pre[y];
        }
        int sz=ans.size();
        int l=0,r=ll/2+1,res,ans1,ans2;
        while(l<=r)
        {
            int mid=(l+r)>>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\n",res,ans1,ans2);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值