【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 ...
随机推荐
- oracle获取主机服务器IP
--要获取服务器端的IP :: SYS@XXX> select utl_inaddr.get_host_address from dual; GET_HOST_ADDRESS --------- ...
- Java面向对象编程三大特性 --- 多态
多态特性: 子类Child继承父类Father,我们可以编写一个指向子类的父类类型引用,该引用既可以处理父类Father对象,也可以处理子类Child对象,当相同的消息发送给子类或者父类对象时,该对象 ...
- MyBatis的SQL语句映射文件详解(二)----增删改查
1.select语句 public List<User> findUser() { // TODO Auto-generated method stub List users= ( ...
- struts2和jstl有关循环的写法
一:前言 其实觉得自己现在就是个码农啊,对于struts2的标签和jstl的标签我一直都是只会用,但是觉得自己老是会混淆这种概念性的问题.所以我自己在代码里面就试着用了几种方式,实现同一种效果,下面就 ...
- 经典DFS问题 oilland 连通块
#include "iostream" #include "cstdio" using namespace std; ][]={{,},{,-},{,},{-, ...
- bzoj 3196二逼平衡树 线段树套平衡树
比较裸的树套树,对于区间K值bz上有一道裸题,详见题解http://www.cnblogs.com/BLADEVIL/p/3455336.html(其实题解也不是很详细) //By BLADEVIL ...
- iOS float小数四舍五入
http://blog.csdn.net/fanjunxi1990/article/details/21536189 直接贴代码了 #import "ViewController.h&quo ...
- camera摄像原理之四:曝光和GAMMA【转】
转自:http://blog.csdn.net/ghostyu/article/details/7912880 从最明亮到最黑暗,假设人眼能够看到一定的范围,那么胶片(或CCD 等电子感光器件)所能表 ...
- Github上的几个C++开源项目
Github上的几个C++开源项目 http://blog.csdn.net/fyifei0558/article/details/47001677 http://www.zhihu.com/ques ...
- PL/SQL Developer 连接 Oracle
1.从官网http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html 选择instant ...