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行是边的信息,两端点和权,后面 ...
随机推荐
- 评价指标的局限性、ROC曲线、余弦距离、A/B测试、模型评估的方法、超参数调优、过拟合与欠拟合
1.评价指标的局限性 问题1 准确性的局限性 准确率是分类问题中最简单也是最直观的评价指标,但存在明显的缺陷.比如,当负样本占99%时,分类器把所有样本都预测为负样本也可以获得99%的准确率.所以,当 ...
- Lintcode: Kth Prime Number (Original Name: Ugly Number)
Ugly number is a number that only have factors 3, 5 and 7. Design an algorithm to find the kth numbe ...
- QPixmap 显示大小
size picSize(600,400); //将pixmap缩放成picSize大小然后保存在scaledPixmap中 按比例缩放: QPixmap scaledPixmap = pixmap. ...
- CXF创建webservice客户端和服务端
转 一.CXF的介绍 Apache CXF是一个开源的WebService框架,CXF大大简化了Webservice的创建,同时它继承了XFire的传统,一样可以和spring天然的进行无缝的集成.C ...
- Java互联网架构-Mysql分库分表订单生成系统实战分析
概述 分库分表的必要性 首先我们来了解一下为什么要做分库分表.在我们的业务(web应用)中,关系型数据库本身比较容易成为系统性能瓶颈,单机存储容量.连接数.处理能力等都很有限,数据库本身的“有状态性” ...
- JavaScript: apply 方法 详解(转)——非常好
转载自 http://www.cnblogs.com/KeenLeung/archive/2012/11/19/2778229.html 我在一开始看到javascript的函数apply和call ...
- vue 渲染页面的时候 出现闪烁问题的解决办法
在使用vue绑定数据的时候,渲染页面时会出现变量闪烁 <div id="h_cameraman" v-cloak> <public-nav> {{ msg ...
- Java8函数接口实现回调及Groovy闭包的代码示例
本文适用于想要了解Java8 Function接口编程及闭包表达式的筒鞋. 概述 在实际开发中,常常遇到使用模板模式的场景: 主体流程是不变的,变的只是其中要调用的具体方法. 其特征是: Begi ...
- Java性能优化——HashCode的使用
背景 告警子系统监控4万个大网元所有端口的某些指标数据,根据阈值配置判断是否产生告警.采集——数据处理子系统每5分钟会主动采集24万次数据,发送24万条消息给告警子系统,这24万条消息涉及100万实体 ...
- python中的对象(三)
一.python对象 python使用对象模型来存储数据.构造任何类型的值都是一个对象. 所有python对象都拥有三个特性:身份.类型.值 身份:每个对象都有一个唯一的身份标识自己,任何对象的身份可 ...