现在的位置: 首页 > 综合 > 正文

HDU 4607 Park Visit

2018年01月15日 ⁄ 综合 ⁄ 共 3047字 ⁄ 字号 评论关闭

Park Visit

                                                Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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≤105), 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
 

题目大意:

一棵树
每条边长度都为1,问要到达k个点的最短路径(起点可以从k个顶点中任意一个出发);
 
算法分析:
首先如果k小于等于直径长度,那么答案为k−1;
如果k大于直径长度,设直径长度为d,那么答案为d−1+(k−d)*2;
 

树的直径:树上的最长简单路径;

 直径的求法:

可以任意选择一个点开始进行bfs或者dfs;

从而找到离该点最远的那个点;

再从找到的点出发,找到据该点的最远点,那么这两点就确定了树的一条直径,两点间距即为所求距离;

可以证明,离树上任意一点最远的点一定是树的某条直径的两端点之一;

BFS的代码:

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>

using namespace std;

const int maxn=110000;

struct Node{
	int x;
	int d;
};

int flag,Max;
bool vis[maxn];
vector<int> son[maxn];
queue<Node> Que;

void BFS(int x)
{
	int i;
	Node tmp,a;

	while(!Que.empty())
	{
		Que.pop();
	}
	
	tmp.x=x;	tmp.d=1;
	Que.push(tmp);
	memset(vis,0,sizeof(vis));
	vis[x]=1;	flag=x;	Max=1;

	while(!Que.empty())
	{
		tmp=Que.front();	Que.pop();
		if(tmp.d>Max)
		{
			Max=tmp.d;
			flag=tmp.x;
		}
		for(i=0;i<son[tmp.x].size();i++)
		{
			if(!vis[son[tmp.x][i]])
			{
				vis[son[tmp.x][i]]=1;
				a.x=son[tmp.x][i];
				a.d=tmp.d+1;
				Que.push(a);
			}
		}
	}
}

int main()
{
	int T,N,M,K;
	int u,v,i;

	scanf("%d",&T);

	while(T--)
	{
		scanf("%d%d",&N,&M);
		for(i=1;i<=N;i++)
			son[i].clear();
		for(i=1;i<N;i++)
		{
			scanf("%d%d",&u,&v);
			son[u].push_back(v);
			son[v].push_back(u);
		}

		BFS(1);
//		printf("max=%d   flag=%d\n",Max,flag);
		BFS(flag);
//		printf("max=%d   flag=%d\n",Max,flag);

		while(M--)
		{
			scanf("%d",&K);
			if(K<=Max)
				printf("%d\n",K-1);
			else
				printf("%d\n",Max-1+(K-Max)*2);
		}
	}
	return 0;
}

DFS的代码:

#include<cstdio>
#include<cstring>
#include<vector>

using namespace std;

const int maxn=110000;

int flag,Max;
bool vis[maxn];
vector<int> son[maxn];

void DFS(int x,int d)
{
    if(d>Max)
    {
        Max=d;
        flag=x;
    }
    for(int i=0;i<son[x].size();i++)
    {
        if(!vis[son[x][i]])
        {
            vis[son[x][i]]=true;
            DFS(son[x][i],d+1);
        }
    }
}

int main()
{
    int T,N,M,K;
    int u,v,i;

    scanf("%d",&T);

    while(T--)
    {
        scanf("%d%d",&N,&M);
		
		for(i=1;i<=N;i++)
			son[i].clear();
        for(i=1;i<N;i++)
        {
            scanf("%d%d",&u,&v);
            son[u].push_back(v);
            son[v].push_back(u);
        }

        memset(vis,0,sizeof(vis));
        vis[1]=true;    Max=1;    flag=1;
        DFS(1,1);
//        printf("max=%d   flag=%d\n",Max,flag);

        memset(vis,0,sizeof(vis));
        vis[flag]=1;    Max=1;
        DFS(flag,1);
//        printf("max=%d\n",Max);

        while(M--)
        {
            scanf("%d",&K);
            if(K<=Max)
                printf("%d\n",K-1);
            else
                printf("%d\n",Max-1+(K-Max)*2);
        }
    }

    return 0;
}

抱歉!评论已关闭.