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. 8.4.1 ImageLoader

    ImageLoader 的工作原理(已经不维护了) 在显示图片的时候,它会先在内存中查找:如果没有,就去本地查找:如果还没有,就开一个新的线程去下载这张图片,下载成功会把图片同时缓存到内存和本地. 基 ...

  2. Windows 10开机的秘密在哪里

    如何查看开机速度 查看开机速度,一定要安装所谓的第三方安全软件么?老子就是不喜欢被安全! Windows自带的事件查看器中记录了系统的一举一动,何必需要别人来监视? 在运行(WinKey+R)中输入e ...

  3. [Leetcode]Reverse Integer

      核心思想:原数对10取余数赋值给新数后降一位,再把新数升一位加上下一次原数取余值,直到原数降为0. 解法如下: int reverse(int x) { bool minus = false; ) ...

  4. webuploader 断点续传

    webuploader 实现 断点续传webuploader是百度开发的上传文件前端控件.可支持html5和flash,因此对浏览器的兼容比较好.因为需要用到ie8,ie8不支持html5,所以必须支 ...

  5. 【C-循环结构】

    C语言提供了多种循环语句,可以组成各种不同形式的循环结构: 用goto语句和if语句构成循环: 用while语句: 用do-while语句: 用for语句: 一.goto语句 goto语句是一种无条件 ...

  6. IO-04. 混合类型数据格式化输入(5)

    本题要求编写程序,顺序读入浮点数1.整数.字符.浮点数2,再按照字符.整数.浮点数1.浮点数2的顺序输出. 输入格式: 输入在一行中顺序给出浮点数1.整数.字符.浮点数2,其间以1个空格分隔. 输出格 ...

  7. centos7 zabbix3 install done

    centOS7安装zabbix3.0 一.前提: 现在zabbix出现了3.0版本,服务器端只支持centOS7,客户端和代理可以支持centOS6,实验环境是服务端安装centOS7,客户用cent ...

  8. PDF

    源代码请从这里下载: http://download.csdn.net/source/2984395 使用的是JSP编程 ‍ 这是导出后的效果 ‍ 这是数据库中的内容 ‍ 部分代码: <%@ p ...

  9. T4模版基础例子

    <#@ template debug="false" hostspecific="true" language="C#" #> ...

  10. Xamarin跨平台移动开发解决方案

    Xamarin始创于2011年,旨在使移动开发变得难以置信地迅捷和简单.Xamarin的产品简化了针对多种平台的应用开发,包括iOS.Android.Windows Phone和Mac App.Xam ...