传送门

Park Visit

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

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
 
【题目大意】
在一棵树上 求经过k个点的最短路径。
【思路】
如果是最短路径的话 我们想一直走路径不重复就可以了。
求树的直径,如果要经过k个点的k<r 那么说明这k个点可以在一条线上进行,答案为k-1.
如果不行 肯定要走直径外的分支,不要考虑复杂走哪个分支,反正分支肯定走进去又走出来(继续走直径)
用公式算出来就行 自己推推吧。
我这个代码第二个样例没过,但是算出来就是4啊,但是A了。
【code】
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,k,x,y,sumedge,maxn,maxx,t;
int dad[],dis[],head[];
struct Edge
{
int x,y,nxt;
Edge(int x=,int y=,int nxt=):
x(x),y(y),nxt(nxt){}
}edge[];
void init()
{
sumedge=;
memset(head,,sizeof(head));
memset(dad,,sizeof(dad));
memset(dis,,sizeof(dis));
}
void add(int x,int y)
{
edge[++sumedge]=Edge(x,y,head[x]);
head[x]=sumedge;
}
void dfs(int x)
{
for(int i=head[x];i;i=edge[i].nxt)
{
int v=edge[i].y;
if(dad[x]!=v)
{
dad[v]=x;
dis[v]=dis[x]+;
dfs(v);
}
}
}
int main()
{
scanf("%d",&t);
while(t--)
{
init();
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
{
scanf("%d%d",&x,&y);
add(x,y);add(y,x);
}
dfs();
maxx=-0x7ffff;
for(int i=;i<=n;i++)
{
if(dis[i]>maxx)
{
maxx=dis[i];
maxn=i;
}
}
memset(dis,,sizeof(dis));
memset(dad,,sizeof(dad));
dfs(maxn);
maxx=-0x7ffff;
for(int i=;i<=n;i++)
{
if(dis[i]>maxx)
{
maxx=dis[i];
}
}
while(m--)
{
scanf("%d",&k);
if(k<=maxx)
printf("%d\n",k-);
else
printf("%d\n",maxx+(k-maxx-)*);
}
}
return ;
}
 

Park Visit(树的直径)的更多相关文章

  1. HDU4607 - Park Visit(树的直径)

    题目大意 给定一颗树,要求走过其中连续的k个点,使得步数最少 题解 每条边要么经过两次,要么一次,因为我们的目标就是使得走一次的边尽量的多,这样就转换成求树的直径了,求树的直径我用的是两次dfs,先随 ...

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

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

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

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

  4. hdu4607Park Visit 树的直径

    //给一棵双向树,数中边的权值为1,问对于这颗树中走k个节点的最短路径 //假设k小于这颗数的直径加1,那么走k个节点就没有反复的路,假设大于 //那么大于的节点都须要走两遍 #include< ...

  5. hdu 4607 Park Visit 求树的直径

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

  6. hdu4607 Park Visit(树的直径)

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

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

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

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

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

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

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

随机推荐

  1. VMware View 要求操作句柄的状态错误

    win10系统安装的VMware-viewclient,版本是

  2. disruptor 核心链路应用场景

    核心链路一般比较复杂并且需要考虑:服务之间相互依赖性.流程能够走完.人员的变动等情况 要考虑:兜底.补偿. 常见解决方案是:1)完全解耦 2)模板模式 其他解决方案:1)有限状态机框架:spring- ...

  3. 实验十二 swing图形界面设计

    1.源程序 package information;import java.awt.Container;import java.awt.FlowLayout;import java.awt.event ...

  4. vSphere 6.5支持512e,NVMe SSD呢?

    原创 2017-01-12 朱朋博 金笑雨 企事录 2016年底,VMware终于宣布,从vSphere 6.5开始支持512e扇区格式了. 这当然是好事.不过,不黑不舒服斯基说:原来以前的版本连51 ...

  5. C++ OCX控件开发后出现的注册问题

    error MSB3075: 命令“regsvr32 /s /c "F:\JOBS\项目\格网数据的动态三维可视化\Dev\GridDynamicDisplay\gdiplusplot\GD ...

  6. 深入GCD(五):资源竞争

    概述我将分四步来带大家研究研究程序的并发计算.第一步是基本的串行程序,然后使用GCD把它并行计算化.如果你想顺着步骤来尝试这些程序的话,可以下载源码.注意,别运行imagegcd2.m,这是个反面教材 ...

  7. open-falcon的插件机制

    Plugin可以看做是对agent功能的扩充.对于业务系统的监控指标采集,最好不要做成plugin,而是把采集脚本放到业务程序发布包中,随着业务代码上线而上线,随着业务代码升级而升级,这样会比较容易管 ...

  8. [Unit Testing] Fundamentals of Testing in Javascript

    In this lesson, we’ll get the most fundamental understanding of what an automated test is in JavaScr ...

  9. Linux的SOCKET编程详解(转)

    Linux的SOCKET编程详解 1. 网络中进程之间如何通信 进 程通信的概念最初来源于单机系统.由于每个进程都在自己的地址范围内运行,为保证两个相互通信的进 程之间既互不干扰又协调一致工作,操作系 ...

  10. spring MVC使用Interceptor做用户登录判断

    在任何一个项目中,我们必须要用到的就是用户登录,那么就少不了用户是否登录的判断,如果我们每一个请求都要去做一次判断,那么就会变得很麻烦,但我们复制粘贴的时候我们就要考虑我们的代码写的是不是有问题,是不 ...