题目连接  http://poj.org/problem?id=1330

就是构建一棵树,然后问你两个节点之间最近的公共父节点是谁?

代码:

 /*Source Code
Problem: 1330 User: huifeidmeng
Memory: 1232K Time: 63MS
Language: C++ Result: Accepted Source Code
*/
#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn=;
vector<int> tree[maxn],qus[maxn];
int rank[maxn],father[maxn];
bool vis[maxn];
int rudu[maxn];
int lroot[maxn];
void init(int n){
memset(vis,,sizeof(vis));
memset(rudu,,sizeof(rudu));
memset(lroot,,sizeof(lroot));
for(int i=;i<=n;i++){
father[i]=i;
rank[i]=;
tree[i].clear();
qus[i].clear();
}
}
int find(int a){
while(a!=father[a])
a=father[a];
return a;
}
void Union(int a,int b)
{
int x=find(a);
int y=find(b);
if(x==y) return ;
if(rank[x]<rank[y]){
rank[y]+=rank[x];
father[x]=y;
}
else {
rank[x]+=rank[y];
father[y]=x;
}
}
void LCA(int u)
{
lroot[u]=u;
int len=tree[u].size();
for(int i=;i<len;i++){
LCA(tree[u][i]);
Union(u,tree[u][i]);
lroot[find(u)]=u;
}
vis[u]=;
int ss=qus[u].size();
for(int i=;i<ss;i++){
if(vis[qus[u][i]]){
printf("%d\n",lroot[find(qus[u][i])]);
return ;
}
}
}
int main()
{
int cas,n,u1,u2;
//freopen("test.in","r",stdin);
scanf("%d",&cas);
while(cas--){
scanf("%d",&n);
init(n);
for(int i=;i<n;i++){
scanf("%d%d",&u1,&u2);
tree[u1].push_back(u2);
rudu[u2]++;
}
scanf("%d%d",&u1,&u2);
qus[u1].push_back(u2);
qus[u2].push_back(u1);
for(int i=;i<=n;i++){
if(==rudu[i]){ //找到root
LCA(i);
break;
}
}
}
return ;
}

poj----1330Nearest Common Ancestors(简单LCA)的更多相关文章

  1. POJ:1330-Nearest Common Ancestors(LCA在线、离线、优化算法)

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

  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,离线Tarjan算法)

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

  4. POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14902   Accept ...

  5. POJ 1330 Nearest Common Ancestors(lca)

    POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...

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

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

  7. poj 1330 Nearest Common Ancestors(LCA 基于二分搜索+st&rmq的LCA)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30147   Accept ...

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

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

  9. poj----(1470)Closest Common Ancestors(LCA)

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

  10. ZOJ 1141:Closest Common Ancestors(LCA)

    Closest Common Ancestors Time Limit: 10 Seconds      Memory Limit: 32768 KB Write a program that tak ...

随机推荐

  1. An Example of Pre-Query and Post-Query Triggers in Oracle Forms With Using Display_Item to Highlight Dynamically

    Example is given for Pre-Query and Post-Query triggers in Oracle Forms, with using Display_Itembuilt ...

  2. MySQL多表更新(逻辑外键/事实外键)

    语法结构: UPDATE  table_reference  SET  列名1=value1[,列名2=value2,......] [WHERE  where_condition] 说明: tabl ...

  3. XAF应用开发教程(六)控制器

    是的,XAF也是MVC结构的,但不仅限于MVC,ViewModel也存在,它是一项复合技术,AOP,ORM,MVC都有. 真实运行的系统中,仅有增删改查功能肯定是远远不够的,ERP.CRM等系统的开发 ...

  4. CDN学习笔记二(技术详解)

    一本好的入门书是带你进入陌生领域的明灯,<CDN技术详解>绝对是带你进入CDN行业的那盏最亮的明灯.因此,虽然只是纯粹的重点抄录,我也要把<CDN技术详解>的精华放上网.公诸同 ...

  5. mysql 存储过程中limit

    1.mysql的高版本(5.5),存储过程中的limit可以使用变量,如下:select * from student limit iStart,iNum; 2.mysql的低版本(5.1),存储过程 ...

  6. Android spinner控件

    spinner控件是Android中下拉控件,现在介绍它两种用法.第一种,从资源文件中获取下拉值:第二种,从代码中获取下拉值. 第一种,首先要在资源文件中把值写好: <?xml version= ...

  7. JavaSE复习_9 集合框架复习

    △列表迭代器也是不支持在迭代的时候添加元素的,只是列表迭代器自己定义了增删的方法而已.迭代器可以看成实在两个元素之间的指针,每当调用next就跳过一个元素并返回刚刚跳过的元素. △HashTable不 ...

  8. (七)C语言中的void 和void 指针类型

    许多初学者对C中的void 和void 的指针类型不是很了解.因此常常在使用上出现一些错误,本文将告诉大家关于void 和void 指针类型的使用方法及技巧. 1.首先,我们来说说void 的含义: ...

  9. Git的优势

    分布式,强调个体 公共服务器压力和数据量都不会太大 速度快.灵活 任意两个开发者之间可以很容易的解决冲突 离线工作

  10. Linux计划任务入门详解

    Linux操作系统定时任务系统 Cron 入门 cron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业.由于Cron 是Linux的内置服务,但它不自动起来,可以用以下的方法启动 ...