HDU 4607 Park Visit
来源:互联网 发布:国家电网数据运维岗位 编辑:程序博客网 时间:2024/04/28 19:21
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?
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.
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
14 23 21 24 224
Sample Output
14
Source
2013 Multi-University Training Contest 1
题目大意:
一棵树每条边长度都为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);elseprintf("%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;}
- hdu 4607 Park Visit
- hdu 4607 Park Visit
- hdu - 4607 - Park Visit
- hdu 4607 Park Visit
- hdu-4607-Park Visit
- HDU 4607 Park Visit
- HDU 4607 Park Visit
- HDU 4607 Park Visit
- hdu 4607 Park Visit
- hdu 4607 Park Visit
- hdu 4607 Park Visit
- HDU - 4607 Park Visit
- HDU 4607 Park Visit
- HDU 4607 Park Visit
- hdu 4607 Park Visit dfs
- hdu 4607 Park Visit (DFS)
- hdu 4607 Park Visit 贪心
- HDU 4607 Park Visit 解题报告
- 不自破、必被外破
- Message及其使用
- memcpy带来的灾难
- 什么是java序列化,如何实现java序列化?
- Linux内核的内存屏障
- HDU 4607 Park Visit
- MySQL 分区表 partition线上修改分区字段,后续进一步学习partition (1)
- UVa 400 - Unix ls
- 小心免费wifi的吸费陷阱
- Java 课堂笔记第一节
- PHP相关系列 - PHP日志
- 理解null值和C#中可空类型(NullAble<T>)
- 数据结构 顺序表及合并
- 图文解释XCode常用快捷键的使用