HDOJ 4607 Park Visit

Park Visit

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


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
 

这题还要分析一个规律出来= =、做个题需要的综合素养怎么这么高啊,然后我看题的时候居然又看错了(OTZ日常读错英语系列),当成两个景点之间的距离是他们两个的编号差值了(应该是两个直接连接的景点的距离是1)……(坑死自己啊


题意:一个人去旅游,第一行T,后面跟着T组数据。第二行n,m。n表示景点数目,m表示后面询问几次。后面n-1行表示景点之间有路,每条路的距离都是1。最后跟m次询问,每一次输入一个数k,表示他想去k处景点。然后输出要去k处景点最少需要走多远。


题解:主要是k,k如果不大于树的直径L的话,那么距离就是k-1(只走一次,直接走最小距离),如果大于的话,有一个式子L+2*(k-L-1)。具体不推了……网上应该有人贴推的过程=。=……


#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
#define N 200000+10

int dis[N],vis[N],head[N];
struct a
{
	int t,w,next;
}edge[N*2];
int edgenum,n,m,Tnode,L;
void addedge(int u,int v,int w)
{
	edge[edgenum].t=v;
	edge[edgenum].w=w;
	edge[edgenum].next=head[u];
	head[u]=edgenum++;
}
void bfs(int s)
{
	memset(dis,0,sizeof(dis));
	memset(vis,0,sizeof(vis));
	int i,u,v;
	queue<int> q;
	vis[s]=1;dis[s]=0;L=0;
	q.push(s);
	while(!q.empty())
	{
		u=q.front();
		q.pop();
		for(i=head[u];i!=-1;i=edge[i].next)
		{
			v=edge[i].t;
			if(!vis[v])
			{
				if(dis[v]<dis[u]+edge[i].w)
				{
					dis[v]=dis[u]+edge[i].w;
					if(L<dis[v])
					{
						L=dis[v];
						Tnode=v;
					}
				}
				vis[v]=1;
				q.push(v);
			}
		}
	}
}
int main()
{
	int T;
	int i,a,b,x;
	scanf("%d",&T);
	while(T--)
	{
		memset(head,-1,sizeof(head));
		edgenum=0;
		scanf("%d%d",&n,&m);
		for(i=1;i<n;i++)
		{
			scanf("%d%d",&a,&b);
			addedge(a,b,1);
			addedge(b,a,1);
		}
		bfs(1);
		bfs(Tnode);
		while(m--)
		{
			scanf("%d",&x);
			if(L+1>=x)
				printf("%d\n",x-1);
			else
				printf("%d\n",L+(x-L-1)*2);
		}
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值