题目链接: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. DATAGUARD的搭建

    ORACLE Data Guard 理论知识 请查看此blog :http://blog.csdn.net/haibusuanyun/article/details/11519241 Oracle D ...

  2. JSON XSS

    漏洞实例一: 1.在更新用户信息,修改联系电话,抓包绕过前端20个字符限制,Payload为 111<img src=1 onerror=alert(1)> 2.更新后,访问json 3. ...

  3. HttpClient(五)-- 模拟表单上传文件

    1.maven依赖 <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId ...

  4. 分布式计算开源框架Hadoop入门实践

    目录(?)[+] Author :岑文初 Email: wenchu.cenwc@alibaba-inc.com msn: cenwenchu_79@hotmail.com blog: http:// ...

  5. Oracle和Mysql中mybatis模糊匹配查询区别

    1.Oracle AND NAME LIKE '%'||#{name}||'%' 2.Mysql AND NAME LIKE "%"#{name}"%"

  6. 【Spring源码分析系列】启动component-scan类扫描加载过程

    原文地址:http://blog.csdn.net/xieyuooo/article/details/9089441/ 在spring 3.0以上大家都一般会配置一个Servelet,如下所示: &l ...

  7. C#设计模式--设配器模式

    0.C#设计模式-简单工厂模式 1.C#设计模式--工厂方法模式 2.C#设计模式--抽象工厂模式 3.C#设计模式--单例模式 4.C#设计模式--建造者模式 5.C#设计模式--原型模式 设计模式 ...

  8. CDH localhost:7180 页面无法打开

    有时会遇到CDH集群的7180页面无法访问,通过命令查看服务发现: service --status-all cloudera-scm-server dead but pid file exists ...

  9. tcp连接出现close_wait状态?可能是代码不够健壮

    一.问题概述 今天遇到个小问题. 我们的程序依赖了大数据那边的服务,大数据那边提供了restful接口供我们调用. 测试反映接口有问题,我在本地重现了. 我这边感觉抓包可能对分析问题有用,就用wire ...

  10. 【BZOJ5133】[CodePlus2017年12月]白金元首与独舞 矩阵树定理

    [BZOJ5133][CodePlus2017年12月]白金元首与独舞 题面:www.lydsy.com/JudgeOnline/upload/201712/div1.pdf 题解:由于k很小,考虑用 ...