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.什么是最近公共 ...
随机推荐
- 前端实现table表格导出excel
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 一款基于jQuery的联动Select下拉框
今天我们要来分享一款很实用的jQuery插件,它是一个基于jQuery多级联动的省市地区Select下拉框,并且值得一提的是,这款联动下拉框是经过自定义美化过的,外观比浏览器自带的要漂亮许多.另外,这 ...
- 字符集导致乱码问题,gi安装问题
今天是2014-4-24,今天中午收到一个天津网友问的一个安装gi的问题,和一个网友问的字符集问题:在此整理一下 问题一: gi安装问题: 问题描写叙述: 在安装gi的时候提示:"INS-2 ...
- EmWebAdmin 初步上手
EmWebAdmin 简介: // github 地址: https://github.com/ZengjfOS/EmWebAdmin // 介绍: 参考gentelella做的模板: 这是一个PHP ...
- DRBD安装配置、工作原理及故障恢复
一.DRBD简介 DRBD的全称为:Distributed ReplicatedBlock Device(DRBD)分布式块设备复制,DRBD是由内核模块和相关脚本而构成,用以构建高可用性的集群.其实 ...
- CI框架中 类名不能以方法名相同
昨天晚上一个坑爹的问题折腾了我一晚上,首先我来说下我的代码,我建立了一个index的控制器然后呢 在控制器里有一个index的方法.页面模板都有. if ( ! defined('BASEPATH' ...
- 一个RESTful服务,用来定位运行在AWS地区(Region)中的中间层服务
Eureka 一个RESTful服务,用来定位运行在AWS地区(Region)中的中间层服务.由两个组件组成:Eureka服务器和Eureka客户端.Eureka服务器用作服务注册服务器.Eureka ...
- Leetcode_num4_Reverse Integer
题目: Reverse digits of an integer. Have you thought about this? Here are some good questions to ask b ...
- PHPMailer发送邮箱
1.可以参考的链接.http://www.helloweba.com/view-blog-205.html 2.下载最新的PHPMailer文件库 3.主要代码 class.phpmailer.php ...
- UVa 11178:Morley’s Theorem(两射线交点)
Problem DMorley’s TheoremInput: Standard Input Output: Standard Output Morley’s theorem states that ...