Park Visit

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

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

题意就是给你一棵树,然后让你求k个点走一遍最短的路程,因为每个节点都有出口,所以不用再返回来。k个点是随意取的,只要求最短路径就可以。

思路就是先求一下树的直径,假设树的直径走过的节点为x个,如果k比x小,直接ans为k-1,如果大的话,就是树的直径+(k-直径节点数)*2就可以了。

公式我是随便测了几个数据然后猜出来的。。。

写的时候树的直径的板子错了,所以wa了几次。

代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=1e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); int n,m,cnt;//cnt为边数
int dist[maxn],head[maxn];//dist表示最长路,head为存图用的
bool vis[maxn]; struct node{//定义边的结构体
int from,to,val,next;
}edge[maxn<<];//注意是无向图,边数是二倍的 void init()//初始化,不可少
{
cnt=;
memset(head,-,sizeof(head));
} void addedge(int u,int v,int w)
{
edge[cnt].from=u;//起点
edge[cnt].to=v;//终点
edge[cnt].val=w;//权值
edge[cnt].next=head[u];//指向下一条边
head[u]=cnt++;
} int length;//最终的最长路径(树的直径)
int node;//记录端点值 void bfs(int s)
{
queue<int>q;//定义队列
memset(vis,false,sizeof(vis));//初始化,清零
memset(dist,,sizeof(dist));
q.push(s);//入列
vis[s]=true;//记录为遍历过的点
length=;
node=s;
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=head[u];i!=-;i=edge[i].next){//遍历每一条边
int v=edge[i].to;
if(!vis[v]&&dist[v]<dist[u]+edge[i].val){
vis[v]=true;
dist[v]=dist[u]+edge[i].val;//到v的最长路径
if(length<dist[v]){
length=dist[v];//不断更新最长路径
node=v;//更新节点
}
q.push(v);//重新入列,寻找下一个点
}
}
}
} int main()
{
int t;
scanf("%d",&t);
while(t--){
init();
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
int u,v,val;
scanf("%d%d",&u,&v);
val=;//路径权值
addedge(u,v,val);
addedge(v,u,val);
}
bfs();//第一遍找到距离最远的端点
bfs(node);//第二遍找最长距离
int x=length+;//x为树的直径的节点个数
while(m--){
int k;
scanf("%d",&k);
if(k<=x) printf("%d\n",k-);//如果比树的直径短
else{
int ans=length+(k-x)*;
printf("%d\n",ans);
}
}
}
return ;
} /*
1
19 5
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
8 12
12 13
9 14
14 15
15 16
5 10
5 11
10 17
17 18
17 19 7
6 10
9 11
11 12
13 15
19 */

开溜,回寝室洗澡。。。

HDU 4607.Park Visit-树的直径(BFS版)+结论公式(乱推公式)-备忘(加油!)的更多相关文章

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

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

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

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

  3. hdu 4607 Park Visit

    http://acm.hdu.edu.cn/showproblem.php?pid=4607 先求树的直径 方法:两遍bfs ,任选一点 a  求到a点最远的一点b ,然后 求到b点最远点 c 这样 ...

  4. hdu 4607 Park Visit 求树的直径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n) ...

  5. HDU 4607 Park visit (求树的直径)

    解题思路: 通过两次DFS求树的直径,第一次以随意点作为起点,找到距离该点距离最远的点,则能够证明这个点一定在树的直径上,然后以该点为起点进行DFS得到的最长路就是树的直径. 最后的询问,假设K &l ...

  6. HDU 4607 Park Visit (树的最长链)

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

  7. 题解报告:hdu 4607 Park Visit(最长链)

    Problem Description Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The par ...

  8. hdu 4607 Park Visit (dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 首先如果k小于等于直径长度,那么答案为k−1.如果k大于直径长度,设直径长度为r,那么答案为r− ...

  9. hdu 4607 树形dp 树的直径

    题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n)个点,至少需要走多少距离(每条边的距离是1): 思路:树形dp求树的直径r: a:若k<=r+1 ...

随机推荐

  1. Java 中 给静态方法 添加泛型 (static <T>)

    今天在用到static方法的时候.想要用泛型.结果不能通过编译. 上网查了一下.其具体写法如下:

  2. SpringMVC+MyBatis开发中指定callSettersOnNulls,可解决返回字段不全的问题

    Spring+MyBatis开发过程中,在xxMapper.xml配置文件进行select查询时resultType="map",如果要查询的字段是空值,在返回的map中会出现找不 ...

  3. session验证-使用filter过滤器

    public override void OnActionExecuting(ActionExecutingContext context) { string test = context.Contr ...

  4. [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    洛谷题目链接:[USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...

  5. 【C++ STL】List

    1.结构 list使用一个double linked list(双向链表)来管理元素. 2. list 能力 list内部结构和vector或deque截然不同,所以与他们的区别: list不支持随机 ...

  6. bzoj 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列——map+hash+转换

    Description N(1<=N<=100000)头牛,一共K(1<=K<=30)种特色, 每头牛有多种特色,用二进制01表示它的特色ID.比如特色ID为13(1101), ...

  7. 【BZOJ】1584: [Usaco2009 Mar]Cleaning Up 打扫卫生

    [算法]DP+数学优化 [题意]把n个1~m的数字分成k段,每段的价值为段内不同数字个数的平方,求最小总价值.n,m,ai<=40000 [题解] 参考自:WerKeyTom_FTD 令f[i] ...

  8. HDU 1234 开门人和关门人 (模拟)

    题目链接 Problem Description 每天第一个到机房的人要把门打开,最后一个离开的人要把门关好.现有一堆杂乱的机房签  到.签离记录,请根据记录找出当天开门和关门的人.    Input ...

  9. github: Permission denied (publickey). 问题解决方法

    部署服务器过程中想clone自己github中的库,结果出现Permission denied (publickey).的错误,解决方法是添加服务器公钥到github的settings->SSH ...

  10. HDU 2199 二分

    我们可以发现这个函数是单增的,那么这样二分就好了. 反思:刚转C++,不会用scanf读入实数.(scanf("%lf",&y)) //By BLADEVIL #inclu ...