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行是边的信息,两端点和权,后面 ...
随机推荐
- 【Cocos2dx 3.3 Lua】SpriteBatchNode和SpriteFrameCache使用
精灵帧缓存类 一.SpriteFrameCache 精灵帧缓冲类SpriteFrameCache用于存储精灵帧,SpriteFrameCache是一个单例模式,不属于某一个精灵,是所有精灵共享 ...
- Appium-Python-Client安装
官网是这个:https://pypi.org/project/Appium-Python-Client/#files 下载下来后是这样的文件不知道怎么安装: 不用下载,直接这样就可以 了~~ 安装后导 ...
- [LeetCode] 513. Find Bottom Left Tree Value_ Medium tag: BFS
Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...
- Javascript--普通函数调用-涨工资计算函数
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Javascript-涨工资案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- app的底部菜单设计
一.个人看法. 1.一般都是四个菜单或者五个菜单,这个是绝对主流,我估计占比达99%.当然也有三个菜单图标的,也有零个菜单图标的 2.如果该app软件功能复杂,那么尽量选择5个图标布局.比如苹果app ...
- bind,live,delegate
.live() 到目前为止,一切似乎很完美.可惜,事实并非如此.因为.live()方法并不完美,它有如下几个主要缺点: $()函数会找到当前页面中的所有td元素并创建jQuery对象,但在确认事件目标 ...
- Java求解迷宫问题:栈与回溯算法
摘要: 使用栈的数据结构及相应的回溯算法实现迷宫创建及求解,带点JavaGUI 的基础知识. 难度: 中级 迷宫问题是栈的典型应用,栈通常也与回溯算法连用. 回溯算法的基本描述是: (1) 选择一个 ...
- javascript中父、子页面间调用
本文主要转自:http://www.360doc.com/content/11/0525/17/6161903_119333834.shtml http://zh ...
- MD5验签同一字符串得到不同的MD5签名值可能问题之一
public static String md555(String plainText) throws UnsupportedEncodingException { byte[] secretByte ...