题目链接: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+离线算法)的更多相关文章

  1. POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13372   Accept ...

  2. POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13370   Accept ...

  3. poj----(1470)Closest Common Ancestors(LCA)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 15446   Accept ...

  4. POJ 1470 Closest Common Ancestors 【LCA】

    任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000 ...

  5. ZOJ 1141:Closest Common Ancestors(LCA)

    Closest Common Ancestors Time Limit: 10 Seconds      Memory Limit: 32768 KB Write a program that tak ...

  6. poj1330Nearest Common Ancestors 1470 Closest Common Ancestors(LCA算法)

    LCA思想:http://www.cnblogs.com/hujunzheng/p/3945885.html 在求解最近公共祖先为问题上,用到的是Tarjan的思想,从根结点开始形成一棵深搜树,非常好 ...

  7. 最近公共祖先 Least Common Ancestors(LCA)算法 --- 与RMQ问题的转换

    [简介] LCA(T,u,v):在有根树T中,询问一个距离根最远的结点x,使得x同时为结点u.v的祖先. RMQ(A,i,j):对于线性序列A中,询问区间[i,j]上的最值.见我的博客---RMQ - ...

  8. POJ 1330 Nearest Common Ancestors(LCA Tarjan算法)

    题目链接:http://poj.org/problem?id=1330 题意:给定一个n个节点的有根树,以及树中的两个节点u,v,求u,v的最近公共祖先. 数据范围:n [2, 10000] 思路:从 ...

  9. POJ 1470 Closest Common Ancestors【LCA Tarjan】

    题目链接: http://poj.org/problem?id=1470 题意: 给定若干有向边,构成有根数,给定若干查询,求每个查询的结点的LCA出现次数. 分析: 还是很裸的tarjan的LCA. ...

随机推荐

  1. 【代码审计】CmsEasy_v5.7 代码执行漏洞分析

      0x00 环境准备 CmsEasy官网:http://www.cmseasy.cn/ 网站源码版本:CmsEasy_v5.7_UTF8-0208 程序源码下载: http://ftp.cmseas ...

  2. PHP代码审计笔记--文件包含漏洞

    有限制的本地文件包含: <?php include($_GET['file'].".php"); ?> %00截断: ?file=C://Windows//win.in ...

  3. Python中常见的字符串小笔试题

    1.获取实现两个字符串中最大的公共子串 思路:    1.比较两个字符串的长度 2.获取较短字符串的所有子串 3.使用__contains__函数进行比较 4.把子串当做键,子串长度作为值,存入字典, ...

  4. javaweb项目中表单生成的验证码以及校验

    首先先来看一下项目的结构吧,有两个servlet,一个是进行验证码的生成以及存储的,一个是进行校验的,还有一个jsp页面是用来实现form表单的书写和展示: 我们只需要看这三个就行了,其他的自动忽略: ...

  5. 在 Core Data 中存取 transformable 类型的数据

    本文转载至 http://imenjoe.com/2015/04/10/CoreData-transformable-20150410/ 在开发过程中有一个需要在 Core Data 中存取 NSDi ...

  6. JDBC批量加密数据库密码

    package com.lxc.wmb; import java.io.UnsupportedEncodingException; import java.security.MessageDigest ...

  7. No.3 PyQt学习

    使用box布局,写了 一个系统的主页(非常丑) 代码如下: # -*- coding: utf-8 -*- import sys from PyQt4.QtGui import * from PyQt ...

  8. 深入理解 Neutron -- OpenStack 网络实现(1):GRE 模式

    问题导读1.什么是VETH.qvb.qvo?2.qbr的存在的作用是什么?3.router服务的作用是什么? 如果不具有Linux网络基础,比如防火墙如何过滤ip.端口或则对openstack ovs ...

  9. 【Eclipse】Ubuntu 下菜单栏失效了,怎么办?(已解决)

    如果你的 Ubuntu 的版本是 13.10 , 且你又安装了 Eclipse , 你就会发现 Eclipse 的菜单不起作用了. 就是点击 File , Edit ... 这些菜单,不会显示子菜单了 ...

  10. jQuery属性操作(三)

    在阅读attr.remove方法时,看到一些对浏览器兼容性做处理的hooks.接下来看一下这些hooks都做了哪些兼容性处理 1.attrHooks.主要处理IE6-9 input的type属性无法写 ...