【LCA倍增】POJ1330-Nearest Common Ancestors
【知识点:离线算法&在线算法】
一个离线算法,在开始时就需要知道问题的所有输入数据,而且在解决一个问题后就要立即输出结果。
一个在线算法是指它可以以序列化的方式一个个的处理输入,也就是说在开始时并不需要已经知道所有的输入。
【倍增-在线算法】
水题……这道题范围卡得很紧…数组少了一位就会WA。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN=+;
const int DEG=;
vector<int> E[MAXN];
int n;
int anc[MAXN][DEG],dep[MAXN]; void addedge(int u,int v)
{
E[u].push_back(v);
} void dfs(int v,int p,int d)
{
anc[v][]=p;
dep[v]=d;
for (int i=;i<E[v].size();i++)
dfs(E[v][i],v,d+);
} void getanc()
{
for (int i=;i<DEG;i++)
for (int j=;j<=n;j++)
{
anc[j][i]=anc[anc[j][i-]][i-];
}
} int swim(int x,int H)
{
for (int i=;H>;i++)
{
if (H&) x=anc[x][i];
H/=;
}
return x;
} int LCA(int u,int v)
{
if (dep[u]<dep[v]) swap(u,v);
u=swim(u,dep[u]-dep[v]);
if (u==v) return u;
for (int i=DEG-;i>=;i--)
{
if (anc[u][i]!=anc[v][i])
{
u=anc[u][i];
v=anc[v][i];
}
}
return anc[u][];
} void init()
{
for (int i=;i<MAXN;i++) vector<int>().swap(E[i]);
scanf("%d",&n);
int notrt[MAXN];
memset(notrt,,sizeof(notrt));
for (int i=;i<n-;i++)
{
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
notrt[v]=;
}
int rt;
for (int i=;i<=n;i++) if (!notrt[i]) //这里注意下标是从1开始的
{
rt=i;
break;
}
dfs(rt,,);
getanc();
} void query()
{
int u,v;
scanf("%d%d",&u,&v);
cout<<LCA(u,v)<<endl;
} int main()
{
int T;
scanf("%d",&T);
for (int kase=;kase<T;kase++)
{
init();
query();
}
return ;
}
【待更新】
【LCA倍增】POJ1330-Nearest Common Ancestors的更多相关文章
- POJ1330 Nearest Common Ancestors
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24587 Acce ...
- LCA POJ 1330 Nearest Common Ancestors
POJ 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24209 ...
- POJ1330 Nearest Common Ancestors(最近公共祖先)(tarjin)
A - Nearest Common Ancestors Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld &am ...
- [POJ1330]Nearest Common Ancestors(LCA, 离线tarjan)
题目链接:http://poj.org/problem?id=1330 题意就是求一组最近公共祖先,昨晚学了离线tarjan,今天来实现一下. 个人感觉tarjan算法是利用了dfs序和节点深度的关系 ...
- POJ1330 Nearest Common Ancestors (JAVA)
经典LCA操作.. 贴AC代码 import java.lang.reflect.Array; import java.util.*; public class POJ1330 { // 并查集部分 ...
- 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
POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节 ...
- 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,dfs+ST在线算法)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14902 Accept ...
随机推荐
- UVA10480:Sabotage(最小割+输出)
Sabotage 题目链接:https://vjudge.net/problem/UVA-10480 Description: The regime of a small but wealthy di ...
- nginx 静态文件支持跨域访问权限
一.原生态 location ^~ /repurchase-web/ { alias /var/www/webapps/repurchase-web/; } 二.支持跨 ...
- HDU2571--命运---DP
http://acm.hdu.edu.cn/showproblem.php?pid=2571 #include "iostream" #include "cstdio&q ...
- Spring - IoC(2): 属性注入 & 构造注入
依赖注入是指程序运行过程中,如果需要另外的对象协作(访问它的属性或调用它的方法)时,无须在代码中创建被调用者,而是依赖于外部容器的注入. 属性注入(Setter Injection) 属性注入是指 I ...
- [POJ1180&POJ3709]Batch Scheduling&K-Anonymous Sequence 斜率优化DP
POJ1180 Batch Scheduling Description There is a sequence of N jobs to be processed on one machine. T ...
- 02-导航实例-storyboard实现
源代码下载链接:02-导航实例-storyboard实现.zip38.5 KB // MJAboutViewController.h // // MJAboutViewController. ...
- [ CodeVS冲杯之路 ] P2952
不充钱,你怎么AC? 题目:http://codevs.cn/problem/2952/ 题目讲一个细胞可分裂成 2 个,那么当前数目就是2a,a 为时间 然后 q 个细胞一起会死亡,也就是对 q 取 ...
- Linux curl命令【curl】
命令:curl 在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具.它支持文件的上传和下载,是综合传输工具,但按传统,习惯称url为下载工具 ...
- CSS3 渐变(Gradients)
参考: http://www.runoob.com/css3/css3-gradients.html CSS3 渐变(gradients)可以让你在两个或多个指定的颜色之间显示平稳的过渡. 以前,你必 ...
- UVA 10385 Duathlon
Problem HDuathlonInput: standard inputOutput: standard outputTime Limit: 15 seconds A duathlon is a ...