这道题与之前那两道模板题不同的是,路径有了权值,而且边是双向的,root已经给出来了,就是1,(这个地方如果还按之前那样来计算入度是会出错的。数据里会出现多个root。。。数据地址可以在poj的discuss板块看到)。两个节点之间的距离,可以这样处理:先处理出每个节点i到根的距离dist[i],则节点a,b之间的距离就是dist[a]+dist[b]-2*dist[LCA(a,b)],或者是在LCA的过程中加一个形式变量来传递距离值(目测这样效率会更高)。我一开始是想的仅传递每层的距离,具体怎样记不清了,结果样例就华丽丽地wa了。个人认为这个题目描述真心不爽。最后那个方向字符在这个题中没用。

#include<cstdio>
#include<vector>
#include<string>
//sba,just predeal the distance between every node and the root.and the dist[u][v]=dist[u][root]+dist[v][root]-2*dist[x][root]
using namespace std;
;
;
;
struct node{
    int v,dis;
    node(){v=;dis=;}
};
int ansque[MAXQUERY];
int father[MAXN];//i's ancestor and the distance between the son and the ancestor
vector<node>map[MAXN];
vector<node>query[MAXN];
int dist[MAXN];//i -->root
bool visit[MAXN],visit2[MAXN];
int getfather(int v){
    if(father[v]==v)return v;
    return father[v]=getfather(father[v]);
}
void aunion(int u,int v){
    int fu=father[u],fv=father[v],di;
    father[fv]=fu;
}
void LCA(int id,int distance){
    int len=map[id].size();
    int son;
    visit2[id]=;
    dist[id]=distance;
    ;i<len;i++){
        son=map[id][i].v;
        if(!visit2[son]){
            LCA(son,distance+map[id][i].dis);
            aunion(id,son);
        }

    }
    visit[id]=true;
    len=query[id].size();
    ;i<len;i++){
        son=query[id][i].v;
        if(visit[son]){
            ansque[query[id][i].dis]=dist[id]+dist[son]-*dist[father[getfather(son)]];
            //mark
        }
    }
}
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){//attention
        //at the begining,we'd better to initialize all the vars
        int x,y,l;
        char a;
        node b;
        ;i<=n;i++){
            map[i].clear();//the mothod of the initialization of queue
            query[i].clear();
            father[i]=i;
            visit[i]=;
            visit2[i]=;
            ansque[i]=;
            dist[i]=;
        }

        while(m--){
            scanf("%d %d %d %c",&x,&y,&l,&a);//only father
            b.v=y;b.dis=l;
            map[x].push_back(b);
            b.v=x;
            map[y].push_back(b);
        }
        scanf("%d",&m);
        node tmp2;
        ;i<m;i++){
            scanf("%d%d",&x,&y);
            tmp2.v=y;tmp2.dis=i;
            query[x].push_back(tmp2);
            tmp2.v=x;
            query[y].push_back(tmp2);
        }
        LCA(,);
        ;i<m;i++)
            printf("%d\n",ansque[i]);
    }

    ;
}

POJ1986 DistanceQueries 最近公共祖先LCA 离线算法Tarjan的更多相关文章

  1. LCA(最近公共祖先)离线算法Tarjan+并查集

    本文来自:http://www.cnblogs.com/Findxiaoxun/p/3428516.html 写得很好,一看就懂了. 在这里就复制了一份. LCA问题: 给出一棵有根树T,对于任意两个 ...

  2. 近期公共祖先(LCA)——离线Tarjan算法+并查集优化

    一. 离线Tarjan算法 LCA问题(lowest common ancestors):在一个有根树T中.两个节点和 e&sig=3136f1d5fcf75709d9ac882bd8cfe0 ...

  3. 最近公共祖先 LCA 倍增算法

          树上倍增求LCA LCA指的是最近公共祖先(Least Common Ancestors),如下图所示: 4和5的LCA就是2 那怎么求呢?最粗暴的方法就是先dfs一次,处理出每个点的深度 ...

  4. LCA离线算法Tarjan详解

    离线算法也就是需要先把所有查询给保存下来,最后一次输出结果. 离线算法是基于并查集实现的,首先就是初始化P[i] = i. 接下来对于每个点进行dfs: ①首先判断是否有与该点有关的查询,如果当前该点 ...

  5. LCA离线算法Tarjan的模板

    hdu 2586:题意:输入n个点的n-1条边的树,m组询问任意点 a b之间的最短距离 思路:LCA中的Tarjan算法,RMQ还不会.. #include <stdio.h> #inc ...

  6. 距离LCA离线算法Tarjan + dfs + 并查集

    距离B - Distance in the Tree 还是普通的LCA但是要求的是两个节点之间的距离,学到了一些 一开始我想用带权并查集进行优化,但是LCA合并的过程晚于离线计算的过程,所以路径长度会 ...

  7. HDU 2874 LCA离线算法 tarjan算法

    给出N个点,M条边.Q次询问 Q次询问每两点之间的最短距离 典型LCA 问题   Marjan算法解 #include "stdio.h" #include "strin ...

  8. LCA(最近公共祖先)——离线 Tarjan 算法

    tarjan算法的步骤是(当dfs到节点u时):1 在并查集中建立仅有u的集合,设置该集合的祖先为u1 对u的每个孩子v:   1.1 tarjan之   1.2 合并v到父节点u的集合,确保集合的祖 ...

  9. [模板] 最近公共祖先/lca

    简介 最近公共祖先 \(lca(a,b)\) 指的是a到根的路径和b到n的路径的深度最大的公共点. 定理. 以 \(r\) 为根的树上的路径 \((a,b) = (r,a) + (r,b) - 2 * ...

随机推荐

  1. 两个Activity之间的交互startActivityForResult的使用

    代码如下: package com.zzw.teststartintentforrequest; import android.app.Activity; import android.content ...

  2. 比较不错的JS 曲线图

    fashion chart   falsh文件支持,无需考虑兼容 Highcharts(纯JS,很漂亮 效果很好) Highcharts是一个制作图表的纯Javascript类库,主要特性如下: 兼容 ...

  3. c#中的类型转换

    Parse类型转换 Parse()函数 int.double都能调用Parse()函数,Parse(string str);如果转换成功就成功,失败就会抛出一个异常; TryParse()函数 相应地 ...

  4. 2.opencv图像处理常用操作

    图像的平滑处理 平滑,也称 模糊, 平滑处理时需要用到一个滤波器 .滤波器想象成一个包含加权系数的窗口,这个加权系数也叫做核或者模版. // 图像平滑处理分而学之.cpp : 定义控制台应用程序的入口 ...

  5. 关于WP8 微信分享的补充说明

    1.根据微信官方Demo完成相应功能. 2.在分享完后,从微信回来,需要进行 快速恢复. 3.在快速恢复中加入 RootFrame.Navigating += HandlerFotResetNavig ...

  6. 如何在mysql中退出当前窗口界面而不关闭窗口的方法

    CTRL+Cexit;quit ;

  7. LintCode-Implement Iterator of Binary Search Tree

    Design an iterator over a binary search tree with the following properties: Elements are visited in ...

  8. LintCode-Search 2D Matrix II

    Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of ...

  9. Maven实站读后感

    这本书是一本非常经典的Maven教程,通俗易懂,同时介绍的东西十分实用,在工作上都能用到. 以前在公司里面需要要问同时的有关的Maven的问题,都可以自己解决了. 除了最基本的,以后自己可能要用到的: ...

  10. html5音频、视频

    1.插入一个视频:<video src="test.webm" width="800" height="600"></vi ...