题目链接:HDU 6233
X-Men
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 121 Accepted Submission(s): 73
Problem Description
There is a kingdom called Dream Kingdom with
N
cities connected by
N−1
roads. There is a path between any two city. The length of each road is one kilometer. The cities are numbered from
1
to
N
. There are
M
X-men in this kingdom. The
i
-th X-man is in the city numbered
ai(1≤ai≤N)
. There can be no or multiple X-men in one city.
Everyone start to walk simultaneously. At the beginning of each hour, one man will choose a adjacent city ("adjacent" means there is a road between two cities) which is on the shortest path to the city where there is a man he can communicate with now. If there are several eligible adjacent cities that can be chosen, the X-man will choose one of them \textbf{randomly}. Each x-man will make the decision and move simultaneously. The speed of X-men is only one kilometer per hour. So they will move to chosen city at the end of each hour. X-men can communicate with the people whose distance to him is \textbf{more than one} kilometer at this time. If there are no X-man he can communicate with now, he will not move in the following hour.
The king of the Dream Kingdom want to arrest X-men. And at the beginning of one hour he could check whether there is any X-man can move in the following hour. If the king knows no X-man can move in the following hour, he will send the army to catch all X-men immediately.
Now the king wants you to help him calculate the expected hours he could arrest the X-men. In other words, you need to calculate the expected hours such that all X-men stop moving.
Everyone start to walk simultaneously. At the beginning of each hour, one man will choose a adjacent city ("adjacent" means there is a road between two cities) which is on the shortest path to the city where there is a man he can communicate with now. If there are several eligible adjacent cities that can be chosen, the X-man will choose one of them \textbf{randomly}. Each x-man will make the decision and move simultaneously. The speed of X-men is only one kilometer per hour. So they will move to chosen city at the end of each hour. X-men can communicate with the people whose distance to him is \textbf{more than one} kilometer at this time. If there are no X-man he can communicate with now, he will not move in the following hour.
The king of the Dream Kingdom want to arrest X-men. And at the beginning of one hour he could check whether there is any X-man can move in the following hour. If the king knows no X-man can move in the following hour, he will send the army to catch all X-men immediately.
Now the king wants you to help him calculate the expected hours he could arrest the X-men. In other words, you need to calculate the expected hours such that all X-men stop moving.
Input
The first line is the number of test cases.
For each test case, the first line contains two positive numbers N(1≤N≤103),M(1≤M≤103) . The second line contains M numbers ai(1≤ai≤N) .
The following N−1 lines describe the roads. Each line contains two integers u,v (1≤u,v≤N) , denoting there is a road between city u and city v .
For each test case, the first line contains two positive numbers N(1≤N≤103),M(1≤M≤103) . The second line contains M numbers ai(1≤ai≤N) .
The following N−1 lines describe the roads. Each line contains two integers u,v (1≤u,v≤N) , denoting there is a road between city u and city v .
Output
For each test case, output one number in one single line representing the answer. You should output your answer rounded to two decimal places.
Sample Input
2 7 3 5 6 7 1 2 1 3 1 4 5 2 6 3 4 7 3 3 1 1 2 1 2 2 3
Sample Output
2.00 0.00HintIn the first example, each X-man only have one adjacent city can be chosen to move in the first hour. They will move from city {5, 6, 7} to city {2, 3, 4} respectively. Each X-man only have one adjacent city can be chosen to move in the second hour, too. They will all move to city {1}. And then all of them can't feel any X-man such that distance between two X-men is more than one unit length. So they will be arrested immediately after two hours from the beginning to now. This is the only situation. So the answer is {2/1 = 2.00}.
题目分析:最短时间取决于2个点最远的距离。所以只需要做类似树的直径的方法即可,第一次bfs找到一端,第二次dfs找到另一端,然后注意的是起始点和终点都是标记的节点。
//
// main.cpp
// HDU 6223 X-Men
//
// Created by teddywang on 2017/12/6.
// Copyright © 2017年 teddywang. All rights reserved.
//
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
vector<int>mp[1111];
int hashs[1111];
int vis[1111];
int n,m,mm;
int ans;
void dfs(int s,int num)
{
int len=mp[s].size();
for(int i=0;i<len;i++)
{
if(!vis[mp[s][i]])
{
vis[mp[s][i]]=1;
if(hashs[mp[s][i]]==1) ans=max(ans,num+1);
dfs(mp[s][i],num+1);
vis[mp[s][i]]=0;
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(hashs,0,sizeof(hashs));
memset(vis,0,sizeof(vis));
scanf("%d%d",&n,&m);
for(int i=0;i<=n;i++)mp[i].clear();
int s1=0,s2=0;
mm=0;
for(int i=0;i<m;i++)
{
int buf;
scanf("%d",&buf);
if(!hashs[buf]) mm++;
hashs[buf]=1;
s1=buf;
}
m=mm;
for(int i=0;i<n-1;i++)
{
int a,b;
scanf("%d%d",&a,&b);
mp[a].push_back(b);
mp[b].push_back(a);
}
if(m==1)
{
printf("0.00\n");
continue;
}
queue<int>q;
queue<int>d;
int buf=1;
q.push(s1);
d.push(0);
vis[s1]=1;
int maxlen=0;
while(!q.empty())
{
int s=q.front();
int num=d.front();
d.pop();
q.pop();
int len=mp[s].size();
for(int i=0;i<len;i++)
{
if(!vis[mp[s][i]]){
q.push(mp[s][i]);
d.push(num+1);
vis[mp[s][i]]=1;
if(hashs[mp[s][i]]==1)
{
buf++;
if(num+1>maxlen)
{
maxlen=num+1;
s2=mp[s][i];
}
if(buf==m)
break;
}
}
}
}
ans=0;
memset(vis,0,sizeof(vis));
vis[s2]=1;
dfs(s2,0);
ans=ans/2;
double ans1=ans;
printf("%.2lf\n",ans1);
}
}