Park Visit

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

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
 
Recommend
liuyiding
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = ;
int n,m;
int head[N];
struct node {
int v,w,next;
}edge[N<<];
int cnt;
void add(int u, int v, int w) {
edge[cnt].v = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
}
int vis[N];
int dist[N];
int que[N];
int ret;
int bfs(int u) {
memset(vis, , sizeof(vis));
int start = ;
int rear = ;
que[] = u;
vis[u] = ;
dist[u] = ;
int i;
int ans = ;
while (start < rear) {
start++;
int tmp = que[start];
for (i = head[tmp]; i != -; i = edge[i].next) {
int v = edge[i].v;
if (!vis[v]) {
rear++;
que[rear] = v;
vis[v] = ;
dist[v] = dist[tmp] + edge[i].w;
if (dist[v] > ans) {
ans = dist[v];
ret = v;
}
}
}
}
return ans;
}
int main() {
// freopen("in.txt","r",stdin);
int i;
int u,v;
int T;
scanf("%d",&T);
while (T--) {
scanf("%d %d",&n,&m);
memset(head, -, sizeof(head));
cnt = ;
for (i = ; i < n; i++) {
scanf("%d %d",&u,&v);
add(u,v,);
add(v,u,);
}
bfs();
int ans = bfs(ret);
for (i = ; i < m; i++) {
int t;
scanf("%d",&t);
if (t <= ans + )
printf("%d\n", t - );
else
printf("%d\n", ans + (t - ans - ) * );
}
}
return ;
}

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

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

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

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

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

  3. [HDU4607]Park Visit(树上最长链)

    HDU#4607. Park Visit 题目描述 Claire and her little friend, ykwd, are travelling in Shevchenko's Park! T ...

  4. HDU4607 Park Visit

    肯定会想到树的直径: 如果直径够长,就在直径(1+8)上面找路径,ans=k. 如果不够长,肯定会在有点分叉点(如3,4,5)回溯,然后我们把路径拉直,把其中一条的作为主线(有机化学,ORZ),主线是 ...

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

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

  6. hdu4607Park Visit 树的直径

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

  7. HDU-4607 Park Visit bfs | DP | dfs

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 首先考虑找一条最长链长度k,如果m<=k+1,那么答案就是m.如果m>k+1,那么最 ...

  8. 树形DP求树的直径

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

  9. Park Visit(树的直径)

    传送门 Park Visit Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. 如何使用Chrome Timeline 工具(译)

    [原文地址]https://developers.google.com/web/tools/chrome-devtools/profile/evaluate-performance/timeline- ...

  2. Codeforces Round #381 (Div. 2)B. Alyona and flowers(水题)

    B. Alyona and flowers Problem Description: Let's define a subarray as a segment of consecutive flowe ...

  3. oracle like模糊查询

    简单的方式,使用"%" 和 "_" 字符匹配操作可以使用通配符 "%" 和 "_": %:表示任意个字符,包括零个: _ ...

  4. CGI

    CGI的工作原理一般是这么定义的: 客户端web浏览器浏览某个主页后,利用一定的方式提交数据,并通过HTTP协议向Web服务器发出请求,服务器端的HTTP Daemon(守护进程)将描述的主页信息通过 ...

  5. Integration Services创建ETL包

    http://www.cnblogs.com/chiniao/archive/2009/12/23/1630595.html  (转载) Microsoft Integration Services ...

  6. css 画基本图形

    抄于http://dongtianee.sinaapp.com/demo9.html /******************************************************** ...

  7. BCP

    转:(总结) SQL Server Bulk Insert 批量数据导入 DB SQL ServerBulk InsertBCPOPENROWSET格式文件 SQL Server的Bulk Inser ...

  8. android下拉框

    XML: <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:androi ...

  9. xampp 配置虚拟主机

    1.安装好xampp后 2.找到安装目录 apache目录--conf目录--extra目录--httpd-vhosts.conf文件(用记事本或者别的编辑器打开) 文件最后添加代码 <Virt ...

  10. More Effective c++

    指针和引用 引用对象必须存在,即不能引用空值,指针可以指向空值,引用必须初始化指向一个对象 指针可以改变指向的对象,引用不能改变所引用的对象 不改变指向对象使用引用,改变指向对象使用指针 重载[]时必 ...