HDU 1330 Nearest Common Ancestors(求两个点的近期公共祖先)
题目链接: id=1330">传送门
在线算法:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 40010; struct nod{
int to,next,w;
}edge[maxn*2]; int head[maxn],ip,tot;
bool vis[maxn];
int R[maxn*2],ver[maxn*2];
int dp[maxn*2][25];
int first[maxn];
int dis[maxn];
bool isroot[maxn]; void init(){
memset(head,-1,sizeof(head));
memset(isroot,0,sizeof(isroot));
memset(vis,false,sizeof(vis));
dis[1]=0,ip=0,tot=0;
} void add(int u,int v){
edge[ip].to=v;
edge[ip].next=head[u];
head[u]=ip++;
}
/***
ver[i]=x:第i个点是x.
first[i]=x: 点i第一次出现的位置是x
R[i]=x:第i个点的深度为x;
dis[i]=x;点i到根节点的距离为x.
***/
void dfs(int u,int dept){
vis[u]=true,ver[++tot]=u,first[u]=tot,R[tot]=dept;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(!vis[v]){
dfs(v,dept+1);
ver[++tot]=u,R[tot]=dept;
}
}
} void ST(int n){
for(int i=1;i<=n;i++) dp[i][0]=i;
for(int i=1;(1<<i)<=n;i++){
for(int j=1;j+(1<<i)<=n;j++){
int a = dp[j][i-1],b=dp[j+(1<<(i-1))][i-1];
if(R[a]<R[b]) dp[j][i]=a;
else dp[j][i]=b;
}
}
} int RMQ(int l,int r){
int k=0;
while(1<<(k+1)<=r-l+1)
k++;
int x = dp[l][k], y=dp[r-(1<<k)+1][k];
if(R[x]<R[y]) return x;
else return y;
} int LCA(int u,int v){
u=first[u],v=first[v];
if(u>v) swap(u,v);
return ver[RMQ(u,v)];
} int main(){
int t,n,m;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
init();
for(int i=0;i<n-1;i++){
int u,v,w;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
isroot[v]=1;
}
int x,y;
scanf("%d%d",&x,&y);
int root;
for(int i=1;i<=n;i++){
if(!isroot[i]){
root=i;
break;
}
}
dfs(root,1);
ST(n*2-1);
printf("%d\n",LCA(x,y));
}
return 0;
}
离线算法:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 40010; struct nod{
int u,v,next,w,lca;
}edge[maxn*2],edge1[maxn]; int par[maxn],ancestors[maxn];
int head[maxn];
int dis[maxn],ip;
int x,y;
bool vis[maxn];
bool root[maxn]; void init(){
memset(head,-1,sizeof(head));
memset(vis,false,sizeof(vis));
for(int i=0;i<maxn;i++) root[i]=true;
for(int i=1;i<maxn;i++) par[i]=i;
ip=0;
} int find_par(int x){
if(x!=par[x]) return par[x]=find_par(par[x]);
return par[x];
} void Union(int u,int v){
u=find_par(u);
v=find_par(v);
if(u!=v) par[v]=u;
} void add(int u,int v){
edge[ip].v=v;
edge[ip].next=head[u];
head[u]=ip++;
} bool ans; void tarjan(int u){
vis[u]=1;
for(int i=head[u];i!=-1;i=edge[i].next){
int v = edge[i].v;
if(!vis[v]){
dis[v]=dis[u]+edge[i].w;
tarjan(v);
Union(u,v);
}
}
if(u==x&&vis[y]&&!ans){
ans=1;
printf("%d\n",find_par(y));
return;
}
if(u==y&&vis[x]&&!ans){
ans=1;
printf("%d\n",find_par(x));
return;
}
} int main()
{
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
init();
for(int i=0;i<n-1;i++){
int u,v;
scanf("%d%d",&u,&v);
root[v]=false;
add(u,v);
add(v,u);
}
ans=0;
scanf("%d%d",&x,&y);
for(int i=1;i<=n;i++){
if(root[i]){
tarjan(i);
break;
}
}
}
return 0;
}
HDU 1330 Nearest Common Ancestors(求两个点的近期公共祖先)的更多相关文章
- poj 1330 Nearest Common Ancestors 求最近祖先节点
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37386 Accept ...
- POJ 1330 Nearest Common Ancestors(求最近的公共祖先)
题意:给出一棵树,再给出两个节点a.b,求离它们最近的公共祖先.方法一: 先用vector存储某节点的子节点,fa数组存储某节点的父节点,最后找出fa[root]=0的根节点root. 之后 ...
- 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 Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %l ...
- 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模板题】
任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000 ...
- LCA POJ 1330 Nearest Common Ancestors
POJ 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24209 ...
- POJ 1330 Nearest Common Ancestors(lca)
POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...
随机推荐
- 1019.Line Painting(线段树 离散化)
1019 离散化都忘记怎么写了 注意两个端点 离散化后用线段树更新区间 混色为-1 黑为2 白为1 因为N不大 最后直接循环标记这一段的颜色查找 #include <iostream> ...
- OK335xS can't reset with reboot
/*********************************************************************** * OK335xS can't reset * 说明: ...
- 阿里云主机安装Memcached
http://www.zyuns.com/?page_id=354 前言最近发现阿里云主机在使用中,并发访问量稍大,页面加载速度就很慢.于是学习了一些服务器优化的文章,决定安装Memcached,优化 ...
- 微信公众平台开发—利用OAuth2.0获取微信用户基本信息
在借鉴前两篇获取微信用户基本信息的基础下,本人也总结整理了一些个人笔记:如何通过OAuth2.0获取微信用户信息 1.首先在某微信平台下配置OAuth2.0授权回调页面: 2.通过appid构造url ...
- JAVACC详解
JavaCC(Java Compiler Compiler)是一个用JAVA开发的最受欢迎的语法分析生成器.这个分析生成器工具可以读取上下文无关且有着特殊意义的语法并把它转换成可以识别且匹配该语法的J ...
- 多线程与网络之JSON和XML数据的解析
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- [原]AngularJS iframe打开不同域的内容时报错误
<iframe id="myFrame" ng-src="{{url}}" width="100%" height="100 ...
- HDU-4655 Cut Pieces 数学,贪心
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4655 先不考虑相临的有影响,那么总数就是n*prod(ai),然后减去每个相邻的对总数的贡献Σ( Mi ...
- jad的用法(反编译某目录下所有class)
jad -s java -d E:\scm\MonitorServerEx\src2 -o -ff -r E:\scm\MonitorServerEx\classes-recomp\**\*.clas ...
- spring3.0的BeanFactory上下文context获取不到bean
开门见山,背景: 系统初始化的时候扫包实例化bean,然后一个工具类实现ServletContextAware接口,拿到servletContext之后: WebApplicationContextU ...