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 ...
随机推荐
- awk 以HWI开头,并且:相邻两行的第一个字段完全相同;
## 思路:以HWI开头,并且:相邻两行的第一个字段完全相同:awk 'BEGIN{ last_col_1="xxxxxx"; last_row="bbbbbbbbbbb ...
- 9.Parameters
1.Optional and Named Parameters calls these methods can optionally not specify some of the arguments ...
- [C和指针]第一部分
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- CUBRID学习笔记 14 dll加载错误
这个问题通常是缺少文件cascci.dll 或者版本错误 32 64弄错了 C:\Program Files (x86)\Python266>python.exe Python 2.6.6 (r ...
- Linux netmask
wget http://mirrors.sohu.com/ubuntu/pool/universe/n/netmask/netmask_2.3.12.tar.gz tar xf netmask_2.3 ...
- hdu 5673 Robot 卡特兰数+逆元
Robot Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem D ...
- new的深一步
new的深一步 new运算符 用于创建对象和条用构造函数 new修饰符 用于隐藏基类中被继承的成员 new约束 用于在泛型声明中约束可能用作类型参数的参数类型 new运算符 用于创建对象和调用构造函数 ...
- java 解析汉字拼音
pinyin4j的使用很方便,一般转换只需要使用PinyinHelper类的静态工具方法即可: String[] pinyin = PinyinHelper.toHanyuPinyinStrin ...
- Java I/O 对象序列化
我们知道对象的持持久化有三种方式: 1: 对象序列化 2: XML 3: 数据库技术 序列化可以帮助使得对象的生命周期不取决与程序是否正在执行,它可以生存于程序的调用之间. 只要将任何对象序列化到单一 ...
- NPN&PNP
一.晶体管基础知识 晶体管分2种:NPN.PNP 晶体管通常封装为TO-92,下面是元件实物图 和 元件符合: NPN: 当电压和电流被加到基极上时,NPN晶体管: 其工作原理: 就像水龙头—给控制开 ...