poj 1330 LCA (倍增+离线Tarjan)
/*
先来个倍增
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#define maxn 10010
using namespace std;
int T,n,num,head[maxn],st,end,anc,fa[maxn][],dep[maxn],out[maxn],root;
struct node
{
int u,v,t,pre;
}e[maxn*];
void Add(int from,int to)
{
num++;
e[num].u=from;
e[num].v=to;
e[num].pre=head[from];
head[from]=num;
}
void Dfs(int now,int from,int c)
{
fa[now][]=from;
dep[now]=c;
for(int i=head[now];i;i=e[i].pre)
if(e[i].v!=from)
Dfs(e[i].v,now,c+);
}
void Get_fa()
{
for(int j=;j<=;j++)
for(int i=;i<=n;i++)
fa[i][j]=fa[fa[i][j-]][j-];
}
int Get_same(int a,int t)
{
for(int i=;i<=t;i++)
a=fa[a][];
return a;
}
int LCA(int a,int b)
{
if(dep[a]<dep[b])swap(a,b);
a=Get_same(a,dep[a]-dep[b]);
if(a==b)return a;
for(int i=;i>=;i--)
if(fa[a][i]!=fa[b][i])
{
a=fa[a][i];b=fa[b][i];
}
return fa[a][];
}
int main()
{
scanf("%d",&T);
while(T--)
{
memset(head,,sizeof(head));
memset(fa,,sizeof(fa));
memset(out,,sizeof(out));
memset(dep,,sizeof(dep));
num=;root=;
scanf("%d",&n);
int x,y;
for(int i=;i<=n-;i++)
{
scanf("%d%d",&x,&y);
Add(x,y);Add(y,x);
out[y]=;
}
for(int i=;i<=n;i++)
if(out[i]==)root=i;
Dfs(root,root,);
Get_fa();
scanf("%d%d",&st,&end);
anc=LCA(st,end);
printf("%d\n",anc);
}
return ;
}
/*
离线Tarjan
我们Dfs整张图的时候 对于一组u v
我们一定按照 u s v 的顺序跑完
此时u v 在以s为根的子树里
那么我们借助并茶几 将u v的fa 的anc赋值为s
这样我们查询u v 的时候就能找到s
如果我们求 st end 的lca
当我们遍历到st 或者end的时候 只需要判断另一个是不是已经被访问过
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#define maxn 100010
using namespace std;
int T,n,m,fa[maxn],st,end,anc[maxn];
vector<int>a[maxn];
int root[maxn],f[maxn];
void init()
{
scanf("%d",&n);
int x,y;
for(int i=;i<=n;i++)
{
fa[i]=i;root[i]=;
}
for(int i=;i<=n-;i++)
{
scanf("%d%d",&x,&y);
a[x].push_back(y);
fa[y]=x;root[y]=;
}
}
int find(int x)
{
if(x!=fa[x])fa[x]=find(fa[x]);
return fa[x];
}
void Union(int x,int y)
{
int r1=find(x);
int r2=find(y);
if(r1!=r2)fa[r2]=r1;
}
void LCA(int parent)
{
anc[parent]=parent;//初始化自己的lca为自己
for(int i=;i<a[parent].size();i++)
{
LCA(a[parent][i]);
Union(parent,a[parent][i]);
anc[find(parent)]=parent;//把自己和自己子孙们的lca赋值为它
}
f[parent]=;
if(st==parent&&f[end]==)
{
printf("%d\n",anc[find(end)]);
return;
}
if(end==parent&&f[st]==)
{
printf("%d\n",anc[find(st)]);
return;
}
}
int main()
{
scanf("%d",&T);
while(T--)
{
memset(f,,sizeof(f));
memset(a,,sizeof(a));
init();
scanf("%d%d",&st,&end);
for(int i=;i<=n;i++)
if(root[i])
LCA(i);
}
return ;
}
poj 1330 LCA (倍增+离线Tarjan)的更多相关文章
- POJ 1330 LCA裸题~
POJ 1330 Description A rooted tree is a well-known data structure in computer science and engineerin ...
- POJ 1330 LCA最近公共祖先 离线tarjan算法
题意要求一棵树上,两个点的最近公共祖先 即LCA 现学了一下LCA-Tarjan算法,还挺好理解的,这是个离线的算法,先把询问存贮起来,在一遍dfs过程中,找到了对应的询问点,即可输出 原理用了并查集 ...
- POJ 1330 Nearest Common Ancestors(Tarjan离线LCA)
Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...
- poj 1986 Distance Queries(LCA:倍增/离线)
计算树上的路径长度.input要去查poj 1984. 任意建一棵树,利用树形结构,将问题转化为u,v,lca(u,v)三个点到根的距离.输出d[u]+d[v]-2*d[lca(u,v)]. 倍增求解 ...
- POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13372 Accept ...
- LCA/在线(倍增)离线(Tarjan)
概念 祖先 公共祖先 最近公共祖先 方法1:暴力爬山法 方法2:倍增 求公共祖先 求俩点的距离 Tarjan 概念 祖先 有根树中,一个节点到根的路径上的所有节点被视为这个点的祖先,包括根和它本身 公 ...
- poj 1330 LCA最近公共祖先
今天学LCA,先照一个模板学习代码,给一个离线算法,主要方法是并查集加上递归思想. 再搞,第一个离线算法是比较常用了,基本离线都用这种方法了,复杂度O(n+q).通过递归思想和并查集来寻找最近公共祖先 ...
- LCA:倍增与tarjan
学了好久(一两个星期)都没彻底搞懂的lca,今天总算理解了.就来和大家分享下我自己的心得 首先,如果你还不懂什么是lca,出门左转自行百度 首先讲倍增 倍增的思想很简单,首先进行预处理,用一个深搜将每 ...
- poj 1330 LCA
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #i ...
随机推荐
- Python学习 - 编写一个简单的web框架(二)
在上一篇日志中已经讨论和实现了根据url执行相应应用,在我阅读了bottle.py官方文档后,按照bottle的设计重写一遍,主要借鉴大牛们的设计思想. 一个bottle.py的简单实例 来看看bot ...
- iptables的设置
一.filter表防火墙(过滤器) iptables -A ( INPUT OUTPUT ) -s 192.1680.1.200 -p ( TCP UDP ICMP ) -i ( eth0 eth1 ...
- 在VS2008.Net下使用WPF开发Web应用程序
原文地址:http://hankjin.blog.163.com/blog/static/33731937200922353623434/ 胖客户端的好处是可以轻易的实现绚丽的效果, 而瘦客户端则需要 ...
- [转]Uploading and Downloading VHDs to Windows Azure
The article shows how to download and upload VHD to Azure. http://michaelwasham.com/windows-azure-po ...
- 普通table表格样式及代码大全(全)
普通table表格样式及代码大全(全)(一) 单实线边框表格 <TABLE style="BORDER-COLLAPSE: collapse" borderColor=#00 ...
- VIM大作战之C++简易集成编译环境(Windows篇)
一切都要从这篇文章说起 Vim 实在是精致独特得有点像个林妹妹.但谁要是希望家里也有个林妹妹,光把自家丫头照着绣像打扮打扮是不行的,必须从零开始养成一个.而且就算真能养出来个“天上掉下来”一般的可人儿 ...
- Wall(Graham算法)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27110 Accepted: 9045 Description Once ...
- BZOJ1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 4 ...
- weblogic启动报错之未修改hosts产生错误
报错如下: Enter username to boot WebLogic server:weblogic Enter password to boot WebLogic server: <Ju ...
- 去掉cell边框的简单办法
实很简单,把backgroundView设置为一个空的View,然后就干净了 UIView *tempView = [[UIView alloc] init]; [cell setBackground ...