POJ1330Nearest Common Ancestors最近公共祖先LCA问题
用的离线算法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问题的更多相关文章
- POJ1330Nearest Common Ancestors——近期公共祖先(离线Tarjan)
		
http://poj.org/problem? id=1330 给一个有根树,一个查询节点(u,v)的近期公共祖先 836K 16MS #include<iostream> #includ ...
 - POJ 1330 Nearest Common Ancestors (最近公共祖先LCA + 详解博客)
		
LCA问题的tarjan解法模板 LCA问题 详细 1.二叉搜索树上找两个节点LCA public int query(Node t, Node u, Node v) { int left = u.v ...
 - POJ1470Closest Common Ancestors 最近公共祖先LCA 的 离线算法 Tarjan
		
该算法的详细解释请戳: http://www.cnblogs.com/Findxiaoxun/p/3428516.html #include<cstdio> #include<alg ...
 - 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)
		
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18136 Accept ...
 - POJ1330 Nearest Common Ancestors(最近公共祖先)(tarjin)
		
A - Nearest Common Ancestors Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld &am ...
 - POJ - 1330 Nearest Common Ancestors 最近公共祖先+链式前向星 模板题
		
A rooted tree is a well-known data structure in computer science and engineering. An example is show ...
 - POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)
		
POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...
 - POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)
		
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...
 - 最近公共祖先LCA(前置知识)
		
1.前言 最近公共祖先(Least Common Ancestors),简称LCA,是由Tarjan教授(对,又是他)提出的一种在有根树中,找出某两个结点u和v最近的公共祖先问题. 2.什么是最近公共 ...
 
随机推荐
- CodeForces 459D  Pashmak and Parmida's problem
			
Pashmak and Parmida's problem Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
 - Unicode Table
			
Unicode Table http://www.tamasoft.co.jp/en/general-info/unicode.html 00 01 02 03 04 05 06 07 08 09 ...
 - 一款基于jQuery的图片下滑切换焦点图插件
			
之前为大家分享了好多款jquery插件,今天我们要分享的一款jQuery插件也比较实用,是一款jQuery焦点图插件.焦点图相当普通,一共可以循环播放4张图片,并且每一张图片在切换的时候都是向下滑动的 ...
 - mysql学习笔记2--mysql的基本使用
			
4. 运行和关闭MySQL服务器 首先检查MySQL服务器正在运行与否.在资源管理器查看有没有mysqld的进程,如果MySQL正在运行,那么会看到列出来的 mysqld 进程.如果服务器没有运行,那 ...
 - scala,spark练习题提高
			
1.求每家公司有哪些产品 val arr3 = List("Apache" -> "Spark", "Apache" -> &q ...
 - Unix系统编程()信号处理器简介
			
信号处理器简介 信号处理器程序(也称为信号捕捉器)是当指定信号传递给进程时将会调用的一个函数.这里会学习信号处理器的基本原理,后面将继续学习. 调用信号处理器程序,可能会随时打断主程序流程:内核代表进 ...
 - [misc]如何在嵌入式平台使用printf功能
			
转自:http://www.cnblogs.com/liu_xf/archive/2011/04/14/2015726.html 摘要: 当我们在调试代码时,通常需要将程序中的某个变量打印至PC机上, ...
 - STUN协议简析
			
http://blog.csdn.net/mazidao2008/article/details/4934257 ——————————————————————————————————————————— ...
 - Ubuntu 16.04 更换阿里云源
			
Ubuntu 16.04 更换阿里云源sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak #备份sudo gedit /etc/apt/so ...
 - pydoc介绍
			
Ka-Ping Yee 曾创建了一个相当著名的模块,名叫 pydoc (比较而言: pydoc 可以做到 perldoc 所能做的任何事,并且做得更好.更漂亮:-).对于 Python 2.1 来说, ...