HDU4607+BFS
/*
bfs+求树的直径
关键:if k<=maxs+1 直接输出k-1;
else: k肯定的是包括最长路。先从最长路的起点出发,再走分支,最后到达最长路的终点。
因此是2*(k-(maxs+1))+maxs;
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<math.h>
using namespace std;
typedef long long ll;
//typedef __int64 int64;
const int maxn = ;
const int inf = 0x7fffffff;
const double pi=acos(-1.0);
const double eps = 1e-;
struct Node{
int v,next;
}edge[ maxn<< ];
int cnt,head[ maxn ];
int vis[ maxn ],dis[ maxn ];
int maxs,maxNode;
void init(){
cnt = ;
memset( head,-,sizeof( head ) );
}
void addedge( int a,int b ){
edge[ cnt ].v = b;
edge[ cnt ].next = head[ a ];
head[ a ] = cnt++; edge[ cnt ].v = a;
edge[ cnt ].next = head[ b ];
head[ b ] = cnt++;
}
void bfs( int s,int n ){
memset( vis,,sizeof( vis ) );
vis[ s ] = ;
queue<int>q;
q.push( s );
//for( int i=0;i<=n;i++ )
//dis[ i ] = inf;
dis[ s ] = ;
maxs = ;
while( !q.empty() ){
int cur = q.front();
q.pop();
if( dis[ cur ]>maxs ){
maxs = dis[ cur ];
maxNode = cur;
}
//maxs = max( maxs,dis[ cur ] );
for( int i=head[ cur ];i!=-;i=edge[ i ].next ){
int v = edge[ i ].v;
if( vis[ v ]== ) continue;
vis[ v ] = ;
dis[ v ] = dis[ cur ]+;
q.push( v );
}
}
return ;
}
int main(){
int T;
scanf("%d",&T);
while( T-- ){
int n,m;
scanf("%d%d",&n,&m);
int a,b;
init();
int N = n-;
while( N-- ){
scanf("%d%d",&a,&b);
addedge( a,b );
}
bfs( ,n );
bfs( maxNode,n );
//maxs=the R of the tree
while( m-- ){
scanf("%d",&b);
if( b<=maxs+ ) printf("%d\n",b-);
else printf("%d\n",*(b-maxs-)+maxs);
}
}
return ;
}
HDU4607+BFS的更多相关文章
- HDU-4607 Park Visit bfs | DP | dfs
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 首先考虑找一条最长链长度k,如果m<=k+1,那么答案就是m.如果m>k+1,那么最 ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 186 Solved: 118[Su ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- Sicily 1215: 脱离地牢(BFS)
这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...
- Sicily 1048: Inverso(BFS)
题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...
- Sicily 1444: Prime Path(BFS)
题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...
- Sicily 1051: 魔板(BFS+排重)
相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...
随机推荐
- C# 中控件 WebBrowser 对 frameset/ iframe 操作和内容获取
1.获取frame的document HtmlDocument htmlDoc = webBrowser1.Document; htmlDoc = webBrowser1.Document.Wind ...
- C# Winform AutoScaleMode属性
属性的用法: None : 禁用自动缩放.(默认时) Font :根据类使用的字体(通常为系统字体)的维度控制缩放. Dpi : 根据显示分辨率控制缩放.常用分辨率为 96 和 120 DPI. In ...
- ASP.NET实现在线人员实时显示
在最近的学习中,参考其他资源,做了一个简单的在线人员显示的功能,总结了一下,思路如下: 1.定义一个全局的内存来作为在线人员列表 2.通过实时判断用户Session值,来判断某个用户的登录或离线 3. ...
- js画了一个椭圆
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- css笔记——杂记
####文本框内阴影#### input[type=text], textarea, input[type=email], input[type=url]{ box-shadow: inset 2px ...
- 【leetcode】8. String to Integer (atoi)
题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...
- template_11实参演绎
1,演绎过程匹配类型A(来自实参的类型),参数化类型P(行参参数声明)如果被声明的参数是一个引用声明g(T& )那么P就是所引用类型T:f(T)中P就是所声明的参数类: decay指从数组和函 ...
- Laravel Homestead安装教程
1/先在https://www.vagrantup.com 下载vagrantup ,对应平台下载,并安装,安装后可以在命令行使用vagrant https://www.vagrantup.com/ ...
- 关于B/S系统中文件上传的大小限制怎么做
1.前端:采用flash控件或者Html5的特性(有浏览器版本要求)来判断文件大小.纯html或js是没法判断用户上传文件大小的. 2.nginx:服务器端的第一道防线,一般会有对上传文件做大小限制. ...
- C# 学习之旅(1)
第一, 输入输出流都来自控制台. using System; using System.Collections.Generic; using System.Linq; using System.Tex ...