Zoj problem set-3820 building fire stations [tree diameter + operation]

Source: Internet
Author: User

Tag: blog HTTP Io OS AR for SP data 2014

Title: zoj problem set-3820 building fire stations


A tree with N points and n-1 sides should be given. Then, two fire stations should be established on the two points to minimize the distance from all points to the maximum distance from the fire station.


Analysis: First, find the diameter of the tree, then divide the tree into two trees at the midpoint of the tree's diameter, and then take the maximum value of the midpoint of the two trees respectively as the ANS value.

The data on this question is a bit confusing...

AC code:


#include <cstdio>#include <iostream>#include <algorithm>#include <vector>#include <cstring>#include <queue>using namespace std;const int N = 204000;int n;vector<int> v[N];int vis[N],father[N];vector<int> line;int BFS(int s,int flag){    queue<int> q;    int e=s;    line.clear();    memset(vis,0,sizeof(vis));    memset(father,-1,sizeof(father));    vis[flag]=1;    q.push(s);    vis[s]=1;    int ans=1;    while(!q.empty())    {        int f=q.front();        q.pop();        for(int i=0; i<v[f].size(); i++){            int k=v[f][i];            if(vis[k])                continue;            vis[k]=vis[f]+1;            father[k]=f;            if(vis[k]>ans){                e=k;                ans=vis[k];            }            q.push(k);        }    }    for(int i=e;i!=-1;i=father[i])        line.push_back(i);    return e;}struct Node{    int one,two,ans;};void print()  //输出{    for(int i = 0;i<line.size();i++)    {        printf("x%d ",line[i]);    }    printf("\n");}pair<int,int> Yougth(int s,int t){    int p1 = BFS(s,t);    int p2 = BFS(p1,t);    int len = line.size();    int one = line[len/2];    int tmp = len/2;    pair<int,int> ans(one,tmp);    return ans;}Node Importent(int fir,int sec){    Node pps;    int ans = -1;    pair<int,int> tt = Yougth(fir,sec);    pps.one = tt.first;    ans = max(ans,tt.second);    tt = Yougth(sec,fir);    pps.two = tt.first;    ans = max(ans , tt.second);    pps.ans = ans;    return pps;}void solve(){    Node pps,ans2;    int p1 = BFS(1,n+1);    int p2 = BFS(p1,n+1);    int len = line.size()/2;    int a = line[len-1],b = line[len] , c = line[len+1];    if(line.size()%2==0)    {        pps = Importent(a,b);    }    else    {        pps = Importent(a,b);        ans2 = Importent(b,c);        if(ans2.ans<pps.ans)    //取小的。但是好像没有这样的数据            pps = ans2;    }    printf("%d %d %d\n",pps.ans,pps.one,pps.two);}int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        for(int i=0;i<n-1;i++)        {            int x,y;            scanf("%d%d",&x,&y);            v[x].push_back(y);            v[y].push_back(x);        }        solve();        for(int i=0;i<=n;i++)            v[i].clear();    }    return 0;}


Zoj problem set-3820 building fire stations [tree diameter + operation]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.