POJ 水题 1330 LCA
2012-01-23 01:41
447 查看
#include<iostream> using namespace std; const int maxn=10001; int pre[maxn]; int main() { freopen("in.txt","r",stdin); int t,n,a,b; scanf("%d",&t); while(t--){ scanf("%d",&n); memset(pre,0,sizeof(pre)); for(int i=0;i<n-1;i++){ int a,b; scanf("%d%d",&a,&b); pre[b]=a; } scanf("%d%d",&a,&b); int la=0,lb=0; int p1=a,p2=b; while(pre[p1]){p1=pre[p1];la++;} while(pre[p2]){p2=pre[p2];lb++;} p1=a,p2=b; if(la>lb){ while(la>lb){la--;a=pre[a];} }else{ while(lb>la){lb--;b=pre[b];} } while(la--){ if(a==b)break; a=pre[a]; b=pre[b]; } printf("%d\n",a); } }
相关文章推荐
- 【POJ1330】Nearest Common Ancestors(树链剖分求LCA)
- poj 1330 , LCA, 乱搞
- POJ 1330:Nearest Common Ancestors【lca】
- poj1330 Nearest Common Ancestors LCA倍增法 或 LCA转RMQ
- poj 1330 LCA
- POJ 1330 Nearest Common Ancestors LCA
- poj 1330 LCA [ vector 模拟邻接表存图 DFS 找到每个节点的层]
- LCA 算法学习 (最近公共祖先)poj 1330
- POJ 1330 Nearest Common Ancestors (在线LCA转RMQ)
- Nearest Common Ancestors POJ - 1330 (LCA)
- poj 1330 LCA 最近公共祖先
- poj 1330 Nearest Common Ancestors(LCA)
- 【LCA倍增模板】【poj1330】最近公共祖先
- (LCA离线)poj1330 M - Nearest Common Ancestors
- POJ 1330 Nearest Common Ancestors 倍增算法的LCA
- POJ1330:Nearest Common Ancestors(LCA + Tarjan离线处理)
- POJ 1330 Nearest Common Ancestors LCA
- POJ 1330 Nearest Common Ancestors (模板水题离线LCA,3级)
- POJ1330 TarjanLCA
- POJ题目1330 Nearest Common Ancestors(LCA)