用的离线算法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. Makefile 10——打造更专业的编译环境-huge项目

    先手工创建几个文件目录: 接下来先创建code/foo/src目录下的Makefile: .PHONY: all clean MKDIR = mkdir RM = rm RMFLAGS = -rf C ...

  2. vue中axios调用接口和用node.js跨域

    <script>const API_PROXY = 'https://bird.ioliu.cn/v1/?url='import axios from 'axios'export defa ...

  3. echarts报表

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...

  4. C语言 · 图形输出

    算法提高 图形输出   时间限制:1.0s   内存限制:512.0MB      编写一程序,在屏幕上输出如下内容: X | X | X ---+---+--- | | ---+---+--- O ...

  5. C语言 · 排列数

    算法提高 排列数   时间限制:1.0s   内存限制:256.0MB      问题描述 0.1.2三个数字的全排列有六种,按照字母序排列如下: 012.021.102.120.201.210 输入 ...

  6. Android——Activity恢复用户用EditText输入的数据

    说明: 在横屏输入的内容,在Activity销毁后,即横屏后,获取用户输入的内容 步骤: 1.在xml页面定义EditText的id 2.用onSaveInstanceState保存用户输入的数据 ( ...

  7. ZooKeepr日志清理(转)

    转载请用注明:@ni掌柜 nileader@gmail.com 在使用zookeeper过程中,我们知道,会有dataDir和dataLogDir两个目录,分别用于snapshot和事务日志的输出(默 ...

  8. jQuery无刷新分页完整实例代码

    在线演示地址如下: http://demo.jb51.net/js/2015/jquery-wsx-page-style-demo/ <!DOCTYPE html> <head> ...

  9. 原生Orcale数据库连接

    package tj.test.demo; import java.sql.Connection;import java.sql.DriverManager;import java.sql.Prepa ...

  10. 京东阅读PDF导出

    适用平台:windows 需要软件:FastStone Capture(截图软件),TinyTask(操作录制软件) 1.打开京东阅读 2.设置截图软件 (1)设置截图区域(FastStone Cap ...