用的离线算法Tarjan

该算法的详细解释请戳

http://www.cnblogs.com/Findxiaoxun/p/3428516.html

做这个题的时候,直接把1470的代码copy过来,改了改输入输出。这个的难度比那个低。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
;
int father[MAXN],ancestor[MAXN];
bool visit[MAXN];
int ans[MAXN];
vector<int> map[MAXN];//save the tree
int n,t,root,sx,sy;
bool indegree[MAXN];//the indegree to find the root
int getfather(int v){//path compression
    if(father[v]==v)return v;
    return father[v]=getfather(father[v]);
}
void aunion(int u,int v){
    int fv=getfather(v),fu=getfather(u);
    father[fv]=fu;
}
bool isit(int a,int b){
    if(a==sx&&b==sy)return true;
    return false;
}
void LCA(int id){
    int len=map[id].size();
    int son;
    ;i<len;i++){
        son=map[id][i];
        LCA(son);
        aunion(id,son);
    }
    visit[id]=;
    if(visit[sy]&&id==sx){
        printf("%d\n",father[getfather(sy)]);
        return;
    }else{//attention:this way
        if(visit[sx]&&id==sy){
            printf("%d\n",father[getfather(sx)]);
            return;
        }
    }

}
void init(){
    int x,y,z;
    scanf("%d",&n);
    //initialize all the vars
    ;i<=n;i++){
        map[i].clear();
    }
    memset(visit,,sizeof(visit));
    memset(ans,,sizeof(ans));
    memset(indegree,,sizeof(indegree));
    ;i<=n;i++)father[i]=i;
    ;i<n-;i++){
        scanf("%d%d",&x,&y);
        indegree[y]=;
        map[x].push_back(y);
    }
    scanf("%d%d",&sx,&sy);
    ;i<=n;i++)if(!indegree[i])root=i;//find the root;warning:the 0
}
int main(){
    int  t;
    scanf("%d",&t);
    while(t--){
        init();
        LCA(root);
    }
    ;
}

POJ1330Nearest Common Ancestors最近公共祖先LCA问题的更多相关文章

  1. POJ1330Nearest Common Ancestors——近期公共祖先(离线Tarjan)

    http://poj.org/problem? id=1330 给一个有根树,一个查询节点(u,v)的近期公共祖先 836K 16MS #include<iostream> #includ ...

  2. POJ 1330 Nearest Common Ancestors (最近公共祖先LCA + 详解博客)

    LCA问题的tarjan解法模板 LCA问题 详细 1.二叉搜索树上找两个节点LCA public int query(Node t, Node u, Node v) { int left = u.v ...

  3. POJ1470Closest Common Ancestors 最近公共祖先LCA 的 离线算法 Tarjan

    该算法的详细解释请戳: http://www.cnblogs.com/Findxiaoxun/p/3428516.html #include<cstdio> #include<alg ...

  4. 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18136   Accept ...

  5. POJ1330 Nearest Common Ancestors(最近公共祖先)(tarjin)

    A - Nearest Common Ancestors Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld &am ...

  6. POJ - 1330 Nearest Common Ancestors 最近公共祖先+链式前向星 模板题

    A rooted tree is a well-known data structure in computer science and engineering. An example is show ...

  7. POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)

    POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...

  8. POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)

    POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...

  9. 最近公共祖先LCA(前置知识)

    1.前言 最近公共祖先(Least Common Ancestors),简称LCA,是由Tarjan教授(对,又是他)提出的一种在有根树中,找出某两个结点u和v最近的公共祖先问题. 2.什么是最近公共 ...

随机推荐

  1. (转载) STL中map用法详解

    Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候 ...

  2. Photoshop学习笔记(1)--界面设置

    根据慕课网教学视频整理(抄了一个妹子的评论): http://www.imooc.com/video/9813 ps版本:CS6 步骤: 1.视图->显示->智能参考线,以及视图-> ...

  3. 【pywin32总结】

    #下面是必备的#注意!所有方法后面都要加括号()!!! import win32com from win32com.client import Dispatch,constants w = win32 ...

  4. Oracle复习(有记录才能沉淀.......)

    一.Oracle准备工作 1.安装Oracle Oracle数据库产品是免费的,我们可以从Oracle的官方网站(http://www.oracle.com)下载到程序安装包,Oracle在Windo ...

  5. PHP——面向对象

    <body> <?php //类调用用:: //对象调用用-> class Dog1//定义类 { //成员属性(变量) var $name; var $age; var $p ...

  6. 解决异常:Package should contain a content type part [M1.13]

    http://blog.csdn.net/llwan/article/details/8890190 ————————————————————————————————————————————————— ...

  7. 因此mybatis最好与spring集成起来使用

    单独使用mybatis是有很多限制的(比如无法实现跨越多个session的事务),而且很多业务系统本来就是使用spring来管理的事务,因此mybatis最好与spring集成起来使用. spring ...

  8. 设置两个WdatePicker的开始时间小于结束时间,结束时间大于开始时间

    contract_start_date_id为开始时间ID contract_end_date_id为结束时间ID $("#contract_start_date_id").bin ...

  9. kotlin正式由Goole公布为Android的最新开发语言

    那么,现在大家开发Android的话一般来说都是直接用Java,这个没错吧(高手除外).嗯,那么用力那么久的Java,不知道大家是否有想过Java的不足,已经很多可以优化的地方呢.当然,新修订的版本中 ...

  10. Popwindow系列

    一系列干货等你来拿 关于我:http://www.cnblogs.com/dubo-/ 项目中也有很多实用的事例,等待录入中...