poj1330Nearest Common Ancestors 1470 Closest Common Ancestors(LCA算法)
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]=;
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=; 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, , sizeof(cnt));
for(int i=; i<n; ++i){
int u, v;
scanf("%d%d", &u, &v);
g[u].push_back(v);
++cnt[v];
}
memset(vis, , sizeof(vis));
scanf("%d%d", &x, &y);
for(int i=; i<=n; ++i)
if(cnt[i]==){
LCA(i);
break;
}
printf("%d\n", ret);
for(int i=; i<=n; ++i)
g[i].clear();
}
return ;
}
#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]=;
int len; len=p[u].size();
for(int i=; i<len; ++i){
int v=p[u][i];
if(vis[v])
++ans[getFather(v)];
} len=g[u].size();
for(int i=; 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, , sizeof(cnt));
for(int i=; i<=n; ++i){
vis[i]=;
ans[i]=;
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=; i<=n; ++i)
if(cnt[i]==){
LCA(i);
break;
}
for(int i=; i<=n; ++i)
if(ans[i]!=)
printf("%d:%d\n", i, ans[i]); for(int i=; i<=n; ++i)
g[i].clear(), p[i].clear();
}
return ;
}
poj1330Nearest Common Ancestors 1470 Closest Common Ancestors(LCA算法)的更多相关文章
- POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)
POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...
- 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
题目链接:http://poj.org/problem?id=1470 Write a program that takes as input a rooted tree and a list of ...
- POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13372 Accept ...
- POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13370 Accept ...
- POJ——T 1470 Closest Common Ancestors
http://poj.org/problem?id=1470 Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 20830 ...
- POJ 1470 Closest Common Ancestors
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 17306 Ac ...
- poj——1470 Closest Common Ancestors
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 20804 Accept ...
- POJ 1470 Closest Common Ancestors【近期公共祖先LCA】
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013912596/article/details/35311489 题目链接:http://poj ...
随机推荐
- JQuery的开发与使用心得
关于jQuery的 入门使用jQuery可以很容易或具有挑战性的,这取决于你如何使用JavaScript,HTML,CSS进行开发和编程. 要知道一件重要的事情是,jQuery是只是一个JavaScr ...
- Microsoft Visual Studio 2010 已安装的模板 没有 “ADO.NET实体数据模型”
2010 sp1才包括entity framework. 装一个补丁即可 地址为:http://www.microsoft.com/zh-CN/download/details.aspx?id=236 ...
- delphi7 编译程序时报win32.indcu.a病毒的解决方法
Delphi7用了很久一直都没问题,同一个工程文件昨天编译时mod32还不会报毒,今天重新编译时,生成的exe突然nod32报毒. 提示: “Project1.exe Win32/Induc.A 病毒 ...
- 00.PHP学习建议
各位师弟师妹,大家好~PHP不是我们专业的本该有的方向.我不知道大家为什么来学习这门语言,也许是自己了解之后喜欢这门语言(我想这种可能在我们专业是挺少的),也许是听守中哥说这门语言简单好学,为了躲避学 ...
- ASP.NET 导出gridview中的数据到Excel表中,并对指定单元格换行操作
1. 使用NPOI读取及生成excel表. (1)导出Click事件: 获取DataTable; 给文件加文件名: string xlsxName = "xxx_" + DateT ...
- spring 装配核心笔记
(1)自动装配 开启ComponentScan(自动扫描), 通过在类使用注解@Component(默认bean id为类名第一个字符小写), 使用@Autowired实现属性,构造函数,成员函数的自 ...
- UWP开发笔记——嵌套式页面的实现
绪论 UWP开发中,Page是最常用的Control之一,通常情况下,在开发的application中,每一个页面就是一个Page.有时候,为了开发整合度更高,UI表现更为一致的UI,开发者需要把UI ...
- dojo的发展历史
dojo的开始要从2004年初开始说起,那时dojo之父 Alex Russell 在Informatica公司内从事一个名为netWindows的项目,这个项目的目的是在浏览器环境下提供创建窗口化界 ...
- 项目八:团队项目——Alpha阶段项目总结
1.项目的预期目标 a.完成游戏的基本功能 b.游戏难度的玩家手动调节 c.游戏能够良好的运行完成 与前期的需求分析对比: 第一点不同是游戏的难度调节,原来是想通过选择难度来调节的,但由于难度的分层上 ...
- 防止开发人员获取到敏感数据(SQL Server的数据加密简介)
背景 有时候,我们还真的会碰到这样的需求:防止开发人员获取到敏感数据.也许你觉得很简单,把开发和运营分开不就可以了吗?是的,如果公司有专门的运营团队的话,但对于很多小公司来说,几个人的开发团队就兼顾了 ...