LCA思想:http://www.cnblogs.com/hujunzheng/p/3945885.html

在求解最近公共祖先为问题上,用到的是Tarjan的思想,从根结点开始形成一棵深搜树,非常好的处理技巧就是在回溯到结点u的时候,u的子树已经遍历,这时候才把u结点放入合并集合中,
这样u结点和所有u的子树中的结点的最近公共祖先就是u了,u和还未遍历的所有u的兄弟结点及子树中的最近公共祖先就是u的父亲结点。以此类推。。这样我们在对树深度遍历的时候就很自然的将树中的结点分成若干的集合,两个集合中的所属不同集合的任意一对顶点的公共祖先都是相同的,也就是说这两个集合的最近公共最先只有一个。对于每个集合而言可以用并查集来优化,时间复杂度就大大降低了,为O(n + q),n为总结点数,q为询问结点对数。

 /*
题意很明显,就是求LCA!
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define N 10005
using namespace std; int n;
int x, y;
vector<int>g[N];
int f[N];
int vis[N], cnt[N];
int ret; int getFather(int x){
return x==f[x] ? x : f[x]=getFather(f[x]);
} bool LCA(int u){
vis[u]=;
f[u]=u;
int len=g[u].size(); if(u==x && vis[y]){
ret=getFather(y);
return true;
} else if(u==y && vis[x]){
ret=getFather(x);
return true;
} for(int i=; i<len; ++i){
int v=g[u][i];
if(!vis[v]){
if(LCA(v)) return true;
f[v]=u;
}
}
return false;
} int main(){
int t;
scanf("%d", &t);
while(t--){
scanf("%d", &n);
memset(cnt, , sizeof(cnt));
for(int i=; i<n; ++i){
int u, v;
scanf("%d%d", &u, &v);
g[u].push_back(v);
++cnt[v];
}
memset(vis, , sizeof(vis));
scanf("%d%d", &x, &y);
for(int i=; i<=n; ++i)
if(cnt[i]==){
LCA(i);
break;
}
printf("%d\n", ret);
for(int i=; i<=n; ++i)
g[i].clear();
}
return ;
}
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#define N 905
#define M 25000
using namespace std; vector<int>g[N];
vector<int>p[M];
int cnt[N];
int f[N];
int vis[N];
int ans[N]; int n, m; int getFather(int x){
return x==f[x] ? x : f[x]=getFather(f[x]);
} void LCA(int u){
f[u]=u;
vis[u]=;
int len; len=p[u].size();
for(int i=; i<len; ++i){
int v=p[u][i];
if(vis[v])
++ans[getFather(v)];
} len=g[u].size();
for(int i=; i<len; ++i){
int v=g[u][i];
if(!vis[v]){
LCA(v);
f[v]=u;
}
}
} int main(){
while(scanf("%d", &n)!=EOF){
memset(cnt, , sizeof(cnt));
for(int i=; i<=n; ++i){
vis[i]=;
ans[i]=;
int a, d, b;
char ch;
scanf("%d", &a);
while(scanf("%c", &ch) && ch!='(');
scanf("%d", &d);
while(scanf("%c", &ch) && ch!=')');
while(d--){
scanf("%d", &b);
++cnt[b];
g[a].push_back(b);
}
}
scanf("%d", &m);
while(m--){
char ch;
while(scanf("%c", &ch) && ch!='(');
int u, v;
scanf("%d%d", &u, &v);
p[u].push_back(v);
p[v].push_back(u);
while(scanf("%c", &ch) && ch!=')');
} for(int i=; i<=n; ++i)
if(cnt[i]==){
LCA(i);
break;
}
for(int i=; i<=n; ++i)
if(ans[i]!=)
printf("%d:%d\n", i, ans[i]); for(int i=; i<=n; ++i)
g[i].clear(), p[i].clear();
}
return ;
}

poj1330Nearest Common Ancestors 1470 Closest Common Ancestors(LCA算法)的更多相关文章

  1. POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)

    POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...

  2. POJ 1470 Closest Common Ancestors 【LCA】

    任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000 ...

  3. poj 1470 Closest Common Ancestors LCA

    题目链接:http://poj.org/problem?id=1470 Write a program that takes as input a rooted tree and a list of ...

  4. POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13372   Accept ...

  5. POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13370   Accept ...

  6. POJ——T 1470 Closest Common Ancestors

    http://poj.org/problem?id=1470 Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 20830   ...

  7. POJ 1470 Closest Common Ancestors

    传送门 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 17306   Ac ...

  8. poj——1470 Closest Common Ancestors

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 20804   Accept ...

  9. POJ 1470 Closest Common Ancestors【近期公共祖先LCA】

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013912596/article/details/35311489 题目链接:http://poj ...

随机推荐

  1. Activity劫持实例与防护手段

    原文地址:Activity劫持实例与防护手段 作者:cjxqhhh (本文只用于学习技术,提高大家警觉,切勿用于非法用途!)   什么叫Activity劫持   这里举一个例子.用户打开安卓手机上的某 ...

  2. Hyper-V初涉_共享式网络链接

    任何一台计算机,如果不能与网络连通,可以说已经失去了大部分的功能,Windows 8尤是如此,虚拟机亦是如此. Hyper-V并不能对物理机的网卡进行识别,所以需要借助虚拟网卡通过物理机的网络共享实现 ...

  3. [转] How to import a large data set using XPO efficiently within a transaction

    https://www.devexpress.com/Support/Center/Example/Details/T333879

  4. (function(){...}())与(function(){...})()

    (function(){         ......   }())  或   (function(){            ......   })()  匿名函数自调用,也就是说,定义一个匿名函数 ...

  5. WebRTC实现网页版多人视频聊天室

    因为产品中要加入网页中网络会议的功能,这几天都在倒腾 WebRTC,现在分享下工作成果. 话说 WebRTC Real Time Communication 简称 RTC,是谷歌若干年前收购的一项技术 ...

  6. EntityFunctions.AsNonUnicode

    http://blog.csdn.net/zzx3q/article/details/7863797 使用工具VS2010 凡是调用FindAll的地方,如果传入参数是String类型的变量(数字类型 ...

  7. Java多线程6:synchronized锁定类方法、volatile关键字及其他

    同步静态方法 synchronized还可以应用在静态方法上,如果这么写,则代表的是对当前.java文件对应的Class类加锁.看一下例子,注意一下printC()并不是一个静态方法: public ...

  8. 支持断点续传的文件上传插件——Huploadify-V2.0来了

    之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需 ...

  9. MySQL—FOREIGN KEY

    作用:保持数据一致性,完整性.实现一对一或一对多关系.(学习的过程中,老师说,实际的生产中,一般不使用物理上的外键约束的,都是使用逻辑上的外键约束) 要求: 父表与子表的存储引擎必须相等,而且只能是I ...

  10. PsySH:PHP交互运行环境

    是什么 我们经常会在命令行用到诸如mysql.python等命令,特点是一旦输入后,会进入命令本身的交互运行环境.示例: [root@iZ25vs3mckhZ ~]# python Python 2. ...