HDU 6233 X-men

题目链接

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 cities connected by roads. There is a path between any two city. The length of each road is one kilometer. The cities are numbered from to . There are X-men in this kingdom. The -th X-man is in the city numbered . 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.

Input
The first line is the number of test cases.

For each test case, the first line contains two positive numbers . The second line contains numbers .

The following lines describe the roads. Each line contains two integers , denoting there is a road between city and city .

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.00
Hint
In 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}.

题意:一棵树上有m个人,每个人在一个节点上,然后每小时走到一个邻居节点上,问最少多长时间他们的距离都不超过1。

题解:最短时间取决于2个点最远的距离。树上最远两个人的距离/2就是答案(向下取整)
因为n只有1000,所以可以n²暴力。

代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<stack>
#include<vector>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
vector<int> G[1100];
int vis[1100];
int t,n,m;
int ans;
void Search(int x,int y,int len)
{
  if(vis[x]) ans=max(len,ans);
  int l=G[x].size();
  for(int i=0;i<l;i++)
  {
    int v=G[x][i];
    if(v==y) continue;
    Search(v,x,len+1);
  }
}
int main()
{
  scanf("%d",&t);
  while(t--)
  {
    int x,y;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
      G[i].clear();
    }
    memset(vis,0,sizeof(vis));
    for(int i=0;i<m;i++)
    {
      scanf("%d",&x);
      vis[x]=1;
    }
    for(int i=1;i<n;i++)
    {
      scanf("%d%d",&x,&y);
      G[x].push_back(y);
      G[y].push_back(x);
    }
    ans=0;
    for(int i=1;i<=n;i++)
    {
      if(vis[i])
      {
        Search(i,0,0);
      }
    }
    printf("%d.00\n",ans/2);
  }
  return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值