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. ...
随机推荐
- Data Guard 主备库角色转换
1. switchover操作 1.1 备库先关闭实时日志应用 standby>alter database recover managed standby database cancel; 1 ...
- 【RF库Built-In测试】Catenate
Name:CatenateSource:BuiltIn <test library>Arguments:[ *items ]Catenates the given items togeth ...
- Android studio导入eclipse工程时出现中文全部乱码问题
File->settings->Copyright的下面File Encoding -> 改成gbk这样修改就OK了
- 2014 华为校招机试题(c/c++开发类)
第一题: 1.2.3....n盏灯,同时有n个人, 第1个人将1的倍数的灯拉一下, 第2个人将2的倍数的灯拉一下, ...... 问最后有几盏灯是亮的, 初始状态下灯是灭的, 输入整数n(n<6 ...
- iOS - 栈顶VC控制器的获取
iOS 获取当前顶层的ViewController #pragma mark - topVC - (UIViewController *)theTopviewControler { UIViewCon ...
- python基础---->python的使用(五)
这里记录一些python的一些基础知识,主要内容是高阶函数的使用.或许我的心包有一层硬壳,能破壳而入的东西是极其有限的.所以我才不能对人一往情深. python中的高阶函数 一.map().reduc ...
- 法律&道德
西弗森是美国加州一名95岁的老妇人,2010年12月份的一天,她在家清理房间,当她翻开一叠纸的时候,一本书从里面掉了下来,她弯腰拾起来,发现是一本名叫<水上飞机独自飞>的书,再一看书页里的 ...
- List的五种去重方式
//set集合去重,不改变原有的顺序 public static void pastLeep1(List<String> list){ System.out.println("l ...
- mac下升级terminal/终端的subversion版本方法
雖然現在程式碼管理已經以 Git 為主了,不過偶爾需要維護一些舊案子還是會用 SVN,懶得轉了. Mac OS 本身有內建 SVN,不過卻是 1.6 版,最近修改一個舊案子就有碰到 project 已 ...
- ubuntu14.04_install_gitlab_platform
/** author: lihaibo URL:http://www.cnblogs.com/horizonli/p/5321770.html */ 下面是干货 [第一部分 安装] 环境:ubuntu ...