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. ...
随机推荐
- Jsoup(三)-- Jsoup使用选择器语法查找DOM元素
1.Jsoup可以使用类似于CSS或jQuery的语法来查找和操作元素. 2.实例如下: public static void main(String[] args) throws Exception ...
- CentOS7--配置时间和日期
CentOS7提供三个命令行工具,可用于配置和显示有关系统日期和时间的信息. timedatectl:Linux 7中的新增功能,也是systemd其中的一部分. date:系统时钟,也成为软件时钟, ...
- java 对 汉字排序(按照拼音字母排序)
业务场景: 一个list集合,里面add了若干个实体类,针对该实体类排序的属性为String. 使用技术,自定义list排序(JDK自带),重写Comparator接口的compare方法,汉字转拼音 ...
- 怎样在js中使用EL表达式
相信已经有很多人对如何在js中使用EL表达式存有困惑,各种引号的处理不胜其烦. 1.在js(嵌入jsp页面)中通过定义变量的方式使用EL表达式: 如:var url = '${param.url}'; ...
- 《转载》Eclipse项目上传码云
本文转载自http://blog.csdn.net/izzyliao/article/details/53074452 把Eclipse项目上传到码云的步骤: 1.登录码云:新建项目 2.输入项目名: ...
- ldap 测试表设计
1. ldap_oc_mappings 存储objeckClass 信息 表结构: Column Desc. id objectClass的唯一标识 name objectClass的名称 k ...
- [微信小程序]计算自己手机到指定位置的距离
目的: 根据目的地的坐标计算自己手机的位置离目的地的距离的 核心思路: 后续操作必须等所有异步请求都返回了才能继续 使用 const qqmap = require("../../utils ...
- 【jquery基础】 jquery.manifest用法:通过后台查询and添加到默认项
今天做一个东西 效果如下: 后台已经保存了006这个SN码,现在需要查到了这个人(杨小婷),然后作为默认值,展示到 manifest 里面 <script> $(document).rea ...
- 浅谈CSS盒子模型
[摘要]盒子模型是CSS中的一个重要概念,虽然CSS中没有盒子这个单独的属性对象,但它却是CSS中无处不在的一个重要组成部分.掌握盒子模型的原理和使用方法可以极大地丰富HTML元素的表现效果,同时对于 ...
- sencha touch NavigationView 源码详解(注释)
Ext.define('Ext.navigation.View', { extend: 'Ext.Container', alternateClassName: 'Ext.NavigationView ...