题目链接:

How far away ?

Time Limit: 2000/1000 MS (Java/Others)   

 Memory Limit: 32768/32768 K (Java/Others)

Problem Description
 
There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int this village the answer is always unique, since the roads are built in the way that there is a unique simple path("simple" means you can't visit a place twice) between every two houses. Yout task is to answer all these curious people.
 
Input
 
First line is a single integer T(T<=10), indicating the number of test cases.
  For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n.
  Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.
 
Output
 
For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.
 
Sample Input
2
3 2
1 2 10
3 1 15
1 2
2 3
 
2 2
1 2 100
1 2
2 1
 
Sample Output
10
25
100
100
 
题意:
 
给一个无向图,问两点i和j之间的距离是多少;
 
思路:
 
由于询问规模大,所以就无向图转化成树,然后寻找lca,然后再算距离;
把lca转化成RMQ问题,我是用的线段树找的RMQ;
 
AC代码:
 
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N=4e4+;
  4. typedef long long ll;
  5. const double PI=acos(-1.0);
  6. int n,m,t,dis[N],dep[N],vis[N],w[N];
  7. vector<int>ve[N];
  8. queue<int>qu;
  9. int u[N],v[N],d[N],fa[N],a[*N],tot,first[N];
  10. struct Tree
  11. {
  12. int l,r,ans;
  13. };
  14. Tree tree[*N];
  15. void bfs()//bfs把图转化成树;
  16. {
  17. qu.push();
  18. vis[]=;
  19. dep[]=;
  20. while(!qu.empty())
  21. {
  22. int top=qu.front();
  23. qu.pop();
  24. int len=ve[top].size();
  25. for(int i=;i<len;i++)
  26. {
  27. int x=ve[top][i];
  28. if(!vis[x])
  29. {
  30. vis[x]=;
  31. qu.push(x);
  32. dep[x]=dep[top]+;
  33. fa[x]=top;
  34. }
  35. }
  36. }
  37. }
  38. int dfs(int num)//dfs找到lca的数组,并计算i到1的dis
  39. {
  40. vis[num]=;
  41. first[num]=tot;
  42. a[tot++]=num;
  43. int len=ve[num].size();
  44. for(int i=;i<len;i++)
  45. {
  46. int x=ve[num][i];
  47. if(vis[x])
  48. {
  49. dis[x]=dis[num]+d[x];
  50. dfs(x);
  51. a[tot++]=num;
  52. }
  53. }
  54. }
  55. void Pushup(int node)
  56. {
  57. if(dep[a[tree[*node].ans]]>=dep[a[tree[*node+].ans]])tree[node].ans=tree[*node+].ans;
  58. else tree[node].ans=tree[*node].ans;
  59. }//tree[node].ans为数组里的lca的位置;
  60. void build(int node,int L,int R)
  61. {
  62. tree[node].l=L;
  63. tree[node].r=R;
  64. if(L>=R)
  65. {
  66. tree[node].ans=L;
  67. return ;
  68. }
  69. int mid=(L+R)>>;
  70. build(*node,L,mid);
  71. build(*node+,mid+,R);
  72. Pushup(node);
  73. }
  74. int query(int node,int L,int R)
  75. {
  76. if(L<=tree[node].l&&R>=tree[node].r)return tree[node].ans;
  77. int mid=(tree[node].l+tree[node].r)>>;
  78. if(R<=mid)return query(*node,L,R);
  79. else if(L>mid)return query(*node+,L,R);
  80. else
  81. {
  82. int pos1=query(*node,L,mid),pos2=query(*node+,mid+,R);
  83. if(dep[a[pos1]]>=dep[a[pos2]])return pos2;
  84. else return pos1;
  85. }
  86. }
  87. int main()
  88. {
  89. scanf("%d",&t);
  90. while(t--)
  91. {
  92. scanf("%d%d",&n,&m);
  93. for(int i=;i<=n;i++)
  94. {
  95. ve[i].clear();
  96. }
  97. for(int i=;i<n;i++)
  98. {
  99. scanf("%d%d%d",&u[i],&v[i],&w[i]);
  100. ve[u[i]].push_back(v[i]);
  101. ve[v[i]].push_back(u[i]);
  102. }
  103. memset(vis,,sizeof(vis));
  104. bfs();
  105. for(int i=;i<n;i++)
  106. {
  107. if(fa[u[i]]==v[i])
  108. {
  109. d[u[i]]=w[i];
  110. }
  111. else
  112. {
  113. d[v[i]]=w[i];
  114. }
  115. }
  116. tot=;
  117. dfs();
  118. build(,,tot-);
  119. int ql,qr;
  120. for(int i=;i<m;i++)
  121. {
  122. scanf("%d%d",&ql,&qr);
  123. int pos;
  124. if(first[ql]<=first[qr])
  125. pos=query(,first[ql],first[qr]);
  126. else pos=query(,first[qr],first[ql]);
  127. printf("%d\n",dis[ql]-dis[a[pos]]-dis[a[pos]]+dis[qr]);
  128. }
  129. }
  130. return ;
  131. }
 

hdu-2586 How far away ?(lca+bfs+dfs+线段树)的更多相关文章

  1. HDU 5877 dfs+ 线段树(或+树状树组)

    1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...

  2. Tsinsen A1505. 树(张闻涛) 倍增LCA,可持久化线段树,DFS序

    题目:http://www.tsinsen.com/A1505 A1505. 树(张闻涛) 时间限制:1.0s   内存限制:512.0MB    总提交次数:196   AC次数:65   平均分: ...

  3. 51nod 1766 树上的最远点对 | LCA ST表 线段树 树的直径

    51nod 1766 树上的最远点对 | LCA ST表 线段树 树的直径 题面 n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个区间内各选一点之间的最大距离,即 ...

  4. Codeforces1110F Nearest Leaf dfs + 线段树 + 询问离线

    Codeforces1110F dfs + 线段树 + 询问离线 F. Nearest Leaf Description: Let's define the Eulerian traversal of ...

  5. HDU 2795 Billboard(宣传栏贴公告,线段树应用)

    HDU 2795 Billboard(宣传栏贴公告,线段树应用) ACM 题目地址:HDU 2795 Billboard 题意:  要在h*w宣传栏上贴公告,每条公告的高度都是为1的,并且每条公告都要 ...

  6. dfs+线段树 zhrt的数据结构课

    zhrt的数据结构课 这个题目我觉得是一个有一点点思维的dfs+线段树 虽然说看起来可以用树链剖分写,但是这个题目时间卡了树剖 因为之前用树剖一直在写这个,所以一直想的是区间更新,想dfs+线段树,有 ...

  7. HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...

  8. hdu 5692(dfs+线段树) Snacks

    题目http://acm.hdu.edu.cn/showproblem.php?pid=5692 题目说每个点至多经过一次,那么就是只能一条路线走到底的意思,看到这题的格式, 多个询问多个更新, 自然 ...

  9. HDU 3974 Assign the task (DFS+线段树)

    题意:给定一棵树的公司职员管理图,有两种操作, 第一种是 T x y,把 x 及员工都变成 y, 第二种是 C x 询问 x 当前的数. 析:先把该树用dfs遍历,形成一个序列,然后再用线段树进行维护 ...

随机推荐

  1. 世纪怎么换算成具体的年份?eg:19世纪60年代=19??年?

    http://zhidao.baidu.com/question/339742625.html&__bd_tkn__=6ab5183c226b84031b08b849ecac35b396039 ...

  2. VueJS自定义全局和局部指令

    除了默认设置的核心指令( v-model 和 v-show ), Vue 也允许注册自定义指令. 使用directive自定义全局指令 下面我们注册一个全局指令 v-focus, 该指令的功能是在页面 ...

  3. eclipse.ini配置文件

    Eclipse安装Maven插件后,Eclipse启动问题:Maven Integration for Eclipse JDK Warning.  解决方法: 1. 设置Eclipse使用的JRE为本 ...

  4. git系列1

    git clone支持多种协议,除了HTTP(s)以外,还支持SSH.Git.本地文件协议等,下面是一些例子. $ git clone http[s]://example.com/path/to/re ...

  5. gdb ../sysdeps/i386/elf/start.S: No such file or directory

    使用 gdb 调试的时候 输入 l 之后出现下列信息 (gdb) l 1 ../sysdeps/i386/elf/start.S: No such file or directory. in ../s ...

  6. MySQL 5.7 等高版本关于JDBC驱动的几个问题

    https://blog.csdn.net/dj673344908/article/details/85223313 mysql 5.7 用8.0版本的驱动可以,5.1版本也可以,5.5.5.6.5. ...

  7. EasyPlayer RTSP Windows播放器D3D,GDI的几种渲染方式的选择区别

    EasyPlayer-RTSP windows播放器支持D3D和GDI两种渲染方式,其中D3D支持格式如下: DISPLAY_FORMAT_YV12 DISPLAY_FORMAT_YUY2 DISPL ...

  8. EasyDSS RTMP流媒体解决方案之Windows服务安装方案

    Windows服务安装 EasyDSS_Solution流媒体解决方案,可以通过start一键启动.在实际应用中,我们希望可以设置成系统服务,那么下面我将会介绍,如何在windows中将流媒体解决方案 ...

  9. Elipse clean后无法编译出class文件

    通常之前一直运行正常的项目,在某次修改或重新启动时总是报 ClassNotFoundException,而事实是这个类确实存在,出现这种原因最好看看 build文件下的classes是否为空 或 编译 ...

  10. sql查询的日期判断问题

    在SQLSERVE中,如果某个数据表的类型被定义成datetime类型,那么数据是包含秒的.这时候如何查询某天的数据呢?新手们可能想:最直接的做法是把时间部分去掉,只取日期部分.于是日期的函数就用上了 ...