zoj 3820 Building Fire Stations 树的中心

Building Fire Stations

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

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

Description

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

HINT

题意

给你一棵树,然后找俩点,使得其他点到这俩点的最短距离最长边最小

题解:

我们是找的直径,然后按着直径剪开,然后变成了两棵树,然后取拆出来的两棵树的中心就好了

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>

using namespace std;

const int N=200010;
int ans1,ans2,l,n,T;
int pre[N*2],to[N*2],nxt[N*2],cnt,d[N],s1,s2,s3,s4,s5,s6,ss,tt,ans,fa[N];

void makeedge(int x,int y)
{
    to[cnt]=y;nxt[cnt]=pre[x];pre[x]=cnt++;
    to[cnt]=x;nxt[cnt]=pre[y];pre[y]=cnt++;
}

int bfs1(int s,int no)
{
    int z=s;
    queue<int> q;
    while(!q.empty()) q.pop();
    for(int i=0;i<=n;i++) d[i]=0;
    q.push(s);
    d[s]=1;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        for(int p=pre[x];p!=-1;p=nxt[p])
        {
            int y=to[p];
            if(d[y]||y==no) continue;
            d[y]=d[x]+1;
            z=y;
            fa[y]=x;
            q.push(y);
        }
    }
    return z;
}
int dfs(int x,int no,int dep)
{
    if(d[x]==dep)
    {
        if(no==0)tt=fa[x];
        return x;
    }
    return dfs(fa[x],no,dep);
}

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        memset(pre,-1,sizeof(pre));
        cnt=0;
        scanf("%d",&n);
        for(int i=1;i<n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            makeedge(x,y);
        }
        s1=bfs1(1,0);
        s2=bfs1(s1,0);
        ss=dfs(s2,0,(d[s2]+2)/2);
        s3=bfs1(ss,tt);
        s4=bfs1(s3,tt);
        ans1=dfs(s4,tt,(d[s4]+1)/2);
        ans=max(d[ans1]-1,d[s4]-d[ans1]);
        s5=bfs1(tt,ss);
        s6=bfs1(s5,ss);
        ans2=dfs(s6,ss,(d[s6]+1)/2);
        ans=max(ans,max(d[ans2]-1,d[s6]-d[ans2]));
        printf("%d %d %d\n",ans,ans1,ans2);
    }
}
时间: 2024-11-03 21:03:35

zoj 3820 Building Fire Stations 树的中心的相关文章

ZOJ 3820:Building Fire Stations(树的直径 Grade C)

题意: n个点的树,边长全为1,求找出两个点,使得树上离这两个点距离最远的那个点,到这两个点(中某个点就行)的距离最小. 思路: 求树直径,找中点,删除中间那条边(如果直径上点数为奇数,则删任何一侧都可),分成两个子树,再求中心,即为答案. 代码: //14:12 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define N 200100 struct G

zoj 3820 Building Fire Stations(二分法+bfs)

题目链接:zoj 3820 Building Fire Stations 题目大意:给定一棵树.选取两个建立加油站,问说全部点距离加油站距离的最大值的最小值是多少,而且随意输出一种建立加油站的方式. 解题思路:二分距离推断,推断函数的复杂度是o(n),这种复杂度应该是o(nlogn).即使常数系数偏大,可是竟然跑了4.5s,也是醉了. 推断函数里面做了3次bfs,可是每次bfs节点最多遍历1次,首先任取一点做根建立一个树,找到深度最大的节点.以向上移动L(推断的ans)位置处的节点建立加油站,并

zoj 3820 Building Fire Stations(二分+bfs)

题目链接:zoj 3820 Building Fire Stations 题目大意:给定一棵树,选取两个建立加油站,问说所有点距离加油站距离的最大值的最小值是多少,并且任意输出一种建立加油站的方式. 解题思路:二分距离判断,判断函数的复杂度是o(n),这样的复杂度应该是o(nlogn),即使常数系数偏大,但是居然跑了4.5s,也是醉了. 判断函数里面做了3次bfs,但是每次bfs节点最多遍历1次,首先任取一点做根建立一个树,找到深度最大的节点,以向上移动L(判断的ans)位置处的节点建立加油站,

zoj 3820 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 road

ZOJ 3820 Building Fire Stations

Building Fire Stations Time Limit: 5000ms Memory Limit: 131072KB This problem will be judged on ZJU. Original ID: 382064-bit integer IO format: %lld      Java class name: Main Special Judge Marjar University is a beautiful and peaceful place. There a

zoj3820 Building Fire Stations 树的中心

题意:n个点的树,给出n-1条边,每条边长都是1,两个点建立防火站,使得其他点到防火站的最远距离最短. 思路:比赛的时候和队友一开始想是把这两个点拎起来,使得层数最少,有点像是树的中心,于是就猜测是将树的中心找到后,将两棵左右子树分别求树的中心,这两棵树的中心就是答案,but另外一个队友又说了个反例,脑子也不清醒,以为还有没考虑到的,比赛也没A,赛后一想就是最初的猜想,回来之后写了写,报栈了,数据范围太大,真不想改,今天改了改,改成bfs又tle了,囧囧的,把memset和memcpy都改成循环

ZOJ 3820 Building Fire Stations 贪心+树的直径

不知道为什么是对的,但是举不出反例,比赛的时候队友提出找树的直径,不过我没敢写,真是可惜. 具体做法就是先找到原树的直径,然后删去最中间的一条边,变为两个子树,分别球两颗子树的直径中点,便是答案了. #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <stack> #include <map> #include <

ZOJ 3820 Building Fire Stations 求中点+树的直径+BFS

题意:给一棵树,要求找出两个点,使得所有点到这两个点中距离与自己较近的一个点的距离的最大值(所有点的结果取最大的值,即最远距离)最小. 意思应该都能明白. 解法:考虑将这棵树摆直如下: 那么我们可以把最中间的那条直径边删掉,然后在分成的两颗子树内求一个直径中心点,那么这两个点就可以作为答案. 反正当时就觉得这样是正确的, 但是不能证明. 于是,几个bfs就可以搞定了. 当时写TLE了,原因是存要删的边我用了map<pair<int,int>,int>, 后来改掉就不T了,map这种

zoj 3820 Building Fire Stations(树上乱搞)

做同步赛的时候想偏了,状态总是时好时坏.这状态去区域赛果断得GG了. 题目大意:给一棵树.让求出树上两个点,使得别的点到两个点较近的点的距离最大值最小. 赛后用O(n)的算法搞了搞,事实上这道题不算难.逗逼的没A.. 事实上这两个点一定是树直径上的两个点,假设能想到这个就非常好搞了,仅仅须要求出树的直径,然后从中间位置把树拆成两颗子树,然后分别求出子树的中心就好了. 证明例如以下,假如我们已经求出树上的两个点u, v满足条件.那么,我们从u, v中间把树拆开肯定是最优的.这个非常easy想到.