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行是边的信息,两端点和权,后面 ...
随机推荐
- 实习培训——Servlet(6)
实习培训——Servlet(6) 1 Servlet 客户端 HTTP 请求 当浏览器请求网页时,它会向 Web 服务器发送特定信息,这些信息不能被直接读取,因为这些信息是作为 HTTP 请求的头的 ...
- categoriy 重写函数会怎样?
From comp.lang.objective-C FAQ listing: "What if multiple categories implement the same method? ...
- liferay笑傲江湖-API之参数的工具类(ParamUtil)
public class ParamUtil { 036 037 public static boolean get( 038 HttpServletRequest request, String p ...
- C#中NPOI操作excel之读取和写入excel数据
一.下载引用 下载需要引用的dll,即:NPOI.dll,NPOI.OOXML.dll,NPOI.OpenXml4Net.dll,ICSharpCode.SharpZipLib.dll(office2 ...
- mysql日志详解
日志分类: 一.错误日志. 1.在配置文件中的配置是:log-error="DESKTOP-igoodful.err",查看参数的键值对:show variables like ' ...
- centos6更改密码
创建新用户 创建一个用户名为:zhangbiao [root@localhost ~]# adduser zhangbiao 为这个用户初始化密码,linux会判断密码复杂度,不过可以强行忽略: [r ...
- transition过度效果 + transform旋转360度
css样式: .animate{ width:65px; height:40px; background:#92B901; color:#ffffff; position:absolute; font ...
- bzoj2733: [HNOI2012]永无乡 启发式合并
地址:http://www.lydsy.com/JudgeOnline/problem.php?id=2733 题目: 2733: [HNOI2012]永无乡 Time Limit: 10 Sec ...
- POJ 分类
初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. ( ...
- zookeeper 安装以及集群搭建
安装环境: jdk1.7 zookeeper-3.4.10.tar.gz VM虚拟机redhat6.5-x64:192.168.1.200 192.168.1.201 192.168.1.202 ...