Closest Common Ancestors---poj1470(LCA+离线算法)
题目链接:http://poj.org/problem?id=1470
题意是给出一颗树,q个查询,每个查询都是求出u和v的LCA;
以下是寻找LCA的预处理过程:
void LCA(u)
{
for(u的每个儿子v)
{
LCA(v);
union(u,v);//并到一个集合中去
}
visit[u]=;
for(查询中u的每个儿子v)
{
if(visit[v])
u,v的最近公共祖先是father[getfather(v)];
}
}
图文详解
本题可以使用预处理的方式,也可以使用离线处理,由于不需要求任意两数之间的LCA所以可以使用离线算法;
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <math.h> using namespace std; #define met(a, b) memset(a, b, sizeof(a))
#define N 953
#define INF 0x3f3f3f3f
const int MOD = 1e9+; typedef long long LL; vector<int>G[N];
vector<int>Q[N]; int f[N], ans[N], vis[N]; int Find(int x)
{
if(x!=f[x])
return f[x] = Find(f[x]);
return x;
} void Union(int x, int y)
{
int px = Find(x);
int py = Find(y); f[py] = px;
} void LCA(int u)
{
for(int i=, len=G[u].size(); i<len; i++)
{
int v = G[u][i];
LCA(v);
Union(u, v);
}
vis[u] = ;
for(int i=, len=Q[u].size(); i<len; i++)
{
int v = Q[u][i];
if(vis[v])
ans[f[Find(v)]]++;
}
} int main()
{
int n, root;
while(scanf("%d", &n) != EOF)
{
for(int i=; i<N; i++)
{
G[i].clear();
Q[i].clear();
}
met(vis, ); for(int i=; i<=n; i++)
{
f[i] = i;
int u, v, cnt;
scanf("%d:(%d)", &u, &cnt);
for(int j=; j<=cnt; j++)
{
scanf("%d", &v);
G[u].push_back(v);
vis[v] = ;
}
} for(int i=; i<=n; i++)
if(!vis[i])
{
root = i;
break;
} int q, u, v; scanf("%d", &q); for(int i=; i<=q; i++)
{
scanf(" (%d%d)", &u, &v);
Q[u].push_back(v);
Q[v].push_back(u);
} met(vis, );
met(ans, ); LCA(root); for(int i=; i<=n; i++)
if(ans[i])
printf("%d:%d\n", i, ans[i]);
}
return ;
}
Closest Common Ancestors---poj1470(LCA+离线算法)的更多相关文章
- 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----(1470)Closest Common Ancestors(LCA)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 15446 Accept ...
- POJ 1470 Closest Common Ancestors 【LCA】
任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000 ...
- ZOJ 1141:Closest Common Ancestors(LCA)
Closest Common Ancestors Time Limit: 10 Seconds Memory Limit: 32768 KB Write a program that tak ...
- poj1330Nearest Common Ancestors 1470 Closest Common Ancestors(LCA算法)
LCA思想:http://www.cnblogs.com/hujunzheng/p/3945885.html 在求解最近公共祖先为问题上,用到的是Tarjan的思想,从根结点开始形成一棵深搜树,非常好 ...
- 最近公共祖先 Least Common Ancestors(LCA)算法 --- 与RMQ问题的转换
[简介] LCA(T,u,v):在有根树T中,询问一个距离根最远的结点x,使得x同时为结点u.v的祖先. RMQ(A,i,j):对于线性序列A中,询问区间[i,j]上的最值.见我的博客---RMQ - ...
- POJ 1330 Nearest Common Ancestors(LCA Tarjan算法)
题目链接:http://poj.org/problem?id=1330 题意:给定一个n个节点的有根树,以及树中的两个节点u,v,求u,v的最近公共祖先. 数据范围:n [2, 10000] 思路:从 ...
- POJ 1470 Closest Common Ancestors【LCA Tarjan】
题目链接: http://poj.org/problem?id=1470 题意: 给定若干有向边,构成有根数,给定若干查询,求每个查询的结点的LCA出现次数. 分析: 还是很裸的tarjan的LCA. ...
随机推荐
- EhCache初体验
一.简介 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点.Ehcache是一种广泛使用的开源Java分布式缓存.主要面向通用缓存,Java EE和轻量级容器.它具有内存和磁盘存 ...
- CMD打开模拟器
CMD-> CD d:\android\android-sdk-151\tools-> (这里的路径是你emulator.exe所在的路径) emulator -avd avdname-& ...
- Android 监听apk安装替换卸载广播
首先是要获取应用的安装状态,通过广播的形式 以下是和应用程序相关的Broadcast Action ACTION_PACKAGE_ADDED 一个新应用包已经安装在设备上,数据包括包名(最新安装的包程 ...
- mybayis 之resultType="map"
List<Map> publishInfos = memberShareMapper.shareToCouponCountGroupByPublishId(memberShare.getA ...
- centos7/nginx/tornado错误异常收集
临时方法 – 设置系统参数 使用命令setenforce 附: setenforce 设置SELinux 成为enforcing模式 setenforce 设置SELinux 成为permissive ...
- CoreData 多表 关联
本文转载至 http://www.jianshu.com/p/e9f3b5e0cd19 1.概念简介 coreData中存在复杂的数据关系时,一张表难以满足需求,此时就需要了解使用coreData多表 ...
- (iOS)使用auto layout进行复杂布局时,UILabel的相关trick
本文转载至 http://blog.csdn.net/madongchunqiu/article/details/47960745 本文首发于CSDN:http://blog.csdn.net/ma ...
- 《转》Python学习(18)-python函数(二)
转自 http://www.cnblogs.com/BeginMan/p/3173328.html 一.装饰器(decorators) 装饰器的语法以@开头,接着是装饰器函数的名字.可选参数. 紧跟装 ...
- cnBlogs博客推荐
数据结构和算法若可以称为为编程的细胞结构,那设计模式就是编程的灵魂气脉. 一个从是编程的微观演绎,一个是编程的宏观设计.这个从技术和艺术的结合体,毫无疑问是在世界末日之前的很伟大的一项发明. 设计 ...
- 在winform上内嵌入其它的程序
这段代码很有意义,用于把一个程序的界面嵌入到我们自己程序的某个指定窗体上. 比如在某个项目里,我需要把基恩士的激光扫描轮廓显示给客户看,但是激光的DLL中并没有这种功能提供. 于是我想先启动激光的官方 ...