HDU 4607 Park Visit

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607



                                                       Park Visit

                                                                                                             Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                                                                                                        Total Submission(s): 2728    Accepted Submission(s): 1234


Problem Description
Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The park is beautiful - but large, indeed. N feature spots in the park are connected by exactly (N-1) undirected paths, and Claire is too tired to visit all of them. After consideration, she decides to visit only K spots among them. She takes out a map of the park, and luckily, finds that there're entrances at each feature spot! Claire wants to choose an entrance, and find a way of visit to minimize the distance she has to walk. For convenience, we can assume the length of all paths are 1.
Claire is too tired. Can you help her?
 

Input
An integer T(T≤20) will exist in the first line of input, indicating the number of test cases.
Each test case begins with two integers N and M(1≤N,M≤10 5), which respectively denotes the number of nodes and queries.
The following (N-1) lines, each with a pair of integers (u,v), describe the tree edges.
The following M lines, each with an integer K(1≤K≤N), describe the queries.
The nodes are labeled from 1 to N.
 

Output
For each query, output the minimum walking distance, one per line.
 

Sample Input
  
  
1 4 2 3 2 1 2 4 2 2 4
 

Sample Output
  
  
1 4

 题意:

      有n个点,n-1条边,两点之间权边为1,问访问k个点至少需要走多少路


思路:微笑bfs来两发

  对于任意一点,离它最远的点u肯定是最长链的一个端点,然后以u端点找出离它最远的点v即为最长链的另一端点。

我们不难发现,走最长链是问题的最优方案,因为在最长链里,每多访问一个节点只需要走一条边,  而其他几点都需要经过两次。

我们只要讨论k的情况,设最长链节点数为ans,则

k<=ans,  路径长度为k-1

k>ans, 路径长度为 ans-1+(k-ans)*2;



代码如下:

#include <cstdio>
#include <iostream>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=100050;
int t,n,m,ans,last,vis[N];
vector<int>s[N]; //二维数组,相当于s[N][N] (不等价!) 
void bfs(int pos,int len)
{
	vis[pos]=1;
	if(len>ans)
	{
		ans=len,last=pos;  //ans记录最长链的节点数,last为端点  
	}
	int last=-1;
	for(int i=0;i<s[pos].size();i++) //在以pos为源点的树图里搜索最远端点  
	{
		if(!vis[s[pos][i]])
		    bfs(s[pos][i],len+1);
	}
} 

int main()
{
	int i,a,b,k;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		for(i=1;i<=n;i++)
		   s[i].clear();  //数组初始化 (清空) 
        for(i=1;i<n;i++)
        {
        	scanf("%d%d",&a,&b);
        	s[a].push_back(b);  //向尾部添加元素b   
        	s[b].push_back(a); //向尾部添加元素a 
        }
        ans=0;
        memset(vis,0,sizeof(vis));
        bfs(1,1); //找出最长链的一端u 
        ans=0;
        memset(vis,0,sizeof(vis));
        bfs(last,1);     //找出最长链的另一端 v 
        while(m--)
        {
        	scanf("%d",&k);  
        	if(k<=ans) printf("%d\n",k-1);
		else printf("%d\n",ans-1+(k-ans)*2); 
        }
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值