[关键字]:LCA

[题目大意]:求出两点间的最近公共祖先。

//=====================================================================================================

[分析]:利用并查集在每次对子树进行遍历时进行合并,因为对以x为根的子树的遍历时只有当x的所有子树都遍历过后才会把它合并到他父亲的集合里,所以当需要查找的两个节点q1、q2中q1已被遍历且q2正是当前遍历的节点时说明此时只有距他们最近的祖先是在集合里的(可能为q1或q2),所以只要找到已被遍历的q1所在集合的祖先就是这两的节点的LCA。POJ1470和此题是一样的只是询问变成了多组。

[代码]:

View Code

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<algorithm> 6 #include<vector> 7 using namespace std; 8  9 int n,q1,q2;10 int f[10010],a[10010];11 bool c[10010],flag[10010];12 vector<int> tree[10010];13 14 int find(int k)15 {16     if (f[k]==k) return k;17     f[k]=find(f[k]);18     return f[k];19 }20 21 int unions(int x,int y)22 {23     int xx=find(x),yy=find(y);24     f[yy]=xx;25     return 0;26 }27 28 int tarjan(int u)29 {30     //f[u]=u;31     for (int i=0;i<tree[u].size();i++)32     {33         tarjan(tree[u][i]);34         unions(u,tree[u][i]);35         a[find(u)]=u;36     }37     c[u]=1;38     if (q1==u && c[q2]) printf("%d\n",a[find(q2)]);39     else if (q2==u && c[q1]) printf("%d\n",a[find(q1)]);40     return 0;41 }42 43 int main()44 {45     int cases;46     scanf("%d",&cases);47     while (cases--)48     {49           scanf("%d",&n);50           for (int i=1;i<=n;i++)51           {52               tree[i].clear();53               f[i]=i;54               a[i]=0;55               c[i]=0;56               flag[i]=1;57           }58           for (int i=1;i<n;i++)59           {60               int x,y;61               scanf("%d%d",&x,&y);62               flag[y]=0;63               tree[x].push_back(y);64           }65           scanf("%d%d",&q1,&q2);66           int i;67           for (i=1;i<=n;i++)68               if (flag[i]) break;69           tarjan(i);70     }71     system("pause");72     return 0;73 }

转载于:https://www.cnblogs.com/procedure2012/archive/2012/01/29/2331468.html

[POJ1330 Nearest Common Ancestors]相关推荐

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

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

  2. LCA——JD 3055 Nearest Common Ancestors

    3055: Nearest Common Ancestors Time Limit: 1 Sec   Memory Limit: 128 MB Description 给定N个节点的一棵树,有K次查询 ...

  3. JDOJ 3055: Nearest Common Ancestors

    JDOJ 3055: Nearest Common Ancestors JDOJ传送门 Description 给定N个节点的一棵树,有K次查询,每次查询a和b的最近公共祖先. 样例中的16和7的公共 ...

  4. POJ 1330 Nearest Common Ancestors 【LCA模板题】

    任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000 ...

  5. Nearest Common Ancestors(LCA板子)

    题目链接:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 1000 ...

  6. 【POJ - 1330】Nearest Common Ancestors(lca,模板题)

    题干: A rooted tree is a well-known data structure in computer science and engineering. An example is ...

  7. POJ - 1330 Nearest Common Ancestors tanjan_LCA

    传送门 题意就是题目 所谓在线,就是一个一个贼笨且时间太长. #include<stdio.h> #include<string.h> #include<iostream ...

  8. Nearest Common Ancestors

    http://poj.org/problem?id=1330 题解:LCA 树上倍增 /* *@Author: STZG *@Language: C++ */ //#include <bits/ ...

  9. POJ - 1330 Nearest Common Ancestors(树上倍增/树链剖分求LCA)

    题目链接:点击查看 题目大意:给出一棵有根树,我们需要求出两个点的lca 题目分析:因为题目说了是有根树,所以我们在建图的时候直接建有向图就好了,并且记录一下每个点的入度,建完图后找一下入度为0的点, ...

最新文章

  1. 步步为营 .NET 代码重构学习笔记 九
  2. jqurey操作select 语法解释
  3. 让WPF和SL控件同时支持绑定和赋值
  4. 视图函数中进行sql查询,防止sql注入
  5. C#中的委托和事件 (7)---总结
  6. 安全系列------web环境搭建组合
  7. Python学习:模块
  8. php数据库操作类的调用优化,PHP PDO优化数据库操作类 多数据库驱动类
  9. 121 Best Time to Buy and Sell Stock
  10. 使用Python模拟蒙蒂霍尔悖论游戏
  11. UVA10739 String to Palindrome【记忆化搜索+DP】
  12. 聊聊spring for kafka对consumer的封装与集成
  13. 外军网络空间作战简报
  14. Scratch编程训练——小猫进圈
  15. 安卓开发——MaterialDesign实战
  16. HTML禁用浏览器后退功能
  17. 如何创建一张属于自己的简单的网页
  18. 99道python测试题
  19. 时间序列分类算法之时间序列森林(TSF)
  20. 2021-2027全球及中国群集机器人行业研究及十四五规划分析报告

热门文章

  1. 从入门到精通的Java进阶学习笔记整理,不愧是大佬
  2. 我就想要个两年1024徽章~!
  3. 【机器学习入门到精通系列】Logistic回归多分类图示
  4. python【数据结构与算法】Python语法查询大宝剑(全)
  5. 机器字长,指令字长,数据子长,MDR
  6. python图形用户界面设计报告_19.1 Python图形用户界面开发工具包
  7. linux 安装 zookeeper 管理端, dubbo-admin 访问 404
  8. 分峰截幅c语言算法,面向桥梁健康监测的复合传感技术研究
  9. 2048c语言程序,C语言实现2048小游戏
  10. php动态加载js,动态加载script文件的两种方法_javascript技巧