poj----1330Nearest Common Ancestors(简单LCA)
题目连接 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)的更多相关文章
- POJ:1330-Nearest Common Ancestors(LCA在线、离线、优化算法)
传送门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K ...
- POJ 1470 Closest Common Ancestors 【LCA】
任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000 ...
- POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13372 Accept ...
- POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14902 Accept ...
- POJ 1330 Nearest Common Ancestors(lca)
POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...
- POJ 1330 Nearest Common Ancestors 【LCA模板题】
任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000 ...
- poj 1330 Nearest Common Ancestors(LCA 基于二分搜索+st&rmq的LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 30147 Accept ...
- POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13370 Accept ...
- poj----(1470)Closest Common Ancestors(LCA)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 15446 Accept ...
- ZOJ 1141:Closest Common Ancestors(LCA)
Closest Common Ancestors Time Limit: 10 Seconds Memory Limit: 32768 KB Write a program that tak ...
随机推荐
- ubuntu中rar与unrar用法详解
本文转载:http://helloklzs.iteye.com/blog/1139993 安装: sudo apt-get install rar 这样就可以安装了 删除是以下语句 sudo apt- ...
- Some Useful Property Settings Explained Of Oracle Forms
In Oracle forms when we have two or more blocks and there is a requirement to join them or make a re ...
- Java开发中经典的小实例-(while(参数){})
import java.util.Scanner;public class Test_while { public static void main(String[] args) { ...
- Servlet异常及其生命周期
Servlet 异常 在javax.servlet包中定义了两个异常类 ServletException类 ServletException类定义了一个通用的异常,可以被init().service( ...
- MyBatis 动态SQL查询,多条件,分页
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...
- yii 中引入js 和css 的方式
在yii中 我们需要引入css 和 js 的时候,yii 自身有需要的类. 当我在views 视图层中引入css 和 js , <?php Yii::app()->clientScript ...
- 利用CGLib实现动态代理实现Spring的AOP
当我们用Proxy 实现Spring的AOP的时候, 我们的代理类必须实现了委托类的接口才能实现. 而如果代理类没有实现委托类的接口怎么办? 那么我们就可以通过CGLib来实现 package cn. ...
- iOS - OC 内存管理
1.OC 基本内存管理模型 1.1 自动垃圾收集 在 OC 2.0 中,有一种称为垃圾收集的内存管理形式.通过垃圾收集,系统能够自动监测对象是否拥有其他的对象,当程序执行需要空间的时候,不再被引用的对 ...
- struts2 if标签示例
下面总结一下struts2 中if标签的使用 (1)判断字符串是否为空 <s:if test="user.username==null or user.username==''&quo ...
- Linux_查看进程
1. 静态: ps -aux 2. 动态: top 3.