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]=1;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=0; 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, 0, sizeof(cnt));for(int i=1; i<n; ++i){int u, v;scanf("%d%d", &u, &v);g[u].push_back(v);++cnt[v];}memset(vis, 0, sizeof(vis));scanf("%d%d", &x, &y);for(int i=1; i<=n; ++i)if(cnt[i]==0){LCA(i);break;}printf("%d\n", ret);for(int i=1; i<=n; ++i)g[i].clear();}return 0;
}
#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]=1;int len;len=p[u].size();for(int i=0; i<len; ++i){int v=p[u][i];if(vis[v])++ans[getFather(v)];}len=g[u].size();for(int i=0; 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, 0, sizeof(cnt));for(int i=1; i<=n; ++i){vis[i]=0;ans[i]=0;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=1; i<=n; ++i)if(cnt[i]==0){LCA(i);break;}for(int i=1; i<=n; ++i)if(ans[i]!=0)printf("%d:%d\n", i, ans[i]);for(int i=1; i<=n; ++i)g[i].clear(), p[i].clear();}return 0;
}

poj1330Nearest Common Ancestors 1470 Closest Common Ancestors(LCA算法)相关推荐

  1. POJ 1470 Closest Common Ancestors (最近公共祖先LCA 的离线算法Tarjan)

    Tarjan算法的详细介绍,请戳: http://www.cnblogs.com/chenxiwenruo/p/3529533.html #include <iostream> #incl ...

  2. POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)...

    POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...

  3. LCA算法以及原理详解

    LCA-最近公共祖先   LCA(Least Common Ancestors),即最近公共祖先,这种描述是基于树结构的,也即我们通通常只在树结构中考虑祖先问题.树实际上就是图论中的有向无环图,而要研 ...

  4. LeetCode Lowest Common Ancestor of a Binary Tree(LCA问题)

    问题:求二叉树中两个结点p,q的最近公共祖先 思路:第一种方法是二叉树的递归,当搜索是当前结点为p或者为q时,直接返回对应结点.然后再左右子树的返回情况 1.如果左右子树非空,则当前结点就是要找的最近 ...

  5. Innumerable Ancestors 尺取 dfs序 lca

    给一棵树,m次查询,每次查询给两个集合,从这两个集合中分别选一个结点,使得这两个结点的lca的深度最大 考虑dfs序为3, 4, 5的三个结点,3和4的lca深度一定大于等于3和5的lca深度 所以可 ...

  6. common.php下载,插件common.php全局函数文件

    系统内置函数 建议先熟悉系统内置 application/common.php 与 application/function.php 两文件的函数说明,如果有符合的函数,尽量在插件里调用:不符合的话就 ...

  7. linux docker 中使用 System.Drawing.Common 异常 System.Drawing.Common 仅在 Windows 上受支持

    System.Drawing.Common NuGet 包现在被归类为特定于 Windows 的库.为非 Windows 操作系统编译时,平台分析器会在编译时发出警告. 在非 Windows 操作系统 ...

  8. hdu3078 建层次树+在线LCA算法+排序

    题意:n个点,n-1条边构成无向树,每个节点有权,Q次询问,每次或问从a->b的最短路中,权第k大的值,/或者更新节点a的权, 思路:在线LCA,先dfs生成树0,标记出层数和fa[](每个节点 ...

  9. LCA 算法(一)ST表

    介绍一种解决最近公共祖先的在线算法,st表,它是建立在线性中的rmq问题之上.   代码:   1 //LCA: DFS+ST(RMQ) 2 3 #include<cstdio> 4 #i ...

最新文章

  1. redhat7.4启动telnet和SSH服务,修改IP地址
  2. java xssf的背景色,poi xssf背景颜色对照表 java生成excel文件
  3. setjmp与logjmp用法总结
  4. 值得收藏!UI设计学习借鉴常用网站
  5. 哈工大计算机考研 跨考,09哈工大计算机考研复试之我的经历_跨考网
  6. 股市常胜将军都懂得适时休息
  7. debtorpmandrpmtodeb
  8. 微信支付相关(付款码支付,Native支付)
  9. 555 定时器的 3 种用法判断和解释
  10. 2014.3 USACO月赛T1 Watering the Fields
  11. Latex制作beamer幻灯片、写论文的基本语句
  12. 小饶学编程之JAVA SE第二部分——Web 前端基础:09CSS3
  13. Linux CentOS 7 搭建DNS域名服务器
  14. 插值算法之:拉格朗日插值
  15. 基于大数据的保险商业模式
  16. 必不可少的数学基础-聚点原理
  17. Verilog学习之数据大小端转换设计
  18. 在合约中获取代币余额のEOS智能合约系列
  19. 一名优秀设计师,必须知道的印刷常识!
  20. 电脑开机卡在android,如何修复它:Android卡在引导屏幕上-万兴恢复专家

热门文章

  1. http_build_query用法
  2. php正则表达式函数 preg_replace用法
  3. 体验Windows 7的Superbar
  4. 现代内存编号解读(转)
  5. 阿里云弹性公网IP(EIP)的使用限制
  6. 8.11. Migrating MySQL Data into Elasticsearch using logstash
  7. android之android.intent.category.DEFAULT的用途和使用
  8. Pycharm 建立工程,包含多个工程目录
  9. expect脚本同步文件、 expect脚本指定host和要同步的文件 、 构建文件分发系统 、批量远程执行命令...
  10. Emptiness 空值语义