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.什么是最近公共 ...
随机推荐
- eclipse生成export生成jar详解
使用eclipse打jar包可能还有很多人不是很了解,今天特意测试整理一番. 打jar包有3种形式 JAR file JAR Javadoc ja ...
- 【Code::Blocks】windows 环境下编译 Code::Blocks(已修正)
Code::Blocks 在2012-11-25发布了最新的12.11版本,相比上一个版本(10.05),Code::Blocks 进行了许多改进和更新(Change log). 引用 Wikiped ...
- 16V554 的测试代码
//------------------------------------------------------------------------ #include "AT16C554 ...
- c配置库ccl使用小结
配置文件为key=value键值对形式 下载与安装 库文件下载:ccl-0.1.1.tar.gz 安装: tar -zxvf ccl-0.1.1.tar.gz cd ccl-0.1.1 ./con ...
- 打开palette面板
- 9款很酷炫jQuery/HTML5特效应用 有源码哦~
目前最流行的网页特效应用当属jQuery和HTML5的特效应用了,它们可以帮你快速实现网页中的各种特效设计.本文就为了收集了9款非常酷炫的jQuery/HTML5特效应用,可以很方便的 ...
- SQL Server,MySql,Oracle数据库的默认端口号
SQL Server默认端口号为:1433 MySQL 默认端口号为:3306 Oracle 默认端口号为:1521
- 【BZOJ】1646: [Usaco2007 Open]Catch That Cow 抓住那只牛(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1646 这一题开始想到的是dfs啊,,但是本机测样例都已经re了... 那么考虑bfs...很巧妙? ...
- 【BZOJ】1652: [Usaco2006 Feb]Treats for the Cows(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1652 dp.. 我们按间隔的时间分状态k,分别为1-n天 那么每对间隔为k的i和j.而我们假设i或者 ...
- 【mysql】一次有意思的数据库查询分析。
本文是在做一家汽车配件的电商网站时,大体情景是一个List.php页面,该页面分页列出部分配件并统计总数量用于分页. 当然该页面中也可以指定一下查询条件,如适配的车辆品牌.车系.排量.年份等,一件商品 ...