HDU:4607 Park Visit(树的最大直径)

7 篇文章 0 订阅

Park Visit

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


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
 

Source
 

Recommend
liuyiding
题目大意:给你一个N个点和N-1边组成的图(图连通 且 无环),每条边距离为1。问你在图中经过k个点需要行走的最短距离。
解题思路:
先求出S-T路径长度ans,分类讨论(我们已经知道这条S-T路径上有ans+1个点)

一:k <= ans + 1,直接在这条路径上任意选起点,最短距离为k - 1;

二:k > ans+1,说明我们即使走完ans + 1个点也不够。但是我们已经知道这条路径是树的直径,因此它的S-T两个端点度数都为1,这就说明端点只和S-T路径上的点有直接边相连。

针对第二种情况,为了走够k个点,我们只能选择S-T路径之外的点来走。但由于图是连通图且无环,S-T路径之外的点一定会与S-T路径上的点有直接边连接。给个图解释

          

S —— 2 —— 3 —— 4 ——T
                      |          |
                      5         6

如图,若题目要求走够6个点,我们在走完S-T这条路径之后,才仅仅走了5个点。没办法,剩余一个点只能走路径之外的点,如图我们可以知道如果要走就要走一个来回(如 3 - > 4 - >3),最后要回到S-T路径上,继续走S-T路径。这样最少需要走(k - ans - 1) * 2 + ans。
代码如下:
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct node
{
	int from,to,last,val;
}bian[200010];
int head[100010];
int vis[100010];
int dis[100010];
int n,m;
int num;
int start;
int ans;
void add(int s,int e,int power)
{
	bian[num].from=s;
	bian[num].to=e;
	bian[num].val=power;
	bian[num].last=head[s];
	head[s]=num;
	num++;
}
void dfs(int x)
{
	int r=x;
	queue<int>q;
	q.push(r);
	vis[r]=1;
	dis[r]=0;
	ans=0;
	while(!q.empty())
	{
		int tmp=q.front();
		q.pop();
		for(int i=head[tmp];i!=-1;i=bian[i].last)
		{
			int tmp2=bian[i].to;
			if(vis[tmp2]==0&&dis[tmp2]<dis[tmp]+bian[i].val)
			{
				dis[tmp2]=dis[tmp]+bian[i].val;
				vis[tmp2]=1;
				if(dis[tmp2]>ans)
				{
					ans=dis[tmp2];
					start=tmp2;
				}
				q.push(tmp2);
			}
		}
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		num=0;
		memset(head,-1,sizeof(head));
		scanf("%d%d",&n,&m);
		for(int i=1;i<n;i++)
		{
			int x,y;
			scanf("%d%d",&x,&y);
			add(x,y,1);
			add(y,x,1);
		}
		memset(vis,0,sizeof(vis));
		memset(dis,0,sizeof(dis));
		dfs(1);
		memset(vis,0,sizeof(vis));
		memset(dis,0,sizeof(dis));
		dfs(start);
		int size=ans;
		for(int i=0;i<m;i++)
		{
			int k;
			scanf("%d",&k);
			if(k<=size+1)
			{
				printf("%d\n",k-1);
			}
			else
			{
				printf("%d\n",(k-size-1)*2+size);
			}
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值