[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个查询 输出查询结果中每个点出现次 ...
随机推荐
- Google Code Jam 2015 Round1A 题解
快一年没有做题了, 今天跟了一下 GCJ Round 1A的题目, 感觉难度偏简单了, 很快搞定了第一题, 第二题二分稍微考了一下, 还剩下一个多小时, 没仔细想第三题, 以为 前两个题目差不多可以晋 ...
- Eclipse常用功能
功能(Functions):内置运行java程序的插件(JDT:java develop tools)工作集(WorkSet)(Task list)计划任务管理器(Mylyn)系统配置(Prefere ...
- thinkphp对数据库操作有哪些内置函数
getModelName() 获取当前Model的名称 getTableName() 获取当前Model的数据表名称 switchModel(type,vars=array()) 动态切换模型 tab ...
- C#中使用SelectionStart属性指定输入框光标位置
今天工作中遇到一个小BUG需要修改,需求为在文本框输入的过程中,如果数字是以0开头则自动消除0 如输入012,则显示12 很容易想到在textbox的text changed事件中判断,如果text是 ...
- Laravel5 路由问题 /home页面无法访问
参考网址:http://stackoverflow.com/questions/11791375/laravel-routes-not-working Laravel5 路由问题 /home页面无法访 ...
- 文件中的类都不能进行设计,因此未能为该文件显示设计器。设计器检查出文件中有以下类: FormMain --- 未能加载基类“WinForm.Win.FormsBase.FormMainBase”。请确保已引用该程序集并已生成所有项目
出现该问题的原因:FormMain从FormMainBase继承之后,一旦修改FormMainBase就会出现这个问题 解决方案:(1-4是搜索网友的) 1: 关闭VS所有窗口,后重启.即可返回正常. ...
- 深入浅出C++引用(Reference)类型
要点1:为反复使用的.冗长的变量名称定义一个简短的.易用的别名,从而简化了代码.通常,冗长的变量名称源于多层嵌套对象,例如类中定义嵌套类,类中定义其它类对象. //------ 未使用引用的程序片段, ...
- UML: CIM & PIM
CIM-1:定义业务流程 定义及分析业务流程(Business Process)是为了尽快理清系统范围,以便估算开发成本及时间,可不是为了要改造业务流程.系统分析员千万别误解了此步骤的目的.所以,系统 ...
- 1231: [Usaco2008 Nov]mixup2 混乱的奶牛 - BZOJ
Description 混乱的奶牛 [Don Piele, 2007] Farmer John的N(4 <= N <= 16)头奶牛中的每一头都有一个唯一的编号S_i (1 <= S ...
- c++ 枚举 在函数中的应用
#include <iostream> using namespace std; enum RespErrNo { SUCCESS = , INVALID_URL = , INVALID_ ...