http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5374
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.
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).
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.
2 4 1 2 1 3 1 4 5 1 2 2 3 3 4 4 5
1 1 2 1 2 4题意:给出n个城市和n-1条道路,要选两个城市作为消防站,问建造在那两个城市可以使距离消防站最远的城市的距离最小?
分析:先求出树的直径,然后在直径中间把边切开,成为两个新的树,分别求出两个新树的重心和直径:
#include"string.h" #include"stdio.h" #include"iostream" #include"algorithm" #include"queue" #include"stack" #include"vector" #include"stdlib.h" #include"map" #include"string" #include"math.h" #define M 400009 #define inf 10000000 #define INF 0x3f3f3f3f using namespace std; struct node { int u,v,next; }edge[M]; int t,head[M]; void init() { t=0; memset(head,-1,sizeof(head)); } void add(int u,int v) { edge[t].u=u; edge[t].v=v; edge[t].next=head[u]; head[u]=t++; } int dis[M],pre[M],n,use[M],a[M],pref[M],a1[M],a2[M],maxi; int bfs(int u,int fuck) { maxi=0; int i; queue<int>q; for(i=1;i<=n;i++) use[i]=0; dis[u]=0; use[u]=1; q.push(u); int ID=u; while(!q.empty()) { u=q.front(); q.pop(); for(i=head[u];~i;i=edge[i].next) { if(i==(fuck^1)||i==fuck)continue; int v=edge[i].v; if(!use[v]) { use[v]=1; dis[v]=dis[u]+1; pre[v]=u; pref[v]=i; if(maxi<dis[v]) { maxi=dis[v]; ID=v; } q.push(v); } } } return ID; } void bary() { int i; int u=bfs(1,-2); pre[u]=-1; int v=bfs(u,-2); int cnt=0; a[++cnt]=v; for(i=pre[v];i!=-1;i=pre[i]) a[++cnt]=i; int I=pref[a[cnt/2]]; int u1=bfs(a[cnt/2],I); pre[u1]=-1; int v1=bfs(u1,I); int cnt1=0; a1[++cnt1]=v1; for(i=pre[v1];i!=-1;i=pre[i]) a1[++cnt1]=i; int u2=bfs(a[cnt/2+1],I); pre[u2]=-1; int v2=bfs(u2,I); int cnt2=0; a2[++cnt2]=v2; for(i=pre[v2];i!=-1;i=pre[i]) a2[++cnt2]=i; printf("%d %d %d\n",max(cnt1/2,cnt2/2),a1[(cnt1+1)/2],a2[(1+cnt2)/2]); } int main() { int T,i; cin>>T; while(T--) { scanf("%d",&n); init(); for(i=1;i<n;i++) { int a,b; scanf("%d%d",&a,&b); add(a,b); add(b,a); } bary(); } return 0; }