http://poj.org/problem?id=1986

题意:给出一棵n个点m条边的树,还有q个询问,求树上两点的距离。

思路:这次学了一下倍增算法求LCA。模板。

dp[i][j]代表第i个点的第2^j个祖先是哪个点,dp[i][0] = i的第一个祖先 = fa[i]。转移方程:dp[i][j] = dp[dp[i][j-1][j-1]。

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define N 100010
struct Edge {
int v, nxt, w;
Edge () {}
Edge (int v, int nxt, int w) : v(v), nxt(nxt), w(w) {}
} edge[N];
int dp[N][], dep[N], dis[N], fa[N], head[N], tot, n; void Add(int u, int v, int w) {
edge[tot] = Edge(v, head[u], w); head[u] = tot++;
edge[tot] = Edge(u, head[v], w); head[v] = tot++;
} void DFS(int u) {
dp[u][] = fa[u];
for(int i = ; i <= ; i++) // 转移
dp[u][i] = dp[dp[u][i-]][i-];
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if(v == fa[u]) continue;
fa[v] = u;
dep[v] = dep[u] + ;
dis[v] = dis[u] + edge[i].w;
DFS(v);
}
} int LCA(int x, int y) {
if(dep[x] < dep[y]) swap(x, y); //设x为较深的点
for(int i = ; i >= ; i--) // 让x跑到和y同一深度
if(dep[dp[x][i]] >= dep[y]) x = dp[x][i];
if(x == y) return x;
for(int i = ; i >= ; i--) // x和y同时向上跑
if(dp[x][i] != dp[y][i])
x = dp[x][i], y = dp[y][i];
return dp[x][];
} int main() {
int m, q;
while(~scanf("%d%d", &n, &m)) {
memset(dp, , sizeof(dp));
memset(dis, , sizeof(dis));
memset(dep, , sizeof(dep));
memset(head, -, sizeof(head));
tot = ; char s[];
for(int i = ; i < m; i++) {
int u, v, w;
scanf("%d%d%d%s", &u, &v, &w, s);
Add(u, v, w);
}
fa[] = ; dis[] = dep[] = ;
DFS();
scanf("%d", &q);
while(q--) {
int u, v;
scanf("%d%d", &u, &v);
printf("%d\n", dis[u] + dis[v] - dis[LCA(u, v)] * );
}
}
return ;
}

POJ 1986:Distance Queries(倍增求LCA)的更多相关文章

  1. poj 1986 Distance Queries 带权lca 模版题

    Distance Queries   Description Farmer John's cows refused to run in his marathon since he chose a pa ...

  2. POJ.1986 Distance Queries ( LCA 倍增 )

    POJ.1986 Distance Queries ( LCA 倍增 ) 题意分析 给出一个N个点,M条边的信息(u,v,w),表示树上u-v有一条边,边权为w,接下来有k个询问,每个询问为(a,b) ...

  3. POJ 1986 Distance Queries LCA两点距离树

    标题来源:POJ 1986 Distance Queries 意甲冠军:给你一棵树 q第二次查询 每次你问两个点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + d ...

  4. POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)

    POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...

  5. POJ 1986 Distance Queries 【输入YY && LCA(Tarjan离线)】

    任意门:http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total ...

  6. POJ 1986 Distance Queries(Tarjan离线法求LCA)

    Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 12846   Accepted: 4552 ...

  7. poj 1986 Distance Queries LCA

    题目链接:http://poj.org/problem?id=1986 Farmer John's cows refused to run in his marathon since he chose ...

  8. 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 ...

  9. POJ 1986 Distance Queries (Tarjan算法求最近公共祖先)

    题目链接 Description Farmer John's cows refused to run in his marathon since he chose a path much too lo ...

  10. 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 ...

随机推荐

  1. “Rsync” could not be found on your PATH

    Vagrant with VirtualBox on Windows10: “Rsync” could not be found on your PATH 使用agrant安装系统时,遇到的错误提示: ...

  2. ORM 集合

    1.EF   https://github.com/aspnet 2.Chloe.ORM http://www.cnblogs.com/so9527/p/5809089.html http://www ...

  3. delphi中获取memo鼠标所在位置的行和列(通过EM_GETRECT消息取得Rect后,自己算一下)

    也是看别人写的,但是不容易找到,就转发一篇delphi版本的 function GetLine(X, Y: integer): TPoint;var  OldFont : HFont;  Hand : ...

  4. jquery 鼠标经过延时触发事件,jquery插件

    jquery 鼠标经过延时触发事件. 用来做:鼠标经过选项卡,延时切换,鼠标经过商城分类延时显示,最好不过,防止用户随便滑动就切换了选项卡. 一.引入jq 二.加入以下插件代码 /* * 鼠标经过延时 ...

  5. wpf 事件参数 绑定到viewmdoel

    public sealed class EventCommand : TriggerAction<DependencyObject> { public static readonly De ...

  6. 图像滤镜艺术---(Lightleaks Filter)漏光滤镜

    原文:图像滤镜艺术---(Lightleaks Filter)漏光滤镜 (Lightleaks Filter)漏光滤镜 漏光拍摄其实就是一种摄影手法,最初是因为强烈光照导致相片交卷的过分曝光,最终在成 ...

  7. Win8 Metro(C#)数字图像处理--2.50图像运动模糊

    原文:Win8 Metro(C#)数字图像处理--2.50图像运动模糊  [函数名称] 图像运动模糊算法    MotionblurProcess(WriteableBitmap src,int  ...

  8. GIS基础软件及操作(八)

    原文 GIS基础软件及操作(八) 练习八.地理建模 地理建模:Model Builder 土壤侵蚀危险性建模分析 认识ModelBuilder操作界面 1: 添加硬盘上的数据或工具到模型中,数据也可以 ...

  9. 遍历所有的XML

    XmlElement rootElement = doc.DocumentElement; foreach (XmlElement childElement in rootElement) { //C ...

  10. Bigtable:结构化数据的分布式存储系统

    Bigtable最初是谷歌设计用来存储大规模结构化数据的分布式系统,其可以在数以千计的商用服务器上存储高达PB级别的数据量.开源社区根据Bigtable的设计思路开发了HBase.其优势在于提供了高效 ...