hdu4607 Park Visit(树的直径)
Park Visit
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1826 Accepted Submission(s): 798
Claire is too tired. Can you help her?
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.
4 2
3 2
1 2
4 2
2
4
4
#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(树的直径)的更多相关文章
- HDU4607 - Park Visit(树的直径)
题目大意 给定一颗树,要求走过其中连续的k个点,使得步数最少 题解 每条边要么经过两次,要么一次,因为我们的目标就是使得走一次的边尽量的多,这样就转换成求树的直径了,求树的直径我用的是两次dfs,先随 ...
- HDU 4607 Park Visit(树的直径)
题目大意:给定一棵树,让求出依次访问k个点的最小花费,每条边的权值都为1. 思路:如果能一直往下走不回来,那么这个路径肯定是最小的,这就取决于给定的k,但是怎么确定这个能一直走的长度呢,其实这个就是树 ...
- [HDU4607]Park Visit(树上最长链)
HDU#4607. Park Visit 题目描述 Claire and her little friend, ykwd, are travelling in Shevchenko's Park! T ...
- HDU4607 Park Visit
肯定会想到树的直径: 如果直径够长,就在直径(1+8)上面找路径,ans=k. 如果不够长,肯定会在有点分叉点(如3,4,5)回溯,然后我们把路径拉直,把其中一条的作为主线(有机化学,ORZ),主线是 ...
- HDU 4607 Park Visit 树的最大直径
题意: 莱克尔和她的朋友到公园玩,公园很大也很漂亮.公园包含n个景点通过n-1条边相连.克莱尔太累了,所以不能去参观所有点景点. 经过深思熟虑,她决定只访问其中的k个景点.她拿出地图发现所有景点的入口 ...
- hdu4607Park Visit 树的直径
//给一棵双向树,数中边的权值为1,问对于这颗树中走k个节点的最短路径 //假设k小于这颗数的直径加1,那么走k个节点就没有反复的路,假设大于 //那么大于的节点都须要走两遍 #include< ...
- HDU-4607 Park Visit bfs | DP | dfs
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 首先考虑找一条最长链长度k,如果m<=k+1,那么答案就是m.如果m>k+1,那么最 ...
- 树形DP求树的直径
hdu4607 Park Visit Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Park Visit(树的直径)
传送门 Park Visit Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- 给flash添加A链接
项目中,曾有一个需求,给flash广告添加链接,跳转到另一个网站.于是直接在html的flash object前面加上<a href="url">,发现链接不起作用. ...
- [转]大数据hadoop集群硬件选择
问题导读 1.哪些情况会遇到io受限制? 2.哪些情况会遇到cpu受限制? 3.如何选择机器配置类型? 4.为数据节点/任务追踪器提供的推荐哪些规格? 随着Apache Hadoop的起步,云客户 ...
- SQL优化注意事项
sql语句优化 性能不理想的系统中除了一部分是因为应用程序的负载确实超过了服务器的实际处理能力外,更多的是因为系统存在大量的SQL语句需要优化. 为了获得稳定的执行性能,SQL语句越简单越好.对复杂的 ...
- c++ 一些随笔
1. A region of source code where any use of the unqualified name (that is, as a plain identifier) re ...
- 主机映射Linux虚拟机硬盘到本地
Windows7上面通过VMware装了一个ubuntu的虚拟机,为了方便在window下直接查看和编辑linux系统下的代码,就想着远程映射硬盘,把Ubuntu的硬盘映射到主机中. 硬盘映射需要Sa ...
- NET映射导致的应用无法访问
应用环境 IIS 数据库 oracle11G 服务器 windwos2003 企业版 周一早上刚到,听到业务部门的人反应,集团的扫码抢系统登陆不上了,没办法进行出入库操作,大批货物在仓库堆积,承运车 ...
- Ejabberd作为推送服务的优化手段
AVOS Cloud目前还在用Ejabberd做Android的消息推送服务.当时选择Ejabberd,是因为Ejabberd是一个发展很长时间的XMPP实现,并且基于Erlang,设想能在我们自主研 ...
- Netbeans导入Nutch1.2
Windows 环境下,Netbeans下导入Nutch1.2. 测试环境: Nutch 1.2 Netbean7.4 Java 1.8.0_20 cygwin 安装步骤: 1.安装: ...
- install cx_Oracle on Linux
step 1 : install oracle client library url: http://www.oracle.com/technetwork/topics/linuxsoft-08280 ...
- C#对DBF文件的操作
protected void Page_Load(object sender, EventArgs e) { System.Data.Odbc.OdbcConnection conn = new Sy ...