Park Visit

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

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

题意就是给你一棵树,然后让你求k个点走一遍最短的路程,因为每个节点都有出口,所以不用再返回来。k个点是随意取的,只要求最短路径就可以。

思路就是先求一下树的直径,假设树的直径走过的节点为x个,如果k比x小,直接ans为k-1,如果大的话,就是树的直径+(k-直径节点数)*2就可以了。

公式我是随便测了几个数据然后猜出来的。。。

写的时候树的直径的板子错了,所以wa了几次。

代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=1e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); int n,m,cnt;//cnt为边数
int dist[maxn],head[maxn];//dist表示最长路,head为存图用的
bool vis[maxn]; struct node{//定义边的结构体
int from,to,val,next;
}edge[maxn<<];//注意是无向图,边数是二倍的 void init()//初始化,不可少
{
cnt=;
memset(head,-,sizeof(head));
} void addedge(int u,int v,int w)
{
edge[cnt].from=u;//起点
edge[cnt].to=v;//终点
edge[cnt].val=w;//权值
edge[cnt].next=head[u];//指向下一条边
head[u]=cnt++;
} int length;//最终的最长路径(树的直径)
int node;//记录端点值 void bfs(int s)
{
queue<int>q;//定义队列
memset(vis,false,sizeof(vis));//初始化,清零
memset(dist,,sizeof(dist));
q.push(s);//入列
vis[s]=true;//记录为遍历过的点
length=;
node=s;
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=head[u];i!=-;i=edge[i].next){//遍历每一条边
int v=edge[i].to;
if(!vis[v]&&dist[v]<dist[u]+edge[i].val){
vis[v]=true;
dist[v]=dist[u]+edge[i].val;//到v的最长路径
if(length<dist[v]){
length=dist[v];//不断更新最长路径
node=v;//更新节点
}
q.push(v);//重新入列,寻找下一个点
}
}
}
} int main()
{
int t;
scanf("%d",&t);
while(t--){
init();
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
int u,v,val;
scanf("%d%d",&u,&v);
val=;//路径权值
addedge(u,v,val);
addedge(v,u,val);
}
bfs();//第一遍找到距离最远的端点
bfs(node);//第二遍找最长距离
int x=length+;//x为树的直径的节点个数
while(m--){
int k;
scanf("%d",&k);
if(k<=x) printf("%d\n",k-);//如果比树的直径短
else{
int ans=length+(k-x)*;
printf("%d\n",ans);
}
}
}
return ;
} /*
1
19 5
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
8 12
12 13
9 14
14 15
15 16
5 10
5 11
10 17
17 18
17 19 7
6 10
9 11
11 12
13 15
19 */

开溜,回寝室洗澡。。。

HDU 4607.Park Visit-树的直径(BFS版)+结论公式(乱推公式)-备忘(加油!)的更多相关文章

  1. HDU 4607 Park Visit(树的直径)

    题目大意:给定一棵树,让求出依次访问k个点的最小花费,每条边的权值都为1. 思路:如果能一直往下走不回来,那么这个路径肯定是最小的,这就取决于给定的k,但是怎么确定这个能一直走的长度呢,其实这个就是树 ...

  2. HDU 4607 Park Visit 树的最大直径

    题意: 莱克尔和她的朋友到公园玩,公园很大也很漂亮.公园包含n个景点通过n-1条边相连.克莱尔太累了,所以不能去参观所有点景点. 经过深思熟虑,她决定只访问其中的k个景点.她拿出地图发现所有景点的入口 ...

  3. hdu 4607 Park Visit

    http://acm.hdu.edu.cn/showproblem.php?pid=4607 先求树的直径 方法:两遍bfs ,任选一点 a  求到a点最远的一点b ,然后 求到b点最远点 c 这样 ...

  4. hdu 4607 Park Visit 求树的直径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n) ...

  5. HDU 4607 Park visit (求树的直径)

    解题思路: 通过两次DFS求树的直径,第一次以随意点作为起点,找到距离该点距离最远的点,则能够证明这个点一定在树的直径上,然后以该点为起点进行DFS得到的最长路就是树的直径. 最后的询问,假设K &l ...

  6. HDU 4607 Park Visit (树的最长链)

    Park Visit Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. 题解报告:hdu 4607 Park Visit(最长链)

    Problem Description Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The par ...

  8. hdu 4607 Park Visit (dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 首先如果k小于等于直径长度,那么答案为k−1.如果k大于直径长度,设直径长度为r,那么答案为r− ...

  9. hdu 4607 树形dp 树的直径

    题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n)个点,至少需要走多少距离(每条边的距离是1): 思路:树形dp求树的直径r: a:若k<=r+1 ...

随机推荐

  1. linux和windows多线程的异同

    linux多线程及线程同步和windows的多线程之间的异同 并不是所有的程序都必须采用多线程,有时候采用多线程性能还不如单线程.采用多线程的好处如下: (1)多线程之间采用相同的地址空间,共享大部分 ...

  2. Java设计模式の单例模式

    -------------------------------------------------- 目录 1.定义 2.常见的集中单例实现 a.饿汉式,线程安全 但效率比较低 b.单例模式的实现:饱 ...

  3. Object.defineProperty 与 属性描述符

    为JavaScript对象新增或者修改属性,有两种不同方式:直接使用=赋值或者使用Object.defineProperty 定义,使用后者的话还可以设置属性的描述符. Object.definePr ...

  4. c# 设置和取消文件夹共享及执行Dos命令

    /// <summary> /// 设置文件夹共享 /// </summary> /// <param name="FolderPath">文件 ...

  5. .NET RabbitMQ

    在企业应用系统领域,会面对不同系统之间的通信.集成与整合,尤其当面临异构系统时,这 种分布式的调用与通信变得越发重要.其次,系统中一般会有很多对实时性要求不高的但是执行起来比较较耗时的地方,比如发送短 ...

  6. 【BZOJ】1229 [USACO2008 Nov]toy 玩具

    [算法]三分+贪心 [题解] 数据范围小的版本:餐巾计划 这题不是使用最小费用流的下凸函数,因为这题是要满足最大流,那么这题到底在三分什么? 三分的这个函数,其实是总费用随卖出玩具量变化而变化的函数, ...

  7. 消息队列之 ActiveMQ(山东数漫江湖)

    简介 ActiveMQ 特点 ActiveMQ 是由 Apache 出品的一款开源消息中间件,旨在为应用程序提供高效.可扩展.稳定.安全的企业级消息通信. 它的设计目标是提供标准的.面向消息的.多语言 ...

  8. 4.0docker部署

    设置容器的端口映射 -P  :容器暴露的所有端口映射 -p :指定映射容器暴露的端口 Nginx部暑流程 docker run -p 80 --name web -t -i ubuntu /bin/b ...

  9. Python模块学习 - click

    Click模块 click模块是Flask的作者开发的一个第三方模块,用于快速创建命令行.它的作用与Python标准库的argparse相同,但是,使用起来更简单. click是一个第三方库,因此使用 ...

  10. Django rest framework + Vue简单示例

    构建vue项目参考这篇文章https://segmentfault.com/a/1190000008049815 一.创建Vue项目 修改源:npm config set registry https ...