HDU-4607 Park Visit bfs | DP | dfs
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607
首先考虑找一条最长链长度k,如果m<=k+1,那么答案就是m。如果m>k+1,那么最长链上还有其他分支,来回走一遍,因此答案为2*m-k-1。。。求最长链可以DP,两次BFS或者DFS等。。
//STATUS:C++_AC_453MS_3524KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const LL INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End struct Edge{
int u,v;
}e[N*];
int first[N],next[N*],d[N];
int Ca,T,n,m,mt; void adde(int a,int b)
{
e[mt].u=a;e[mt].v=b;
next[mt]=first[a];first[a]=mt++;
e[mt].u=b;e[mt].v=a;
next[mt]=first[b];first[b]=mt++;
} int bfs(int s)
{
int u,i,hig;
mem(d,);
d[s]=;
queue<int> q;
q.push(s);
hig=-;
while(!q.empty())
{
u=q.front();q.pop();
for(i=first[u];i!=-;i=next[i]){
if(!d[e[i].v]){
d[e[i].v]=d[e[i].u]+;
if(d[e[i].v]>hig){
hig=d[e[i].v];
T=e[i].v;
}
q.push(e[i].v);
}
}
}
return hig;
} int main()
{
// freopen("in.txt","r",stdin);
int i,j,a,b,hig;
scanf("%d",&Ca);
while(Ca--)
{
scanf("%d%d",&n,&m);
mem(first,-);mt=;
for(i=;i<n;i++){
scanf("%d%d",&a,&b);
adde(a,b);
}
bfs();
hig=bfs(T); while(m--){
scanf("%d",&a);
if(a>hig){
printf("%d\n",*a-hig-);
}
else printf("%d\n",a-);
}
}
return ;
}
HDU-4607 Park Visit bfs | DP | dfs的更多相关文章
- HDU 4607 Park Visit 两次DFS求树直径
两次DFS求树直径方法见 这里. 这里的直径是指最长链包含的节点个数,而上一题是指最长链的路径权值之和,注意区分. K <= R: ans = K − 1; K > R: ans = ...
- HDU 4607 Park Visit (树的最长链)
Park Visit Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 4607 Park Visit (dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 首先如果k小于等于直径长度,那么答案为k−1.如果k大于直径长度,设直径长度为r,那么答案为r− ...
- HDU 4607 Park Visit (DP最长链)
[题目]题意:N个城市形成一棵树,相邻城市之间的距离是1,问访问K个城市的最短路程是多少,共有M次询问(1 <= N, M <= 100000, 1 <= K <= N). [ ...
- hdu 4607 Park Visit 求树的直径
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n) ...
- 题解报告:hdu 4607 Park Visit(最长链)
Problem Description Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The par ...
- hdu 4607 Park Visit
http://acm.hdu.edu.cn/showproblem.php?pid=4607 先求树的直径 方法:两遍bfs ,任选一点 a 求到a点最远的一点b ,然后 求到b点最远点 c 这样 ...
- hdu 4607 Park Visit(树上最长链)
求树上最长链:两遍搜索. 第一次从树上任意点开始,最远点必然是某一条最长链上的端点u. 第二次从u开始,最远点即该最长链的另一端点. 先在最长链上走,不足再去走支链. 把询问数m错打成n,狠狠wa了一 ...
- HDU 4607 Park Visit(树的直径)
题目大意:给定一棵树,让求出依次访问k个点的最小花费,每条边的权值都为1. 思路:如果能一直往下走不回来,那么这个路径肯定是最小的,这就取决于给定的k,但是怎么确定这个能一直走的长度呢,其实这个就是树 ...
随机推荐
- Error building Player: CommandInvokationFailure: Failed to re-package resources. See the Console for details. ShareSDK 也有这种错误
Error building Player: CommandInvokationFailure: Failed to re-package resources. See the Console for ...
- POJ 1036 Rails 模拟堆栈
水题,主要是思路清晰,判断明确. 记x为A站最前方的车,y表示下一列要进入B站的车厢,初识时,x=1;y=a1;C=[]; 在调度过程中: if(y==0)那么调度成功,退出模拟过程:否则 if(x= ...
- Servlet 下载文件
这几天有点懒散,还好没有忘记看书,上周去了国家图书馆翻阅了一些和Java相关的书籍,其实这些书都是自己以前看过或者听过,按理来说,不应该看自己已经看过的书籍,应该找一些最新的书籍去看,但是每次走到书架 ...
- 拔高你的Java代码质量吧:推荐使用枚举定义常量(转)
提高你的Java代码质量吧:推荐使用枚举定义常量 一.分析 常量的声明是每一个项目中不可或缺的,在Java1.5之前,我们只有两种方式的声明:类常量和接口常量.不过,在1.5版之后有了改进,即新增了一 ...
- 【HDOJ】4355 Party All the Time
好久没做过三分的题目了. /* 4355 */ #include <iostream> #include <sstream> #include <string> # ...
- python 控制vbox虚拟机
(本机环境:win7:python 2.7.9: vbox 4.3.12) 目标: 完成官网(https://pypi.python.org/pypi/pyvbox)上的demo: > pyth ...
- 正确认识Android的内存管理机制,合理关闭进程 (一)
随着大家收货后会有很多乐粉晒内存,为啦方便大家,在网上搜集了一些相关Andriod管理的相关机制合理管理内存,整理下发个贴. 首先要知道Android系统是基于Linux 2.6内核开发的开源操作系统 ...
- Codeforces Round #221 (Div. 2) Lever I.O.U.
这场cf 做的很差,,第一题犯了一个很低级的错误..把i写成了J.... 第二题 想的太复杂了...其实我们只需要 考虑每个人自己的负债情况就行了,就是假设每个人把别人 欠他的钱,拿过来还给别人..这 ...
- poj 2442 Sequence(优先队列)
题目:http://poj.org/problem?id=2442 题意:给你n*m的矩阵,然后每行取一个元素,组成一个包含n个元素的序列,一共有n^m种序列, 让你求出序列和最小的前n个序列的序列和 ...
- SPOJ DQUERY:D-query
主席树/树状数组.给一个区间,多次询问[l,r]内有多少个不重复的元素.每个前缀都建线段树,询问直接r的[l,r]就可以了.(似乎对主席树有一点了解了?...话说spoj好高级的样子... #incl ...