POJ 1330 Nearest Common Ancestors(LCA Tarjan算法)
题目链接:http://poj.org/problem?id=1330
题意:给定一个n个节点的有根树,以及树中的两个节点u,v,求u,v的最近公共祖先。
数据范围:n [2, 10000]
思路:从树根出发进行后序深度优先遍历,设置vis数组实时记录是否已被访问。
每遍历完一棵子树r,把它并入以r的父节点p为代表元的集合。这时判断p是不是所要求的u, v节点之一,如果r==u,且v已访问过,则lca(u, v)必为v所属集合的代表元。p==v的情况类似。
我的第一道LCA问题的Tarjan算法,题目只有唯一的一组查询,实现起来非常简洁。
注意题目给树的格式:给出n-1个数对<u, v>,u为v的父节点。因此可以当作有向图用邻接表存储,同时记录各个节点的入度,入度为0的点为树根。
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int MAX_N = ;
int parent[MAX_N];
void init(){
for(int i=; i<MAX_N; i++){
parent[i] = i;
}
}
int find(int x){
if(parent[x] == x) return x;
return parent[x] = find(parent[x]);
}
void unite(int x, int y){
x = find(x);
y = find(y);
if(x == y) return ;
parent[y] = x;
}
bool same(int x, int y){
return find(x) == find(y);
}
vector<int> G[MAX_N];
int u, v;
int T;
int n;
int vis[MAX_N];
int indeg[MAX_N];
void dfs(int r){
//printf("%d\n", r);
for(int i=; i<G[r].size(); i++){
if(!vis[G[r][i]]){
dfs(G[r][i]);
unite(r, G[r][i]);//孩子合并到父节点
}
}
vis[r] = ; //后序遍历
if(r == u && vis[v]){
printf("%d\n", find(v));
return ;
}else if(r == v && vis[u]){
printf("%d\n", find(u));
return ;
}
}
void lca(){
memset(vis, , sizeof(vis));
init();
int r = ;
for(int i=; i<=n; i++){
if(indeg[i]==){
//printf("root : %d\n", i); //入度为0的是树根
dfs(i);
}
}
}
int main()
{
scanf("%d", &T);
while(T--){
scanf("%d", &n);
memset(indeg, , sizeof(indeg));
for(int i=; i<MAX_N; i++) G[i].clear();
for(int i=; i<n-; i++){
scanf("%d%d", &u, &v);
G[u].push_back(v);//有向图
indeg[v]++;
}
scanf("%d%d", &u, &v);
lca();
}
return ;
}
POJ 1330 Nearest Common Ancestors(LCA Tarjan算法)的更多相关文章
- POJ 1330 Nearest Common Ancestors (LCA,倍增算法,在线算法)
/* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...
- POJ.1330 Nearest Common Ancestors (LCA 倍增)
POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...
- POJ 1330 Nearest Common Ancestors LCA题解
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19728 Accept ...
- poj 1330 Nearest Common Ancestors lca 在线rmq
Nearest Common Ancestors Description A rooted tree is a well-known data structure in computer scienc ...
- poj 1330 Nearest Common Ancestors LCA
题目链接:http://poj.org/problem?id=1330 A rooted tree is a well-known data structure in computer science ...
- POJ 1330 Nearest Common Ancestors(LCA模板)
给定一棵树求任意两个节点的公共祖先 tarjan离线求LCA思想是,先把所有的查询保存起来,然后dfs一遍树的时候在判断.如果当前节点是要求的两个节点当中的一个,那么再判断另外一个是否已经访问过,如果 ...
- POJ 1330 Nearest Common Ancestors 倍增算法的LCA
POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节 ...
- POJ - 1330 Nearest Common Ancestors(基础LCA)
POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %l ...
- POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...
- POJ 1330 Nearest Common Ancestors(lca)
POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...
随机推荐
- perfmon——使用windows系统自带的性能监视器监控进程信息
第一次使用perfmon监控应用进程的信息,步骤总结如下: 第一部分 性能监视器 1.快捷键Win+R打开运行界面,输入“perfmon”命令后回车即可打开windows的性能监视器 2.点击“性能监 ...
- List与Array互相转换
List转换为Array可以这样处理: ArrayList<String> list=new ArrayList<String>(); String[] strings = n ...
- [sh]rm -rf*的防护和普通用户执行命令
尽量用普通用户执行,因为普通用户无法删除root的文件,避免误删除 rm -rf 不可取, 尽量find+rm -rf 尽量cd && rm -rf * 加上逻辑 cd /tmp/re ...
- iot平台在k8s搭建过程
统一在 cd /opt/iot nohup /opt/iopservices.sh >/var/log/helmapi.log & 直接查看pod日志? kubectl logs i ...
- (转)以太坊 钱包 创建 导入 Keystore
最近闲来无事 研究了下以太坊钱包 下边分享下 准备工作 : 需要用到的加密:BIP32 BIP39 BIP44 SCRYPT 加密算法 githab地址 https://github.com/Nova ...
- 使用web3j工具生成java版本的智能合约
这里需要使用的环境 web3j,nodejs 安装编译sol工具 $ npm install -g solc 保存为hello.sol文件到本地 pragma solidity 0.4.19; con ...
- mac nginx compile
编译 ./configure \ --prefix=/usr/local/services/nginx-1.14.0 \ --with-openssl=/Users/gavin/Downloads/s ...
- 关于随机数、方法重载和System.out.println()的认识
(1)使用纯随机数发生器编写一个指定数目内数字的程序(类真随机数) 源代码: package Demo1; public class trueRandom { long Multiplier = 45 ...
- 解决Nginx重启时提示nginx: [emerg] bind() to 0.0.0.0:80错误
Nginx是一款轻量级的Web服务器,特点是占有内存少,并发能力强,因而使用比较广泛,蜗牛今天在一个VPS上重启Nginx时提示“nginx: [emerg] bind() to 0.0.0.0:80 ...
- 219. 存在重复元素 II
给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值最大为 k. 示例 1: 输入: nums = ...