题目链接:

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代码:
 
#include <bits/stdc++.h>
using namespace std;
const int N=4e4+;
typedef long long ll;
const double PI=acos(-1.0);
int n,m,t,dis[N],dep[N],vis[N],w[N];
vector<int>ve[N];
queue<int>qu;
int u[N],v[N],d[N],fa[N],a[*N],tot,first[N];
struct Tree
{
int l,r,ans;
};
Tree tree[*N];
void bfs()//bfs把图转化成树;
{
qu.push();
vis[]=;
dep[]=;
while(!qu.empty())
{
int top=qu.front();
qu.pop();
int len=ve[top].size();
for(int i=;i<len;i++)
{
int x=ve[top][i];
if(!vis[x])
{
vis[x]=;
qu.push(x);
dep[x]=dep[top]+;
fa[x]=top;
}
}
}
}
int dfs(int num)//dfs找到lca的数组,并计算i到1的dis
{
vis[num]=;
first[num]=tot;
a[tot++]=num;
int len=ve[num].size();
for(int i=;i<len;i++)
{
int x=ve[num][i];
if(vis[x])
{
dis[x]=dis[num]+d[x];
dfs(x);
a[tot++]=num;
}
}
}
void Pushup(int node)
{
if(dep[a[tree[*node].ans]]>=dep[a[tree[*node+].ans]])tree[node].ans=tree[*node+].ans;
else tree[node].ans=tree[*node].ans;
}//tree[node].ans为数组里的lca的位置;
void build(int node,int L,int R)
{
tree[node].l=L;
tree[node].r=R;
if(L>=R)
{
tree[node].ans=L;
return ;
}
int mid=(L+R)>>;
build(*node,L,mid);
build(*node+,mid+,R);
Pushup(node);
}
int query(int node,int L,int R)
{
if(L<=tree[node].l&&R>=tree[node].r)return tree[node].ans;
int mid=(tree[node].l+tree[node].r)>>;
if(R<=mid)return query(*node,L,R);
else if(L>mid)return query(*node+,L,R);
else
{
int pos1=query(*node,L,mid),pos2=query(*node+,mid+,R);
if(dep[a[pos1]]>=dep[a[pos2]])return pos2;
else return pos1;
}
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
ve[i].clear();
}
for(int i=;i<n;i++)
{
scanf("%d%d%d",&u[i],&v[i],&w[i]);
ve[u[i]].push_back(v[i]);
ve[v[i]].push_back(u[i]);
}
memset(vis,,sizeof(vis));
bfs();
for(int i=;i<n;i++)
{
if(fa[u[i]]==v[i])
{
d[u[i]]=w[i];
}
else
{
d[v[i]]=w[i];
}
}
tot=;
dfs();
build(,,tot-);
int ql,qr;
for(int i=;i<m;i++)
{
scanf("%d%d",&ql,&qr);
int pos;
if(first[ql]<=first[qr])
pos=query(,first[ql],first[qr]);
else pos=query(,first[qr],first[ql]);
printf("%d\n",dis[ql]-dis[a[pos]]-dis[a[pos]]+dis[qr]);
}
}
return ;
}
 

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. Mybatis无法扫描到mapper.xml文件

    在Mybatis中默认扫描与mapper包同路径下的xml,resource文件的文件夹名称不能一次性创建,如com.baidu.mapper需要创建3次 这里如果是idea开发工具,一次创建与分开创 ...

  2. ArcMap中使用ArcPy实现Geometry与WKT的相互转换

    在Web GIS迅猛发展的今天,使用浏览器来进行交互以其方便性.快捷性被广大用户所推崇,那么在传输格式方面,都已比較简单的JSON或者WKT来解决网络带宽带来的数据压力. 在ArcGIS10.2版本号 ...

  3. gulp 静态资源版本控制

    package.json { "name": "gulp", "version": "0.0.1", "des ...

  4. Kindeditor 修改内容时如何不让&nbsp;及 <> 被自动转义

    $html = str_replace(' ', '&nbsp;', $html); $html = str_replace('>', '&gt;', $html); $html ...

  5. Linux系统调用及用户编程接口(API)

    系统调用 所谓系统调用是指操作系统提供给用户程序调用的一组"特殊"接口,用户程序能够通过这组"特殊"接口来获得操作系统内核提供的服务.比如用户能够通过进程控制相 ...

  6. 错误Log日志的收集

    1.在Application里面初始化 AppCrashHandler.getInstance(this); 2.创建一个类 package com.lvshandian.partylive.util ...

  7. vue-cli3.0升级失败,vue-cli卸载不掉,vue-cli升级不了3.0

    https://juejin.im/post/5bf7d67c51882518805acb1a vue-cli3.0 使用图形化界面创建和管理项目

  8. Mybatis之增删改查操作

    准备工作 建立整体项目目录 新建一个java工程,创建如下工程目录 其中com.kang.pojo中存放pojo类,com.kang.test中存放测试类. 源码目录config中存放Mybatis的 ...

  9. 关于BlockingQueue

    1 什么是BlockingQueue 2 BlockingQueue有什么用 3 ArrayBlockingQueue的用途 1 它是一个线程安全的队列 2 它是一个容量固定的队列 3 它为什么叫bl ...

  10. babylon

    https://github.com/babel/babel babylon - npm https://www.npmjs.com/package/babylon Babel is a compil ...