Bob’s Race
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 378   Accepted: 119

Description

Bob wants to hold a race to encourage people to do sports. He has got trouble in choosing the route. There are N houses and N - 1 roads in his village. Each road connects two houses, and all houses are connected together. To make the race more interesting, he requires that every participant must start from a different house and run AS FAR AS POSSIBLE without passing a road more than once. The distance difference between the one who runs the longest distance and the one who runs the shortest distance is called “race difference” by Bob. Bob does not want the “race difference”to be more than Q. The houses are numbered from 1 to N. Bob wants that the No. of all starting house must be consecutive. He is now asking you for help. He wants to know the maximum number of starting houses he can choose, by other words, the maximum number of people who can take part in his race.

Input

There are several test cases. 

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

For each test case, you should output the answer in a line for each query.

Sample Input

  1. 5 5
  2. 1 2 3
  3. 2 3 4
  4. 4 5 3
  5. 3 4 2
  6. 1
  7. 2
  8. 3
  9. 4
  10. 5
  11. 0 0

Sample Output

  1. 1
  2. 3
  3. 3
  4. 3
  5. 5

Source


题目大意:告诉n,m , 表示一棵树n个节点,接下来n-1条边将n个节点,m表示m组查询q,查询最长的连续区间差值不超过q的长度。

解体思路:与我的   
poj 3162 Walking Race (DFS + 线段树) 有点类似,但是,比那个复杂,不同在那个只有一次查询,这次m次查询,那个效率 O( n*lgn ),如果用那个方法,这次也就是
 O(m* n*lgn )的效率,实验发现超时了,最后用了 rmq算法代替了线段树,用rmq预处理好,查询每次区间的效率为 1 而不是 lgn ,所以效率变为   O(m* n),花了 1秒左右AC。 

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <climits>
  4. #include <map>
  5. #include <vector>
  6. #include <algorithm>
  7. using namespace std;
  8.  
  9. const int maxn=100010;
  10.  
  11. struct edge{
  12. int u,v,w;
  13. int next;
  14. edge(int u0=0,int v0=0,int w0=0){ u=u0;v=v0;w=w0;}
  15. }e[maxn*2];
  16.  
  17. int n,m,cnt,head[maxn],d[maxn],dx[maxn],dy[maxn],qmin[maxn],qmax[maxn],mx,mn;
  18. int maxsum[maxn][20],minsum[maxn][20],flog[maxn];
  19.  
  20. void initial(){
  21. cnt=0;
  22. for(int i=0;i<=n;i++) head[i]=-1;
  23. }
  24.  
  25. void addedge(int u,int v,int w){
  26. e[cnt]=edge(u,v,w);
  27. e[cnt].next=head[u];
  28. head[u]=cnt++;
  29. }
  30.  
  31. void input(){
  32. int x,y,w0;
  33. for(int i=2;i<=n;i++){
  34. scanf("%d%d%d",&x,&y,&w0);
  35. addedge(x,y,w0);
  36. addedge(y,x,w0);
  37. }
  38. }
  39.  
  40. void dfs(int u,int fa,int dis,int *d){
  41. for(int i=head[u];i!=-1;i=e[i].next){
  42. int v=e[i].v,w=e[i].w;
  43. if(v!=fa) dfs(v,u,d[v]=dis+w,d);
  44. }
  45. }
  46.  
  47. void solve1(){
  48. int x=1,y=1;
  49. dfs(1,-1,d[1]=0,d);
  50. for(int i=1;i<=n;i++) if(d[x]<d[i]) x=i;
  51. dfs(x,-1,dx[x]=0,dx);
  52. for(int i=1;i<=n;i++) if(dx[y]<dx[i]) y=i;
  53. dfs(y,-1,dy[y]=0,dy);
  54. for(int i=1;i<=n;i++) d[i]=max(dx[i],dy[i]);
  55. //for(int i=1;i<=n;i++) cout<<"dis["<<i<<"]:"<<d[i]<<endl;
  56. }
  57.  
  58. void getrmq(){
  59. int r=2,cnt=0;
  60. for(int i=1;i<=n;i++){
  61. if(i<r) flog[i]=cnt;
  62. else{
  63. flog[i]=++cnt;
  64. r=r<<1;
  65. }
  66. }
  67. for(int i=1;i<=n;i++){
  68. maxsum[i][0]=d[i];
  69. minsum[i][0]=d[i];
  70. }
  71. for(int j=1;j<=flog[n];j++)
  72. for(int i=1;i<=n;i++){
  73. if(i+(1<<j)-1<=n){
  74. maxsum[i][j]=max(maxsum[i][j-1],maxsum[i+(1<<(j-1))][j-1]);
  75. minsum[i][j]=min(minsum[i][j-1],minsum[i+(1<<(j-1))][j-1]);
  76. }
  77. }
  78. }
  79.  
  80. int getmin(int l,int r){
  81. int x=flog[r-l+1];
  82. return min(minsum[l][x],minsum[r-(1<<x)+1][x]);
  83. }
  84.  
  85. int getmax(int l,int r){
  86. int x=flog[r-l+1];
  87. return max(maxsum[l][x],maxsum[r-(1<<x)+1][x]);
  88. }
  89.  
  90. void solve2(){
  91. int be=1,en=1,ans=1,q=1;
  92. map <int,int> mp;
  93. vector<int> v;
  94. map <int,int>::iterator it;
  95. for(int i=0;i<m;i++){
  96. scanf("%d",&q);
  97. mp[q]=0;
  98. v.push_back(q);
  99. }
  100. for(it=mp.begin();it!=mp.end();it++){
  101. int be=1,en=be+ans-1;
  102. while(en<=n){
  103. mn=getmin(be,en),mx=getmax(be,en);
  104. if(mx-mn<=(it->first)){
  105. ans=max(en-be+1,ans);
  106. en++;
  107. }else{
  108. be++;
  109. en=max(en,be+ans-1);
  110. }
  111. }
  112. it->second=ans;
  113. }
  114. for(int i=0;i<m;i++) printf("%d\n",mp[v[i]]);
  115. }
  116.  
  117. void computing(){
  118. solve1();
  119. getrmq();
  120. solve2();
  121. }
  122.  
  123. int main(){
  124. while(scanf("%d%d",&n,&m)!=EOF && (n||m) ){
  125. initial();
  126. input();
  127. computing();
  128. }
  129. return 0;
  130. }

POJ 4003 Bob’s Race && HDU4123 Bob’s Race (dfs+rmq)的更多相关文章

  1. 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 ...

  2. POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

  3. [poj 2331] Water pipe ID A*迭代加深搜索(dfs)

    Water pipe Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2265 Accepted: 602 Description ...

  4. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  5. poj 3083 Children of the Candy Corn(DFS+BFS)

    做了1天,总是各种错误,很无语 最后还是参考大神的方法 题目:http://poj.org/problem?id=3083 题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径 DF ...

  6. POJ 1564(HDU 1258 ZOJ 1711) Sum It Up(DFS)

    题目链接:http://poj.org/problem?id=1564 题目大意:给定一个整数t,和n个元素组成的集合.求能否用该集合中的元素和表示该整数,如果可以输出所有可行解.1<=n< ...

  7. POJ 1321-棋盘问题(DFS 递归)

    POJ 1321-棋盘问题 K - DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...

  8. poj 1011 Sticks (DFS+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127771   Accepted: 29926 Descrip ...

  9. 数独问题的介绍及POJ 2676-Sudoku(dfs+剪枝)

    知道是数独问题后犹豫了一下要不要做(好像很难的样纸==.),用dfs并剪枝,是一道挺规范的搜索题. 先介绍以下数独吧- 数独(Sudoku)是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上 ...

随机推荐

  1. 【iOS】网页中调用JS与JS注入

    非常多应用为了节约成本,做出同一时候在Android与iOS上都能使用的界面,这时就要使用WebView来做.Android和IOS上都有WebView,做起来非常省事.当然这时就要考虑怎样在Andr ...

  2. 按钮的图标 Button icons-JQUERY MOBILE 1.0正式版中文手册

    按钮的图标 Button icons-JQUERY MOBILE 1.0正式版中文手册 data-icon属性可以被用来创建如下所示的图标 左箭头data-icon="arrow-l&quo ...

  3. [每日一题] OCP1z0-047 :2013-08-29 NULL............................................................168

    转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/10558305 正确答案:B 用函数可以针对各种数据类型时行操作,包括NULL值在内.其中有 ...

  4. servlet后台怎样接收对象參数

    主要思想是用js把对象转换成json.然后把json提交到后台去,后台把这个json字符串转换成map对象 <script type="text/javascript"> ...

  5. Linux账号管理(一)

    整理自<鸟哥的Linux私房菜>,整理者:华科小涛http://www.cnblogs.com/hust-ghtao/ 管理员的工作中,相当重要的一环就是“管理账号”.因为整个系统都是你在 ...

  6. (Relax 数论1.6)POJ 1061 青蛙的约会(扩展的欧几里得公式)

    /* * POJ_1061.cpp * * Created on: 2013年11月19日 * Author: Administrator */ #include <iostream> # ...

  7. 让delphi程序不受WINDOWS日期格式的影响(使用SetLocaleInfo函数和Application.UpdateFormatSettings)

    如果WINDOWS系统的短日期格式为“yyyy/m/d”,执行下面的代码会报错:2013-01-29 00:00:00不是合法的日期procedure TFrmQuerySale.FormShow(S ...

  8. find: paths must precede expression(转)

    find: paths must precede expressionUsage: find [-H] [-L] [-P] [path...] [expression] 然后就上网查了一下,结果搜索到 ...

  9. javascript json格式解析方法

    json.parse用于从一个字符串中解析出json对象 stringify()用于从一个对象解析出字符串 var dataObj = eval("("+json+")& ...

  10. 我在知乎上关于Laser200/310电脑的文章。

    我是30年前从Laser-310起步的,我来回答这个问题. 主要硬件规格: CPU:Z-80A/4.7MHz主频 16K RAM + 2K Video RAM 16K ROM 磁带输出:波特率300 ...