POJ 4003 Bob’s Race && HDU4123 Bob’s Race (dfs+rmq)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 378 | Accepted: 119 |
Description
Input
The first line of each test case contains two integers N and M. N is the number of houses, M is the number of queries.
The following N-1 lines, each contains three integers, x, y and z, indicating that there is a road of length z connecting house x and house y.
The following M lines are the queries. Each line contains an integer Q, asking that at most how many people can take part in Bob’s race according to the above mentioned rules and under the condition that the“race difference”is no more than Q.
The input ends with N = 0 and M = 0.
(N<=50000 M<=500 1<=x,y<=N 0<=z<=5000 Q<=10000000)
Output
Sample Input
5 5
1 2 3
2 3 4
4 5 3
3 4 2
1
2
3
4
5
0 0
Sample Output
1
3
3
3
5
Source
poj 3162 Walking Race (DFS + 线段树) 有点类似,但是,比那个复杂,不同在那个只有一次查询,这次m次查询,那个效率 O( n*lgn ),如果用那个方法,这次也就是
O(m* n*lgn )的效率,实验发现超时了,最后用了 rmq算法代替了线段树,用rmq预处理好,查询每次区间的效率为 1 而不是 lgn ,所以效率变为 O(m* n),花了 1秒左右AC。
#include <iostream>
#include <cstdio>
#include <climits>
#include <map>
#include <vector>
#include <algorithm>
using namespace std; const int maxn=100010; struct edge{
int u,v,w;
int next;
edge(int u0=0,int v0=0,int w0=0){ u=u0;v=v0;w=w0;}
}e[maxn*2]; int n,m,cnt,head[maxn],d[maxn],dx[maxn],dy[maxn],qmin[maxn],qmax[maxn],mx,mn;
int maxsum[maxn][20],minsum[maxn][20],flog[maxn]; void initial(){
cnt=0;
for(int i=0;i<=n;i++) head[i]=-1;
} void addedge(int u,int v,int w){
e[cnt]=edge(u,v,w);
e[cnt].next=head[u];
head[u]=cnt++;
} void input(){
int x,y,w0;
for(int i=2;i<=n;i++){
scanf("%d%d%d",&x,&y,&w0);
addedge(x,y,w0);
addedge(y,x,w0);
}
} void dfs(int u,int fa,int dis,int *d){
for(int i=head[u];i!=-1;i=e[i].next){
int v=e[i].v,w=e[i].w;
if(v!=fa) dfs(v,u,d[v]=dis+w,d);
}
} void solve1(){
int x=1,y=1;
dfs(1,-1,d[1]=0,d);
for(int i=1;i<=n;i++) if(d[x]<d[i]) x=i;
dfs(x,-1,dx[x]=0,dx);
for(int i=1;i<=n;i++) if(dx[y]<dx[i]) y=i;
dfs(y,-1,dy[y]=0,dy);
for(int i=1;i<=n;i++) d[i]=max(dx[i],dy[i]);
//for(int i=1;i<=n;i++) cout<<"dis["<<i<<"]:"<<d[i]<<endl;
} void getrmq(){
int r=2,cnt=0;
for(int i=1;i<=n;i++){
if(i<r) flog[i]=cnt;
else{
flog[i]=++cnt;
r=r<<1;
}
}
for(int i=1;i<=n;i++){
maxsum[i][0]=d[i];
minsum[i][0]=d[i];
}
for(int j=1;j<=flog[n];j++)
for(int i=1;i<=n;i++){
if(i+(1<<j)-1<=n){
maxsum[i][j]=max(maxsum[i][j-1],maxsum[i+(1<<(j-1))][j-1]);
minsum[i][j]=min(minsum[i][j-1],minsum[i+(1<<(j-1))][j-1]);
}
}
} int getmin(int l,int r){
int x=flog[r-l+1];
return min(minsum[l][x],minsum[r-(1<<x)+1][x]);
} int getmax(int l,int r){
int x=flog[r-l+1];
return max(maxsum[l][x],maxsum[r-(1<<x)+1][x]);
} void solve2(){
int be=1,en=1,ans=1,q=1;
map <int,int> mp;
vector<int> v;
map <int,int>::iterator it;
for(int i=0;i<m;i++){
scanf("%d",&q);
mp[q]=0;
v.push_back(q);
}
for(it=mp.begin();it!=mp.end();it++){
int be=1,en=be+ans-1;
while(en<=n){
mn=getmin(be,en),mx=getmax(be,en);
if(mx-mn<=(it->first)){
ans=max(en-be+1,ans);
en++;
}else{
be++;
en=max(en,be+ans-1);
}
}
it->second=ans;
}
for(int i=0;i<m;i++) printf("%d\n",mp[v[i]]);
} void computing(){
solve1();
getrmq();
solve2();
} int main(){
while(scanf("%d%d",&n,&m)!=EOF && (n||m) ){
initial();
input();
computing();
}
return 0;
}
POJ 4003 Bob’s Race && HDU4123 Bob’s Race (dfs+rmq)的更多相关文章
- POJ 3207 Ikki's Story IV - Panda's Trick(2-sat问题)
POJ 3207 Ikki's Story IV - Panda's Trick(2-sat问题) Description liympanda, one of Ikki's friend, likes ...
- POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE
POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...
- [poj 2331] Water pipe ID A*迭代加深搜索(dfs)
Water pipe Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2265 Accepted: 602 Description ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
- poj 3083 Children of the Candy Corn(DFS+BFS)
做了1天,总是各种错误,很无语 最后还是参考大神的方法 题目:http://poj.org/problem?id=3083 题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径 DF ...
- POJ 1564(HDU 1258 ZOJ 1711) Sum It Up(DFS)
题目链接:http://poj.org/problem?id=1564 题目大意:给定一个整数t,和n个元素组成的集合.求能否用该集合中的元素和表示该整数,如果可以输出所有可行解.1<=n< ...
- POJ 1321-棋盘问题(DFS 递归)
POJ 1321-棋盘问题 K - DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
- poj 1011 Sticks (DFS+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 127771 Accepted: 29926 Descrip ...
- 数独问题的介绍及POJ 2676-Sudoku(dfs+剪枝)
知道是数独问题后犹豫了一下要不要做(好像很难的样纸==.),用dfs并剪枝,是一道挺规范的搜索题. 先介绍以下数独吧- 数独(Sudoku)是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上 ...
随机推荐
- [WCF]WCF起航
解决方案概览: Client:windows 控制台应用程序. WcfService1: windows 服务应用程序. WCFWebTest:asp.net 空web应用程序. 变量程序命名.结构可 ...
- 动态绑定Gridview带模板列
公司要做一个可以支持4种数据库(<!--数据库类型 (DLL专用) (SQL SERVER) (ORACLE) (ACCESS)(POSTGRE SQL)-->)的并且字段随表字段变化的可 ...
- 图解C#_事件
概述 今天用来演示事件的例子是模拟实现一个文件下载类,在这个类中我将定义一个DownLoad事件,这个事件用来在文件下载的过程中,向订阅这个事件的用户发出消息,而这个消息将用DownLoadEvent ...
- js正则验证手机号码有效性
验证130-139,150-159,180-189号码段的手机号码 <script type="text/javascript"> [-]{})|([-]{})|([- ...
- 根据li标签 查找class="alcw4 alcw41"对应的值
jrhmpt01:/root/lwp/0526# cat a2.pl use LWP::UserAgent; use DBI; use POSIX; use Data::Dumper; use HTM ...
- Web前端,高性能优化
高性能HTML 一.避免使用iframe iframe也叫内联frame,可将一个HTML文档嵌入另一个HTML文档中. iframe的好处是,嵌入的文档独立于父文档,通常也借此使浏览器模拟多线程.缺 ...
- 两个栈实现队列+两个队列实现栈----java
两个栈实现队列+两个队列实现栈----java 一.两个栈实现一个队列 思路:所有元素进stack1,然后所有出s ...
- 《Swift编程语言》中文翻译及读书笔记page25
The Swift Programming Language读书笔记学习笔记 第25页 本页主要说在swift语言里能够使用分号,但分号不作为每条swift语言语句的结尾 而是间隔写在一行的多条swi ...
- 高仿精仿快播应用android源码下载
今天给大家在网上找到的一款高仿精仿快播应用android源码,分享给大家,希望大家功能喜欢. 说明源码更新中.... 源码即将上传 也可以到这个网站下载:download
- 管道是如何处理HTTP请求的?
管道是如何处理HTTP请求的? 我们知道ASP.NET Core请求处理管道由一个服务器和一组有序的中间件组成,所以从总体设计来讲是非常简单的,但是就具体的实现来说,由于其中涉及很多对象的交互,我想很 ...