<hdoj4607>Park Visit
2016-08-02 22:07
190 查看
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
Sample Output
给出一个连通的集合 每个点之间的距离为1 计算走过m个点需要走多远的距离
有两种情况 假设该集合最长链长度为k;
1. k>=m-1; 那么直接在最长链上走就可以 d=m-1
2. k<m-1 除了最长链还需要在分支上找点 这样到达分支上的每个点 还要返回到最长链 就需要多走该点到最长链的距离
那么d=(m-1-k)*2+k
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
给出一个连通的集合 每个点之间的距离为1 计算走过m个点需要走多远的距离
有两种情况 假设该集合最长链长度为k;
1. k>=m-1; 那么直接在最长链上走就可以 d=m-1
2. k<m-1 除了最长链还需要在分支上找点 这样到达分支上的每个点 还要返回到最长链 就需要多走该点到最长链的距离
那么d=(m-1-k)*2+k
#include<cstdio> #include<queue> #include<cstring> using namespace std; struct node { int from,to,val,next; }; node dian[1000001]; int cut; int head[100001]; void chushihua() { cut=0; memset(head,-1,sizeof(head)); } void Edge(int u,int v,int w) { dian[cut].from =u; dian[cut].to =v; dian[cut].val =w; dian[cut].next =head[u]; head[u]=cut++; } int jilu; int vis[100001]; int dist[100001]; int ans; int n,m; void bfs(int s) { memset(vis,0,sizeof(vis)); memset(dist,0,sizeof(dist)); ans=0; queue<int>q; q.push(s);vis[s]=1;dist[s]=0; while(!q.empty() ) { int u=q.front() ; q.pop() ; for(int i=head[u];i!=-1;i=dian[i].next ) { int v=dian[i].to ; if(!vis[v]) { if(dist[v]<dist[u]+dian[i].val ) dist[v]=dist[u]+dian[i].val ; vis[v]=1; q.push(v); } } } for(int i=1;i<=n;i++) { if(ans<dist[i]) { ans=dist[i]; jilu=i; } } } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); chushihua(); int a,b,c; for(int i=1;i<n;i++) { scanf("%d%d%",&a,&b); Edge(a,b,1); Edge(b,a,1); } bfs(1); bfs(jilu); while(m--) { scanf("%d",&c); if(c<=ans+1) { printf("%d\n",c-1); } else { printf("%d\n",(c-ans-1)*2+ans); } } } return 0; }
相关文章推荐
- hdoj 1205 吃糖果 <抽屉原理>
- 人见人爱A^B<>hdoj2035
- Work<hdoj5326>
- HDOJ2063过山车<<二分图>>
- hdoj 2955Robberies<背包>
- hdoj 2586 <LCA转RMQ算法--学习之路>
- 【HDOJ】 <Problem - 1000> : A+B Problem
- The MAX<hdoj2803>
- the Sum of Cube<hdoj5053>
- 【HDOJ】 <Problem - 1001> : Sum Problem
- HDOJ3743<分治>
- HDOJ 1407 测试你是否和LTC水平一样高(P.S : 这么暴力不好吧~~~(>_<)~~~ )
- 求余<hdoj1212>
- 开门人和关门人<hdoj1234>
- hdoj 1695 GCD <用容斥求区间素数对>
- hdoj2045不容易系列之(3)—— LELE的RPG难题<递推>
- KK's Steel<hdoj5620>菲波那切数列的应用
- HDOJ 1319 Prime Cuts<数论>
- <hdoj2647>Reward
- 大数A+B<hdoj1002>