栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 3820 Building Fire Stations

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 3820 Building Fire Stations

#include <map>#include <queue>#include <math.h>#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;#define N 500010int head[N];int edgeNum;struct Edge{    int v,to;}edge[N];int edge_u_v[N][2];void addedge(int a,int b){    edge[edgeNum].v = b;    edge[edgeNum].to = head[a];    head[a] = edgeNum ++;}int vis[N],depth,ind;int t_vis[N],father[N];int stack[N];void bfs(int u){    vis[u] = 1;    queue<int>que;    que.push(u);    while(!que.empty()){        int tmp = que.front();        que.pop();        if(vis[tmp] >= depth){ depth = vis[tmp]; ind = tmp;        }        for(int i = head[tmp];i != -1;i = edge[i].to){ int v = edge[i].v; if(vis[v] == 0){     vis[v] = vis[tmp] + 1;     father[v] = tmp;     que.push(v); }        }    }    int tmp = ind;    for(int i = 0;i < depth;i ++){        stack[i] = tmp;        tmp = father[tmp];    }    return ;}void init(int n){    edgeNum = 0;    for(int i = 0;i <= n;i ++) {        head[i] = -1;        vis[i] = 0;        father[i] = -1;    }}int main(){    int t;    int n;    int u,v;    scanf("%d",&t);    while(t --){        scanf("%d",&n);        init(n);        for(int i = 0;i < n - 1;i ++){ scanf("%d%d",&u,&v); edge_u_v[i][0] = u; edge_u_v[i][1] = v; addedge(u,v); addedge(v,u);        }        depth = 1;        bfs(1);        for(int i = 0;i <= n;i ++) vis[i] = 0;        bfs(ind);        int ans_one,ans_two;        int depth_one,depth_two;        init(n);        u = stack[depth / 2 - 1];        v = stack[depth / 2];        for(int i = 0;i < n - 1;i ++){ if((edge_u_v[i][0] == u && edge_u_v[i][1] == v) || (edge_u_v[i][1] == u && edge_u_v[i][0] == v)) ; else{     addedge(edge_u_v[i][0],edge_u_v[i][1]);     addedge(edge_u_v[i][1],edge_u_v[i][0]); }        }        int seperation = stack[depth / 2];        int seperation1 = stack[(depth - 1) / 2];        int long_depth = depth;        depth = 1;        bfs(seperation);        for(int i = 0;i <= n;i ++) t_vis[i] = vis[i];        for(int i = 0;i <= n;i ++) vis[i] = 0;        bfs(ind);        ans_one = stack[depth/2];        depth_one = depth;        if(long_depth&1){ addedge(u,v); addedge(v,u); t_vis[seperation] = 0;        }else{ seperation = seperation1;        }        depth = 1;        for(int i = 0;i <= n;i ++) vis[i] = t_vis[i];        bfs(seperation);        for(int i = 0;i <= n;i ++) vis[i] = t_vis[i];        bfs(ind);        if(depth & 1) ans_two = stack[depth/2];        else ans_two = stack[(depth-1)/2];        depth_two = depth;        if(ans_one == ans_two){ printf("%d %d %dn",long_depth/2,ans_one,(ans_one == 1)?2:1);        }else{ printf("%d %d %dn",max(depth_one/2,depth_two/2),ans_one,ans_two);        }    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/380593.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号