传送门

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. Linux命令之ss

    1.ss -s 显示socket的统计信息 2.ss -a显示socket的详细信息 (ta:tcp,ua:udp) 3.ss -l显示本机监听的端口 4.ss -pl 显示本机监听的端口和程序 ht ...

  2. BZOJ——1610: [Usaco2008 Feb]Line连线游戏

    http://www.lydsy.com/JudgeOnline/problem.php?id=1610 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 2 ...

  3. 解决Linux系统没有/etc/sysconfig/iptables文件

    Linux系统中,防火墙默认是不开启的,一般也没有配置过任何防火墙的策略,所以不存在/etc/sysconfig/iptables文件. 一.常规解决方法: 1.在控制台使用iptables命令随便写 ...

  4. android CheckBox使用和状态获得

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...

  5. AppCompatActivity

    刚开始看HelloWorld的目录结构然后就发现Android Studio中的是 import android support.v7.app.AppcompatActivity; public cl ...

  6. keras常见问题解答

    https://keras.io/zh/getting-started/faq/ https://keras.io/zh/ https://github.com/keras-team/keras/tr ...

  7. [WASM] Access WebAssembly Memory Directly from JavaScript

    While JavaScript has a garbage-collected heap, WebAssembly has a linear memory space. Nevertheless u ...

  8. 微信小程序 项目实战(二)board 首页

    1.项目结构 2.页面 (1)数据(逻辑) board.js // pages/board/board.js Page({ /** * 页面的初始数据 */ data: { imgWrap: [] } ...

  9. node 爬虫 --- 批量下载图片

    步骤一:创建项目 npm init 步骤二:安装 request,cheerio,async 三个模块 request 用于请求地址和快速下载图片流. https://github.com/reque ...

  10. NDK编译STL

    方法: 1.在jni目录下新建Application.mk; 加入 APP_STL :=  stlport_static  右边的值还可以换成下面几个: system - 使用默认最小的C++运行库, ...