[POJ1330]Nearest Common Ancestors(LCA, 离线tarjan)
题目链接:http://poj.org/problem?id=1330
题意就是求一组最近公共祖先,昨晚学了离线tarjan,今天来实现一下。
个人感觉tarjan算法是利用了dfs序和节点深度的关系,大致的意思:dfs如果不递归到递归基,那么dfs就会越递归越深,这个时候深度也是相应增加的,所以这个时候任意在已经遍历过的节点中选取两个点,计算他们的lca也就相当于是用并查集求他们的root。而dfs执行到递归基,转而执行下一个分支的时候,这个时候dfs的节点应当是小于等于之前执行到递归基的节点(叶节点)的深度的,此时再更新并查集那就自然不是和之前比此节点深度更深的节点的root了,因为那些root有可能比此点更低。
/*
━━━━━┒ギリギリ♂ eye!
┓┏┓┏┓┃キリキリ♂ mind!
┛┗┛┗┛┃\○/
┓┏┓┏┓┃ /
┛┗┛┗┛┃ノ)
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┃┃┃┃┃┃
┻┻┻┻┻┻
*/
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; #define fr first
#define sc second
#define cl clear
#define BUG puts("here!!!")
#define W(a) while(a--)
#define pb(a) push_back(a)
#define Rint(a) scanf("%d", &a)
#define Rll(a) scanf("%lld", &a)
#define Rs(a) scanf("%s", a)
#define Cin(a) cin >> a
#define FRead() freopen("in", "r", stdin)
#define FWrite() freopen("out", "w", stdout)
#define Rep(i, len) for(int i = 0; i < (len); i++)
#define For(i, a, len) for(int i = (a); i < (len); i++)
#define Cls(a) memset((a), 0, sizeof(a))
#define Clr(a, x) memset((a), (x), sizeof(a))
#define Full(a) memset((a), 0x7f7f, sizeof(a))
#define lrt rt << 1
#define rrt rt << 1 | 1
#define pi 3.14159265359
#define RT return
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef map<string, int> msi;
typedef vector<LL> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb; const int maxn = ;
int n, in[maxn];
vector<int> G[maxn];
int pre[maxn];
bool vis[maxn];
int u, v; int find(int x) {
return x == pre[x] ? x : pre[x] = find(pre[x]);
} void unite(int x, int y) {
x = find(x); y = find(y);
if(x != y) pre[y] = x;
} void dfs(int u) {
pre[u] = u;
Rep(i, G[u].size()) {
if(!vis[G[u][i]]) {
dfs(G[u][i]);
unite(u, G[u][i]);
}
}
vis[u] = ;
if(u == ::u && vis[::v]) printf("%d\n", find(::v));
if(u == ::v && vis[::u]) printf("%d\n", find(::u));
} int main() {
// FRead();
int T;
Rint(T);
W(T) {
Cls(in); Cls(vis);
Rep(i, maxn) G[i].clear();
Rint(n);
Rep(i, n-) {
Rint(u); Rint(v);
G[u].push_back(v); in[v]++;
}
Rint(u); Rint(v);
For(i, , n+) if(!in[i]) dfs(i);
}
RT ;
}
[POJ1330]Nearest Common Ancestors(LCA, 离线tarjan)的更多相关文章
- pku 1330 Nearest Common Ancestors LCA离线
pku 1330 Nearest Common Ancestors 题目链接: http://poj.org/problem?id=1330 题目大意: 给定一棵树的边关系,注意是有向边,因为这个WA ...
- 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 ...
- POJ1330 Nearest Common Ancestors
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24587 Acce ...
- poj 1330 Nearest Common Ancestors lca 在线rmq
Nearest Common Ancestors Description A rooted tree is a well-known data structure in computer scienc ...
- POJ1330 Nearest Common Ancestors(最近公共祖先)(tarjin)
A - Nearest Common Ancestors Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld &am ...
- Nearest Common Ancestors(LCA)
Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...
- 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 - 1470 Closest Common Ancestors(离线Tarjan算法)
1.输出测试用例中是最近公共祖先的节点,以及这个节点作为最近公共祖先的次数. 2.最近公共祖先,离线Tarjan算法 3. /* POJ 1470 给出一颗有向树,Q个查询 输出查询结果中每个点出现次 ...
随机推荐
- 通过注册表检测UAC是否处于关闭状态(不弹窗)
注册表路径: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System UAC各级别对应的注册表值: 从 ...
- [转载]C#时间函数
本文转自livedanta的博客的<C#时间函数> DateTime DateTime dt = DateTime.Now; dt.ToString();//2005-11-5 13:21 ...
- Asp.Net原理Version1.0
Asp.Net原理Version2.0 Asp.Net原理Version3.0_页面声明周期
- 【BZOJ】【3143】【HNOI2013】游走
数学期望/高斯消元/贪心 啊……用贪心的思路明显是要把经过次数期望越大的边的权值定的越小,那么接下来的任务就是求每条边的期望经过次数. 拆边为点?nonono,连接x,y两点的边的期望经过次数明显是 ...
- Redis 安装与配置
启动 Redis WINDOW 服务: https://github.com/ServiceStack/ServiceStack.Redis install-package ServiceStack. ...
- AlphaToCoverage solution
After msaa output the alpha in ps remove clip in ps in blendstate add AlphaToCoverageEnable = TRUE - ...
- 【锋利的JQuery-学习笔记】添加提示图片
效果图: hot图片: (注意:这个图标本身就有抖动效果的,并不是由于标签<del>而具有抖动效果) 周期性抖动,起到提示的作用 html: <div class="jnC ...
- 设置HTTP header方式
一, Server Code JSP----> <%@ page language="java" contentType="text/html; charse ...
- 恢复被win7覆盖的Ubuntu Grub
情景:本本装有Ubuntu 12.04 + Win7 32.重装Win7 64后,Ubuntu启动菜单被覆盖. 恢复的方法有多种,思路都一样.第一步,进入Linux环境:第二步.修改Grub使其重新覆 ...
- JS操作Radio与Select
//radio的chang事件,以及获取选中的radio的值 $("input[name=radioName]").on("change", function( ...