题目链接:

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. 目标主体名称不正确,无法生成 SSPI 上下文。

    参考地址:http://blog.csdn.net/burgess_liu/article/details/18300959 两个命令:setspn -L Server03 和 setspn -D S ...

  2. .NET面试题汇总

    目录 本次给大家介绍的是我收集以及自己个人保存一些.NET面试题 简介 1.C# 值类型和引用类型的区别 2.如何使得一个类型可以在foreach 语句中使用 3.sealed修饰的类有什么特点 4. ...

  3. LVS-DR,real-server为windows 2008的配置

    LVS-DR,real-server为windows 2008的配置 部署邮件系统负载均衡,采用LVS-DR模式,调度器是一台centos 5.8,real-server是两台windows2008, ...

  4. Ajax请求的跨域(CORS)问题

    用浏览器,通过XHR(XMLHttpRequest)请求向另外一个域名请求数据时.会碰到跨域(CORS)问题. CORS:Cross-Origin Resource Sharing 什么是跨域? 简单 ...

  5. GIT简单使用——私人库篇

    1.生成公钥公钥是远程库识别您的用户身份的一种认证方式,通过公钥,您可以将本地git项目与远程库建立联系,然后您就可以很方便的将本地代码上传到远程库,或者将远程库代码下载到本地了.$ ssh-keyg ...

  6. OS开发之旅之App的生命周期【转载】

    原文链接 http://www.360doc.com/content/15/0918/14/27799428_499912639.shtml 在iOS App中,入口函数并不在根目录下,而是在“Sup ...

  7. hadoop修改主机名遇到的坑

    正确的修改方式 CentOS修改主机名(hostname) 需要修改两处:一处是/etc/sysconfig/network,另一处是/etc/hosts,只修改任一处会导致系统启动异常.首先切换到r ...

  8. 献给写作者的 Markdown 新手指南及语法

    烈推荐所有写作者学习和掌握该语言.为什么?可以参考: 『为什么作家应该用 Markdown 保存自己的文稿』. 『Markdown写作浅谈』 让你专注于文字而不是排版. 标题 只需要在文本前面加上 # ...

  9. 【转】基于eclipse进行ndk开发的环境配置

    前述虽然我们在其他的博文中(如https://blog.csdn.net/ericbar/article/details/76602720),早就用到了ndk,但如果想在Android设备运行包含这些 ...

  10. VI使用说明 (转)

    vi使用方法(ZT)         vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令.由于对Unix及Lin ...