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.什么是最近公共 ...
随机推荐
- cocos2d-x中CCEditbox导出到lua
自从工作后感觉时间较少(每天工作9-22,晚上就不想动了,早上想多睡点),工作中用的是 cocos2d-x.cocos2d-x是一款手机游戏引擎,虽然支持lua,但和love2d相比非纯lua游戏引 ...
- C# 注册表修改 立即生效 [转]
修改注册表后不重启计算机边生效. const int WM_SETTINGCHANGE = 0x001A; const int HWND_BROADCAST = 0xffff; IntPtr resu ...
- inline-block元素的空白间距解决方法<转>
使用inline-block来代替float进行布局,或者使用inline-block来实现元素的居中效果.有关于使用inline-block来代替float的讨论也蛮多的. 不过就是使用inline ...
- PHP——修改数据库1
主页面——0126.php 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
- asp.net防止刷新时重复提交
前段时间遇到了需要禁用刷新的需求,f5按钮就不说了,简单的js就能把它禁用,但是工具条上的刷新按钮却傻傻干不掉. 如果简单的在刷新时重新加载画面,通过window.location.href=&quo ...
- 特征根法求通项+广义Fibonacci数列找循环节 - HDU 5451 Best Solver
Best Solver Problem's Link Mean: 给出x和M,求:(5+2√6)^(1+2x)的值.x<2^32,M<=46337. analyse: 这题需要用到高中的数 ...
- VS2013 连接 MySQL
1.安装必须的工具: mysql-connector-net-6.8.3 mysql-installer-community-5.6.16.0.msi mysql-for-visualstudio-1 ...
- JSON动态生成树
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- MapReduce 中的两表 join 几种方案简介
转自:http://my.oschina.net/leejun2005/blog/95186 MapSideJoin例子:http://my.oschina.net/leejun2005/blog/1 ...
- 一个区域只能放置一个组件,如果想在一个区域放置多个组件就需要使用Panel来装载
五种布局管理器: Flow Layout(流式布局):按照组件添加到容器中的顺序,顺序排放组件位置.默认为水平排列,如果越界那么会向下排列.排列的位置随着容器大小的改变而改变. Panel默认的布局管 ...