poj 1986 Distance Queries 带权lca 模版题
Description
Input
* Line 2+M: A single integer, K. 1 <= K <= 10,000
* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms.
Output
Sample Input
7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6
1 4
2 6
Sample Output
13
3
36
Hint
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#define true ture
#define false flase
using namespace std;
#define ll long long
#define inf 0xfffffff
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
#define maxn 100010
#define M 22
struct is
{
int v,next,w;
} edge[maxn*];
int deep[maxn],jiedge;
int dis[maxn];
int head[maxn];
int rudu[maxn];
int fa[maxn][M];
void add(int u,int v,int w)
{
jiedge++;
edge[jiedge].v=v;
edge[jiedge].w=w;
edge[jiedge].next=head[u];
head[u]=jiedge;
}
void dfs(int u)
{
for(int i=head[u]; i; i=edge[i].next)
{
int v=edge[i].v;
int w=edge[i].w;
if(!deep[v])
{
dis[v]=dis[u]+edge[i].w;
deep[v]=deep[u]+;
fa[v][]=u;
dfs(v);
}
}
}
void st(int n)
{
for(int j=; j<M; j++)
for(int i=; i<=n; i++)
fa[i][j]=fa[fa[i][j-]][j-];
}
int LCA(int u , int v)
{
if(deep[u] < deep[v]) swap(u , v) ;
int d = deep[u] - deep[v] ;
int i ;
for(i = ; i < M ; i ++)
{
if( ( << i) & d ) // 注意此处,动手模拟一下,就会明白的
{
u = fa[u][i] ;
}
}
if(u == v) return u ;
for(i = M - ; i >= ; i --)
{
if(fa[u][i] != fa[v][i])
{
u = fa[u][i] ;
v = fa[v][i] ;
}
}
u = fa[u][] ;
return u ;
}
void init()
{
memset(head,,sizeof(head));
memset(fa,,sizeof(fa));
memset(rudu,,sizeof(rudu));
memset(deep,,sizeof(deep));
jiedge=;
}
int main()
{
int x,n,t;
while(~scanf("%d%d",&n,&x))
{
init(); for(int i=; i<x; i++)
{
char a[];
int u,v,w;
scanf("%d%d%d %s",&u,&v,&w,a);
add(u,v,w);
add(v,u,w);//双向可以从任意点开始,并且避免有环
}
deep[]=;
dis[]=;
dfs();
st(n);
scanf("%d",&t);
while(t--)
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",dis[a]-*dis[LCA(a,b)]+dis[b]);
}
}
return ;
}
poj 1986 Distance Queries 带权lca 模版题的更多相关文章
- POJ.1986 Distance Queries ( LCA 倍增 )
POJ.1986 Distance Queries ( LCA 倍增 ) 题意分析 给出一个N个点,M条边的信息(u,v,w),表示树上u-v有一条边,边权为w,接下来有k个询问,每个询问为(a,b) ...
- POJ 1986 Distance Queries LCA两点距离树
标题来源:POJ 1986 Distance Queries 意甲冠军:给你一棵树 q第二次查询 每次你问两个点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + d ...
- POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)
POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...
- POJ 1986 Distance Queries 【输入YY && LCA(Tarjan离线)】
任意门:http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total ...
- POJ 1986 Distance Queries(Tarjan离线法求LCA)
Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 12846 Accepted: 4552 ...
- poj 1986 Distance Queries LCA
题目链接:http://poj.org/problem?id=1986 Farmer John's cows refused to run in his marathon since he chose ...
- POJ 1986 - Distance Queries - [LCA模板题][Tarjan-LCA算法]
题目链接:http://poj.org/problem?id=1986 Description Farmer John's cows refused to run in his marathon si ...
- poj 1986 Distance Queries(LCA)
Description Farmer John's cows refused to run in his marathon since he chose a path much too long fo ...
- POJ 1986 Distance Queries(LCA Tarjan法)
Distance Queries [题目链接]Distance Queries [题目类型]LCA Tarjan法 &题意: 输入n和m,表示n个点m条边,下面m行是边的信息,两端点和权,后面 ...
随机推荐
- bootstrap模态框手动开启关闭与设置点击外部不关闭
http://www.cnblogs.com/qlqwjy/p/7491054.html 完整的参考菜鸟教程:http://www.runoob.com/bootstrap/bootstrap-mod ...
- mac shell终端编辑命令行快捷键
Ctrl + d 删除一个字符,相当于通常的Delete键(命令行若无所有字符,则相当于exit:处理多行标准输入时也表示eof) Ctrl + h 退格删除一个字符,相当 ...
- 版本 ------- 2017年最受开发者欢迎的10个Linux发行版
1.Arch Linux Arch Linux在安装过程中提供了强大的可定制选择,支持你下载和安装自己所需的程序包.虽然这个选择对新手来说没有多大的帮助,但是它确实能够帮助那些使用Arch构建系统和存 ...
- CloudFlare Support - Error 522: Connection timed out 错误522:连接超时
522错误意味着我们无法在所有到达原点Web服务器. 这方面有几个主要原因: 原始服务器太超载回应. 源Web服务器具有挡住了我们的请求的防火墙,或者数据包被主机的网络内下降. 源Web服务器脱机,或 ...
- 3.keras实现-->高级的深度学习最佳实践
一.不用Sequential模型的解决方案:keras函数式API 1.多输入模型 简单的问答模型 输入:问题 + 文本片段 输出:回答(一个词) from keras.models import M ...
- jmeter Bean Shell的使用(一)
未经作者允许,禁止转载!!! Jmeter有哪些Bean Shell 定时器: BeanShell Timer 前置处理器:BeanShell PreProcessor 采样器: BeanShell ...
- Oracle业务用户密码过期问题的解决
实验环境:Oracle 11.2.0.4 如果DBA不知道业务用户密码,当业务密码过期,应用要求DBA帮忙重设为原来的密码. 1.查询业务用户密码 从user$查到hash加密过的值: select ...
- TraceSource记录程序日志
1.配置文件 <system.diagnostics> <sources> <source name="TraceError" switchValue ...
- typed.js
实现效果,文字逐个输出. 实例代码: <script> $(function(){ $("#head-title").typed({ strings: ["为 ...
- hdu6000 Wash ccpc-20162017-finals B Wash
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6000 题目: Wash Time Limit: 20000/10000 MS (Java/Ot ...