zoukankan      html  css  js  c++  java
  • ZOJ 3820 Building Fire Stations

    Building Fire Stations

    Time Limit: 5000ms
    Memory Limit: 131072KB
    This problem will be judged on ZJU. Original ID: 3820
    64-bit integer IO format: %lld      Java class name: Main
    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
    
     

    Source

    Author

    YU, Xiaoyao; ZHUANG, Junyuan
     
    解题:树的直径+二分
      1 #include <bits/stdc++.h>
      2 using namespace std;
      3 const int maxn = 200010;
      4 struct arc{
      5     int to,next;
      6     arc(int x = 0,int y = -1){
      7         to = x;
      8         next = y;
      9     }
     10 }e[maxn<<1];
     11 int head[maxn],d[maxn],p[maxn],dT[maxn],dS[maxn],tot,n,S,T;
     12 bool vis[maxn];
     13 void add(int u,int v){
     14     e[tot] = arc(v,head[u]);
     15     head[u] = tot++;
     16 }
     17 queue<int>q;
     18 int bfs(int u){
     19     while(!q.empty()) q.pop();
     20     memset(d,-1,sizeof d);
     21     memset(p,-1,sizeof p);
     22     d[u] = 0;
     23     int ret = u;
     24     q.push(u);
     25     while(!q.empty()){
     26         u = q.front();
     27         q.pop();
     28         if(d[ret] < d[u]) ret = u;
     29         for(int i = head[u]; ~i; i = e[i].next){
     30             if(d[e[i].to] == -1){
     31                 d[e[i].to] = d[u] + 1;
     32                 p[e[i].to] = u;
     33                 q.push(e[i].to);
     34             }
     35         }
     36     }
     37     return ret;
     38 }
     39 int depth[maxn];
     40 int bfs2(int u){
     41     while(!q.empty()) q.pop();
     42     q.push(u);
     43     int ret = depth[u] = 0;
     44     while(!q.empty()){
     45         u = q.front();
     46         q.pop();
     47         ret = max(ret,depth[u]);
     48         for(int i = head[u]; ~i; i = e[i].next){
     49             if(vis[e[i].to]) continue;
     50             depth[e[i].to] = depth[u] + 1;
     51             q.push(e[i].to);
     52             vis[e[i].to] = true;
     53         }
     54     }
     55     return ret;
     56 }
     57 bool check(int x){
     58     int u = dT[x],v = dS[x],tmp = d[T] - (x<<1),cnt = 0;
     59     while(u != v){
     60         if(depth[u] + min(cnt,tmp - cnt) > x) return false;
     61         u = p[u];
     62         ++cnt;
     63     }
     64     return true;
     65 }
     66 int main(){
     67     int kase,u,v;
     68     scanf("%d",&kase);
     69     while(kase--){
     70         scanf("%d",&n);
     71         memset(head,-1,sizeof head);
     72         memset(vis,false,sizeof vis);
     73         int cnt = tot = 0;
     74         for(int i = 1; i < n; ++i){
     75             scanf("%d%d",&u,&v);
     76             add(u,v);
     77             add(v,u);
     78         }
     79         u = T = bfs(S = bfs(1));
     80         while(~u){
     81             dT[cnt] = u;
     82             dS[d[T] - cnt++] = u;
     83             vis[u] = true;
     84             u = p[u];
     85         }
     86         u = T;
     87         while(~u){
     88             depth[u] = bfs2(u);
     89             u = p[u];
     90         }
     91         int low = 0,high = d[T]>>1,ret = 0;
     92         while(low <= high){
     93             int mid = (low + high)>>1;
     94             if(check(mid)){
     95                 ret = mid;
     96                 u = dS[mid];
     97                 v = dT[mid];
     98                 if(u == v) u = p[u];
     99                 high = mid - 1;
    100             }else low = mid + 1;
    101         }
    102         printf("%d %d %d
    ",ret,u,v);
    103     }
    104     return 0;
    105 }
    View Code
  • 相关阅读:
    springboot中如何获取配置文件的值
    自动获取当月天数
    控制输入前端时间格式及其它常见需求
    页面输出格式常用注解----@JsonIgnore,@JsonFormat,@JsonFormat
    echart动态生成标题
    echart柱状图中每个柱子超过阈值改变颜色状态
    如何把箭头放置在文本前面,并根据数据的正负显示向上和向下箭头
    02、GPIO初始化
    Linux文件目录的权限
    Linux执行命令./command与直接输入命令的区别
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4852865.html
Copyright © 2011-2022 走看看