求树的直径和中心(ZOJ3820)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5374

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
题意:给出n个城市和n-1条道路,要选两个城市作为消防站,问建造在那两个城市可以使距离消防站最远的城市的距离最小?

分析:先求出树的直径,然后在直径中间把边切开,成为两个新的树,分别求出两个新树的重心和直径:

#include"string.h"
#include"stdio.h"
#include"iostream"
#include"algorithm"
#include"queue"
#include"stack"
#include"vector"
#include"stdlib.h"
#include"map"
#include"string"
#include"math.h"
#define M 400009
#define inf 10000000
#define INF 0x3f3f3f3f
using namespace std;
struct node
{
    int u,v,next;
}edge[M];
int t,head[M];
void init()
{
    t=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
    edge[t].u=u;
    edge[t].v=v;
    edge[t].next=head[u];
    head[u]=t++;
}
int dis[M],pre[M],n,use[M],a[M],pref[M],a1[M],a2[M],maxi;
int bfs(int u,int fuck)
{
    maxi=0;
    int i;
    queue<int>q;
    for(i=1;i<=n;i++)
        use[i]=0;
    dis[u]=0;
    use[u]=1;
    q.push(u);
    int ID=u;
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        for(i=head[u];~i;i=edge[i].next)
        {
            if(i==(fuck^1)||i==fuck)continue;
            int v=edge[i].v;
            if(!use[v])
            {
                use[v]=1;
                dis[v]=dis[u]+1;
                pre[v]=u;
                pref[v]=i;
                if(maxi<dis[v])
                {
                    maxi=dis[v];
                    ID=v;
                }
                q.push(v);
            }
        }
    }
    return ID;
}
void bary()
{
    int i;
    int u=bfs(1,-2);
    pre[u]=-1;
    int v=bfs(u,-2);
    int cnt=0;
    a[++cnt]=v;
    for(i=pre[v];i!=-1;i=pre[i])
        a[++cnt]=i;
    int I=pref[a[cnt/2]];

    int u1=bfs(a[cnt/2],I);
    pre[u1]=-1;
    int v1=bfs(u1,I);
    int cnt1=0;
    a1[++cnt1]=v1;
    for(i=pre[v1];i!=-1;i=pre[i])
        a1[++cnt1]=i;

    int u2=bfs(a[cnt/2+1],I);
    pre[u2]=-1;
    int v2=bfs(u2,I);
    int cnt2=0;
    a2[++cnt2]=v2;
    for(i=pre[v2];i!=-1;i=pre[i])
        a2[++cnt2]=i;

    printf("%d %d %d\n",max(cnt1/2,cnt2/2),a1[(cnt1+1)/2],a2[(1+cnt2)/2]);
}
int main()
{
    int T,i;
    cin>>T;
    while(T--)
    {
        scanf("%d",&n);
        init();
        for(i=1;i<n;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            add(a,b);
            add(b,a);
        }
        bary();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值