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,但是怎么确定这个能一直走的长度呢,其实这个就是树 ...
随机推荐
- spoj 338
题意: 无向图 每条边有长度和费用两个属性 求从点1到点n 在花费不超过 k 的情况下的最短路径 BFS 使用优先队列 长度短的优先出列 题解上的方法没看懂 不知道怎么用链表维护 . ...
- hdu 4648
求一个数列中 去掉一些连续的数之后 不改变对m取余后的值 就是求一个最长的连续子序列 对m取余后为0 我的方法可能比较水 #include<iostream> #include<c ...
- Docker 监控实战
如今,越来越多的公司开始使用 Docker 了,现在来给大家看几组数据: 2 / 3 的公司在尝试了 Docker 后最终使用了它 也就是说 Docker 的转化率达到了 67%,而转化市场也控制在 ...
- 眼见为实(1):C++基本概念在编译器中的实现
眼见为实(1):C++基本概念在编译器中的实现 对于C++对象模型,相信很多程序员都耳熟能详. 本文试图通过一个简单的例子演示一些C++基本概念在编译器中的实现,以期达到眼见为实的效果. 本文的演示程 ...
- 在线学习SQL语句?没问题~~
以前弄得少,没注意.. http://sqlfiddle.com/ CREATE TABLE Presidents ( Id INT UNSIGNED NOT NULL AUTO_INCREMENT, ...
- P94、面试题12:打印1到最大的n位数
题目:输入数字n,按顺序打印出从1最大的n位十进制数.比如输入3,则打印出1,2,3一直到最大的3位数999. 思路:先把字符串中的每一个数字都初始化为‘0’,然后每一次为字符串表示的数字加1,再打印 ...
- Source Insight 安装使用
习惯了在source insight下编辑阅读源码,在linux下用vi总是用不好 ,还是在Ubuntu上用回熟悉的source insight. 在Ubuntu中,安装Windows程序用wine, ...
- python web开发遇到socket.error[errno 10013]
socket.error[errno 10013],端口被占用 重新换一个端口,或者把占用该端口的程序关闭就可以了
- MAC 上搭建lua环境
一.下载并安装 (1)最新release版下载地址 http://www.lua.org/ftp/lua-5.3.1.tar.gz (2)编译 Building Lua is implemented ...
- URAL1501. Sense of Beauty(记忆化)
链接 dfs+记忆化 对于当前状态虽然满足和差 但如果搜下去没有满足的情况也是不可以的 所以需要记忆化下 #include <iostream> #include<cstdio> ...