树形DP求树的直径
Park Visit
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2274 Accepted Submission(s): 1005
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?
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.
1
4 2
3 2
1 2
4 2
2
4
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求树的直径的更多相关文章
- HDU 4514 - 湫湫系列故事——设计风景线 - [并查集判无向图环][树形DP求树的直径]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...
- 浅谈关于树形dp求树的直径问题
在一个有n个节点,n-1条无向边的无向图中,求图中最远两个节点的距离,那么将这个图看做一棵无根树,要求的即是树的直径. 求树的直径主要有两种方法:树形dp和两次bfs/dfs,因为我太菜了不会写后者这 ...
- 树形dp - 求树的直径
随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好. 现在已经勘探确定了n个位置可 ...
- 树形DP 学习笔记(树形DP、树的直径、树的重心)
前言:寒假讲过树形DP,这次再复习一下. -------------- 基本的树形DP 实现形式 树形DP的主要实现形式是$dfs$.这是因为树的特殊结构决定的——只有确定了儿子,才能决定父亲.划分阶 ...
- hdu2196 树形dp经典|树的直径
/* 两种做法 1.求出树直径v1,v2,那么有一个性质:任取一点u,树上到u距离最远的点必定是v1或v2 那么可以一次dfs求树v1 第二次求dis1[],求出所有点到v1的距离,同时求出v2 第三 ...
- codeforce 337D Book of Evil ----树形DP&bfs&树的直径
比较经典的老题 题目意思:给你一颗节点数为n的树,然后其中m个特殊点,再给你一个值d,问你在树中有多少个点到这m个点的距离都不大于d. 这题的写法有点像树的直径求法,先随便选择一个点(姑且设为点1)来 ...
- CS academy Growing Trees【模板】DP求树的直径
[题意概述] 给出一棵树,树上的边有两个值a和b,你可以在[0,limit]范围内选择一个整数delta,树上的边的权值为a+b*delta,现在问当delta为多少的时候树的直径最小.最小直径是多少 ...
- hdoj2196(树形dp,树的直径)
题目链接:https://vjudge.net/problem/HDU-2196 题意:给出一棵树,求每个结点可以到达的最远距离. 思路: 如果求得是树上最长距离,两次bfs就行.但这里求的是所有点的 ...
- 【NOI P模拟赛】最短路(树形DP,树的直径)
题面 给定一棵 n n n 个结点的无根树,每条边的边权均为 1 1 1 . 树上标记有 m m m 个互不相同的关键点,小 A \tt A A 会在这 m m m 个点中等概率随机地选择 k k k ...
随机推荐
- SQL错误代码弹出提示信息类
截获SQL错误代码弹出提示信息类 Code:public class DBErrorCode{ /// <summary> /// 根据错误代码弹出错误提示 /// &l ...
- ggplot2——简介
ggplot2是R语言最为强大的作图软件包,强于其自成一派的数据可视化理念.当熟悉了ggplot2的基本套路后,数据可视化工作将变得非常轻松而有条理. 本文主要对ggplot2的可视化理念及开发套路做 ...
- Error -27740: WSA_IO_pending
Error -27740: WSA_IO_pendingAction.c(198): Error -27791: Server **** has shut down the connection pr ...
- 关于Cocos2d-x中图集中图片的调用
1.首先在Resources文件夹下要有打包好的图集文件和相同名称的plist文件. shoot.png shoot.plist shoot_background.png shoot_backgrou ...
- struts2-Action配置-通配符-DMI
1. ActionMethod: Action执行的时候并不一定要执行execute方法,有两种替换办法如下: ①在配置文件中配置action的时候用“method”属性来指定执行哪个方法 ②在url ...
- getaddrinfo ENOTFOUND https://api.weixin.qq.com https://api.weixin.qq.com:443
原因:这是由于你当前的主机不能够连接到你填写的url. 解决方法: 先ping api.weixin.qq.com ping不通的话,就是外网访问的问题. 开通外网访问就可以了.
- ‘,’之后要留空格,如 Function(x, y, z)
‘,’之后要留空格,如 Function(x, y, z).如果‘;’不是一行的结束 符号,其后要留空格,如 for (initialization; condition; update). #inc ...
- MVC生成图片验证码,可指定位数
前台: <h2>mvc后台生成验证码,可指定位数</h2> <img id="gc" src="GetValidateCode" ...
- Java调用FTP实例
package com.test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStre ...
- 修改查看MYSQL字符集(charset)
From: http://www.cnblogs.com/fengqingtao/archive/2010/11/23/1885220.html 查看mysql的字符集 mysql> show ...