用的离线算法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. android圆形图像

    在网上找了一下,最简单的是利用canvas的setXfermode,来控制图片重合部分的显示策略. 图片混合时,先画的是dst,后画的是src,各种混合的方式如下,其中圆形是dst,正方形是src: ...

  2. windows下安装配置apacheserver

    注:一開始公布的时候 图片是复制粘贴的.所以公布完图片所有消失了...如今是补发图片. . .2016/04/25 1.进入apache官网  http://httpd.apache.org/ 这里我 ...

  3. Netty中的那些坑

    Netty中的那些坑(上篇) 最近开发了一个纯异步的redis客户端,算是比较深入的使用了一把netty.在使用过程中一边优化,一边解决各种坑.儿这些坑大部分基本上是Netty4对Netty3的改进部 ...

  4. javax.Servlet的包中,属于类的是。(选择1项)

    javax.Servlet的包中,属于类的是.(选择1项) A.Servlet B.GenericServlet C.ServletRequest D.ServletContext 解答:B Serv ...

  5. UP与瀑布模型

    软件开发过程(Software development process)描述了构造.部署以及维护软件的方式.(就是先做什么后做什么)统一过程(The Unified Software Developm ...

  6. iframe超时处理。。。。

    function iframeTimeOut(url, timeOut_callback, width, height) { /// <summary> /// iframe超时处理 // ...

  7. strust2的Action中validateXxx方法的用法

    Struts2控制部分时常需要验证来自页面的信息是否合法,若在执行struts2中 public String Xxx()方法操作数据库之前需要验证,ActionSupport提供了一个很好的方法.X ...

  8. Windows下基于eclipse的Spark应用开发环境搭建

    原创文章,转载请注明: 转载自www.cnblogs.com/tovin/p/3822985.html 一.软件下载 maven下载安装 :http://10.100.209.243/share/so ...

  9. 图像,script,link 空地址带来的困惑

    前端开发人员估计很多人都不清楚这些标签的空地址会带来哪些让人意想不到的问题.包括我,很长一段时间都认为这是无伤大雅的事情,直到有一次遇到莫名其妙的问题,下面会拿image来详细介绍(其他类似). 下面 ...

  10. 让所有IE支持HTML5的解决方案

    自从HTML5能为我们的新网页带来更高效洁净的代码而得到更多的关注,然而唯一能让IE识别那些新元素(如<article>)的途径是使用HTML5 shiv,感谢remy sharp为我们提 ...