• 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 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



    题意:
    给你一颗树。选两个点做消防站,使得全部点到近期消防站的最大距离最小。

    思路:
    能够yy到两个点选在树的直径上,于是能够二分答案,答案为 mid 的时候两个端点就应该选  st+mid   和  ed-mid 这两个点 (st、ed是树的直径的端点),然后枚举中间的那些点是否满足条件来检验就够了。


    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <vector>
    #include <queue>
    #include <algorithm>
    #define maxn 200005
    #define INF 0x3f3f3f3f
    typedef long long ll;
    using namespace std;
    
    int n,m,ans;
    int st,ed,dd,resu,resv;
    vector<int>edge[maxn];
    int dist[maxn],pre[maxn],city[maxn];
    int indst[maxn],inded[maxn];
    bool vis[maxn],app[maxn];
    
    int bfs(int sx)
    {
        int i,j,u,v,res;
        queue<int>q;
        memset(dist,-1,sizeof(dist));
        memset(pre,0,sizeof(pre));
        dist[sx]=dd=0;
        q.push(sx);
        while(!q.empty())
        {
            u=q.front();
            if(dist[u]>=dd)
            {
                res=u; dd=dist[u];
            }
            q.pop();
            for(i=0;i<edge[u].size();i++)
            {
                v=edge[u][i];
                if(dist[v]==-1)
                {
                    pre[v]=u;
                    dist[v]=dist[u]+1;
                    q.push(v);
                }
            }
        }
        return res;
    }
    int bfs1(int sx)
    {
        int i,j,u,v,res=0;
        queue<int>q;
        city[sx]=0;
        q.push(sx);
        while(!q.empty())
        {
            u=q.front();
            res=max(res,city[u]);
            q.pop();
            for(i=0;i<edge[u].size();i++)
            {
                v=edge[u][i];
                if(!vis[v]&&!app[v])
                {
                    app[v]=1;
                    city[v]=city[u]+1;
                    q.push(v);
                }
            }
        }
        return res;
    }
    bool isok(int mid)
    {
        int i,j,u,v,tu=indst[mid],tv=inded[mid],gap=dist[ed]-2*mid;
        //printf("mid:%d tu:%d tv:%d gap:%d
    ",mid,tu,tv,gap);
        u=tv;
        int num=0;
        while(u!=tu)
        {
            if(city[u]+min(num,gap-num)>mid) return false ;
            u=pre[u];
            num++;
        }
        return true ;
    }
    void solve()
    {
        int i,j,u,v,num=0;
        st=bfs(1);
        ed=bfs(st);
       // printf("st:%d ed:%d
    ",st,ed);
        memset(vis,0,sizeof(vis));
        u=ed;
        while(u)
        {
            inded[num]=u; indst[dist[ed]-num]=u;
            num++;
            vis[u]=1;
            u=pre[u];
        }
        memset(app,0,sizeof(app));
        u=ed;
        while(u)
        {
            city[u]=bfs1(u);
            //printf("u:%d city:%d
    ",u,city[u]);
            u=pre[u];
        }
        int le=0,ri=dist[ed]/2,mid;
        while(le<=ri)
        {
            mid=(le+ri)>>1;
            if(isok(mid))
            {
                ans=mid; resu=indst[mid]; resv=inded[mid];
                if(resu==resv) resu=pre[resu];
                ri=mid-1;
            }
            else le=mid+1;
        }
        printf("%d %d %d
    ",ans,resu,resv);
    }
    int main()
    {
        int i,j,t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);
            for(i=1;i<=n;i++) edge[i].clear();
            int u,v;
            for(i=1;i<n;i++)
            {
                scanf("%d%d",&u,&v);
                edge[u].push_back(v);
                edge[v].push_back(u);
            }
            solve();
        }
        return 0;
    }

    1 1 2
    1 2 4

  • 相关阅读:
    SSIS-SqlTask控件使用
    每年有20万人进军IT行业,为何人才缺口依旧这么大?
    推荐!5 本经典著作描绘C++学习路线图!
    C语言有了"链表"还用"数组"干嘛?因为你傻呀,怕你学不会!
    如何优化【if else】的面条代码,我们一起来解决代码复杂度问题!
    为什么要学编程一定要热爱?对不起我并不热爱编程,我只喜欢创造!
    程序员如何应对双十一的大数据冲击!是服务器先崩溃还是程序员先崩溃?
    【C语言项目】这21个入门练手项目,让你对C语言不再说难!
    【学习编程】有0.5倍速和2倍速的学习方法,那你看到过10倍速的学习技巧吗!
    外包程序员入职蚂蚁金服被质疑,网友评论人生污点,真就不拿外包当人了呗?
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7056479.html
Copyright © 2020-2023  润新知