POJ1330Nearest Common Ancestors——近期公共祖先(离线Tarjan)
id=1330
给一个有根树,一个查询节点(u,v)的近期公共祖先
836K 16MS
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
const int maxn=10005;
using namespace std;
int n,m;//结点数,查询数
int f[maxn];//并查集
bool vis[maxn];//訪问标记
int ancestor[maxn];//祖先
int answer[2];//存储最后的查询结果
int in[maxn];//入度
vector<int> que[maxn];
struct Edge{
int to;
int next;
}edge[maxn<<1];int head[maxn],tot;
void addedge(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void Init(){
tot=0;
memset(in,0,sizeof(in));
memset(f,-1,sizeof(f));
memset(head,-1,sizeof(head));
memset(vis,false,sizeof(vis));
memset(ancestor,0,sizeof(ancestor));
for(int i=1;i<=n;++i){
que[i].clear();
}
}
int find(int x){
return f[x]==-1 ?
x:f[x]=find(f[x]);
}
void Union(int u,int v){
int t1=find(u);
int t2=find(v);
if(t1!=t2) f[t2]=t1;
}
void LCA(int u){
ancestor[u]=u;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(vis[v]) continue;
LCA(v);
Union(u,v);
ancestor[find(v)]=u;//可省略
}
vis[u]=true;
int sz=que[u].size();
for(int i=0;i<sz;++i){
int v=que[u][i];
if(vis[v]){
printf("%d\n",ancestor[find(v)]);//可替换find(v)
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
int T,u,v,w;
cin>>T;
while(T--){
scanf("%d",&n);
Init();
for(int i=0;i<n-1;++i){
scanf("%d%d",&u,&v);
if(u!=v){
addedge(u,v);
in[v]++;
}
}
scanf("%d%d",&u,&v);
que[u].push_back(v);
que[v].push_back(u);
for(int i=1;i<=n;++i){
if(in[i]==0){
LCA(i);
break;
}
}
}
return 0;
}
POJ1330Nearest Common Ancestors——近期公共祖先(离线Tarjan)的更多相关文章
- POJ1330Nearest Common Ancestors最近公共祖先LCA问题
用的离线算法Tarjan 该算法的详细解释请戳 http://www.cnblogs.com/Findxiaoxun/p/3428516.html 做这个题的时候,直接把1470的代码copy过来,改 ...
- 图论-最近公共祖先-离线Tarjan算法
有关概念: 最近公共祖先(LCA,Lowest Common Ancestors):对于有根树T的两个结点u.v,最近公共祖先表示u和v的深度最大的共同祖先. Tarjan是求LCA的离线算法(先存储 ...
- 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18136 Accept ...
- POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13372 Accept ...
- POJ1330 Nearest Common Ancestors(最近公共祖先)(tarjin)
A - Nearest Common Ancestors Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld &am ...
- POJ 1330 LCA最近公共祖先 离线tarjan算法
题意要求一棵树上,两个点的最近公共祖先 即LCA 现学了一下LCA-Tarjan算法,还挺好理解的,这是个离线的算法,先把询问存贮起来,在一遍dfs过程中,找到了对应的询问点,即可输出 原理用了并查集 ...
- POJ - 1330 Nearest Common Ancestors 最近公共祖先+链式前向星 模板题
A rooted tree is a well-known data structure in computer science and engineering. An example is show ...
- POJ1470Closest Common Ancestors 最近公共祖先LCA 的 离线算法 Tarjan
该算法的详细解释请戳: http://www.cnblogs.com/Findxiaoxun/p/3428516.html #include<cstdio> #include<alg ...
- [HDOJ2586]How far away?(最近公共祖先, 离线tarjan, 并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 这题以前做过…现在用tarjan搞一发…竟然比以前暴力过的慢………… 由于是离线算法,需要Que ...
随机推荐
- 图标字体的使用(fontello.com)字体推荐及使用技巧
网页设计中为了页面漂亮好看,图标是少不了,网页中使用的图标通常都是使用图片,使用图片图标的有很多弊端,如果你经常制作网页应该有一肚子埋怨. 使用图片图标的弊端 放大图标必须重新作图, 改变颜色必须开启 ...
- JS+CSS实现选项卡功能
[小小一记] 首先我们写一个选项卡的结构出来,包括tab和content: 首先是tab: <ul class="ttitle-box-tabs" id="tabs ...
- CANoe 入门 Step by step系列(一)基础应用【转】
CANoe是Vector公司的针对汽车电子行业的总线分析工具,现在我用CANoe7.6版本进行介绍,其他版本功能基本差不多. 硬件我使用的是CAN case XL. 1,CANoe软件的安装很简单,先 ...
- 查看yum包安装地址
首先找到包含版本号在内的全包名 rpm -qa|grep t_dp_apsara_exstoret_dp_apsara_exstore-1.0.5-56 然后就可以查询到了 rpm -ql t_dp_ ...
- git+Coding.netの小试牛刀
一.将本地项目推送到Coding中 1.在Coding中新建项目,填写项目名称和项目描述,设置属性,勾选初始化仓库
- Codeforces 731D Funny Game
Description Once upon a time Petya and Gena gathered after another programming competition and decid ...
- js compress and combine
http://www.cnblogs.com/snandy/archive/2012/06/08/2541827.html http://developer.51cto.com/art/201301/ ...
- linux里的php使用phpize拓展各种功能(curl,zip,gd等等)
这里的实验以拓展zip功能为实例,成功使用zip功能需要如下步骤: 1.下载zip拓展包,并解压,并进入zip文件夹 tar -zxvf zip.tar.gz // 解压 cd zip //进入文件夹 ...
- 使用Sass和Compass组合写CSS
最近开始在尝试开始使用Sass来写CSS代码,刚开始虽然还是不太习惯用链式的方式写css,不过这是暂时的阶段. 如果你还不了解Sass,可以看之前发表过的文章来了解详情,Sass主要有下面这几种特性( ...
- 关于memory 和 cache
这篇文章介绍了cache的相关知识,包括全相关.组相关.缓存与置换策略.WriteThrough和WriteBack策略.cache体系.CPUID和缓存一致性.介绍了两种缓存一致性的协议:snoop ...