hdu4607

Park Visit

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2274 Accepted Submission(s): 1005

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

程序:



#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#define M 100009
#define inf 99999999
struct st
{
int u,v,w,next;
}edge[M*3];
int head[M],use[M],t,dis[M][3],in[M];
void init()
{
t=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
edge[t].u=u;
edge[t].v=v;
edge[t].w=w;
edge[t].next=head[u];
head[u]=t++;
}
void dfs(int u)
{
use[u]=1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!use[v])
{
dfs(v);
//更新最大值和次大值
if(dis[u][0]<dis[v][0]+edge[i].w)
{
int tt=dis[u][0];
dis[u][0]=dis[v][0]+edge[i].w;
dis[u][1]=tt;
}
else if(dis[u][1]<dis[v][0]+edge[i].w)
dis[u][1]=dis[v][0]+edge[i].w;
}
}
if(in[u]==1&&u!=1)//注意
dis[u][0]=dis[u][1]=0;
}
int main()
{
int T,n,m,i;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d%d",&n,&m);
memset(in,0,sizeof(in));
for(i=1;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b,1);
add(b,a,1);
in[a]++;
in[b]++;
}
memset(use,0,sizeof(use));
memset(dis,0,sizeof(dis));
dfs(1);
int ans=-1;//记录直径
for(i=1;i<=n;i++)
{
if(ans<dis[i][0]+dis[i][1])
ans=dis[i][0]+dis[i][1];
}
while(m--)
{
int s;
scanf("%d",&s);
if(s<=ans+1)
printf("%d\n",s-1);
else
printf("%d\n",ans+(s-ans-1)*2);
}
}
}

树形DP求树的直径的更多相关文章

  1. HDU 4514 - 湫湫系列故事——设计风景线 - [并查集判无向图环][树形DP求树的直径]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...

  2. 浅谈关于树形dp求树的直径问题

    在一个有n个节点,n-1条无向边的无向图中,求图中最远两个节点的距离,那么将这个图看做一棵无根树,要求的即是树的直径. 求树的直径主要有两种方法:树形dp和两次bfs/dfs,因为我太菜了不会写后者这 ...

  3. 树形dp - 求树的直径

    随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好. 现在已经勘探确定了n个位置可 ...

  4. 树形DP 学习笔记(树形DP、树的直径、树的重心)

    前言:寒假讲过树形DP,这次再复习一下. -------------- 基本的树形DP 实现形式 树形DP的主要实现形式是$dfs$.这是因为树的特殊结构决定的——只有确定了儿子,才能决定父亲.划分阶 ...

  5. hdu2196 树形dp经典|树的直径

    /* 两种做法 1.求出树直径v1,v2,那么有一个性质:任取一点u,树上到u距离最远的点必定是v1或v2 那么可以一次dfs求树v1 第二次求dis1[],求出所有点到v1的距离,同时求出v2 第三 ...

  6. codeforce 337D Book of Evil ----树形DP&bfs&树的直径

    比较经典的老题 题目意思:给你一颗节点数为n的树,然后其中m个特殊点,再给你一个值d,问你在树中有多少个点到这m个点的距离都不大于d. 这题的写法有点像树的直径求法,先随便选择一个点(姑且设为点1)来 ...

  7. CS academy Growing Trees【模板】DP求树的直径

    [题意概述] 给出一棵树,树上的边有两个值a和b,你可以在[0,limit]范围内选择一个整数delta,树上的边的权值为a+b*delta,现在问当delta为多少的时候树的直径最小.最小直径是多少 ...

  8. hdoj2196(树形dp,树的直径)

    题目链接:https://vjudge.net/problem/HDU-2196 题意:给出一棵树,求每个结点可以到达的最远距离. 思路: 如果求得是树上最长距离,两次bfs就行.但这里求的是所有点的 ...

  9. 【NOI P模拟赛】最短路(树形DP,树的直径)

    题面 给定一棵 n n n 个结点的无根树,每条边的边权均为 1 1 1 . 树上标记有 m m m 个互不相同的关键点,小 A \tt A A 会在这 m m m 个点中等概率随机地选择 k k k ...

随机推荐

  1. Jquery实现仿腾讯微薄的广播发表

    前言: 由于这几天在学习Jquery的一些知识,比以前的感觉就是Jquery太强大了,很多很简单的功能以前在JavaScript要写几十行的代码而在Jquery中只用几行代码就搞定了,所以我决定好好学 ...

  2. Windoows窗口程序四

    子窗口的创建 .创建时要设置父窗口句柄 .创建风格要增加WS_CHILD|WS_VISIBLE HWND CreateChild(LPSTR lpClassName,LPSTR lpWndName,H ...

  3. C# HttpRequest基础连接已经关闭: 接收时发生意外错误

    在c#中使用HttpWebRequest时,频繁请求一个网址时,过段时间就会出现“基础连接已经关闭: 接收时发生意外错误”的错误提示. 将webRequest的属性设置成下面的,经测试可以解决. we ...

  4. 【JavaScript】使用setInterval()函数作简单的轮询操作

    轮询(Polling)是一种CPU决策怎样提供周边设备服务的方式,又称"程控输出入"(Programmed I/O). 轮询法的概念是.由CPU定时发出询问.依序询问每个周边设备是 ...

  5. enumerate遍历列表

    enumerate 函数用于遍历序列中的元素以及它们的下标: >>> for i,j in enumerate(('a','b','c')):  print i,j 0 a 1 b ...

  6. docker和kubernetes docker的区别

    之前公司的测试环境,刚开始自己搭建虚拟机,然后安装redis,nginx,mq,mysql,tomcat,jdk,marven,还有jekins.前面些还算好点,jekins还是比较麻烦的.然后搭完以 ...

  7. [转]anchorPoint 锚点解析

    转自:http://blog.csdn.net/cjopengler/article/details/7045638 anchor point 究竟是怎么回事? 之所以造成不容易理解的是因为我们平时看 ...

  8. ThinkPHP之文件上传

    在项目其中.我们有的时候需要上传图片的功能.简单的从面相过程的方法是相对较为复杂的,要一步一步的来.假设用框架的话,相对就简单了很多,主要就是方法以及每个变量所代表的意义,然后就是一些注意的地方了. ...

  9. Unity文件操作路径

    Unity3D中的资源路径: Application.dataPath:此属性用于返回程序的数据文件所在文件夹的路径.例如在Editor中就是Assets了. Application.streamin ...

  10. win7 64位下android开发环境的搭建

    本文转自:http://www.cfanz.cn/index.php?c=article&a=read&id=65289 最近换了新电脑,装了win7 64位系统,安装了各种开发环境, ...