http://poj.org/problem?

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)的更多相关文章

  1. POJ1330Nearest Common Ancestors最近公共祖先LCA问题

    用的离线算法Tarjan 该算法的详细解释请戳 http://www.cnblogs.com/Findxiaoxun/p/3428516.html 做这个题的时候,直接把1470的代码copy过来,改 ...

  2. 图论-最近公共祖先-离线Tarjan算法

    有关概念: 最近公共祖先(LCA,Lowest Common Ancestors):对于有根树T的两个结点u.v,最近公共祖先表示u和v的深度最大的共同祖先. Tarjan是求LCA的离线算法(先存储 ...

  3. 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18136   Accept ...

  4. POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13372   Accept ...

  5. POJ1330 Nearest Common Ancestors(最近公共祖先)(tarjin)

    A - Nearest Common Ancestors Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld &am ...

  6. POJ 1330 LCA最近公共祖先 离线tarjan算法

    题意要求一棵树上,两个点的最近公共祖先 即LCA 现学了一下LCA-Tarjan算法,还挺好理解的,这是个离线的算法,先把询问存贮起来,在一遍dfs过程中,找到了对应的询问点,即可输出 原理用了并查集 ...

  7. POJ - 1330 Nearest Common Ancestors 最近公共祖先+链式前向星 模板题

    A rooted tree is a well-known data structure in computer science and engineering. An example is show ...

  8. POJ1470Closest Common Ancestors 最近公共祖先LCA 的 离线算法 Tarjan

    该算法的详细解释请戳: http://www.cnblogs.com/Findxiaoxun/p/3428516.html #include<cstdio> #include<alg ...

  9. [HDOJ2586]How far away?(最近公共祖先, 离线tarjan, 并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 这题以前做过…现在用tarjan搞一发…竟然比以前暴力过的慢………… 由于是离线算法,需要Que ...

随机推荐

  1. onethink 换空间报错 解决方案

    onethink 换空间的时候有两个配置文件 Application\Common\Conf Application\User\Conf 如果报错先测试数据库 <?php $con = mysq ...

  2. windows 基础及基本软件测试环境搭建

  3. Shell之test

    test命令用法.功能:检查文件和比较值 1)判断表达式 if test (表达式为真) if test !表达式为假 test 表达式1 –a 表达式2                  两个表达式 ...

  4. jdk各个版本

    http://www.cnblogs.com/langtianya/p/3757993.html

  5. 最近Get到的一些HTML/CSS中的小点(一)

    1.<em>和<strong>标签都是用来强调一段话中的某几个文字.<em>默认斜体,<strong>默认粗体.在强调语气上<strong> ...

  6. GHOST还原

    整理日: 2015年2月16日 GHOST GHO2Disk STEP01 STEP02 STEP03 STEP04 STEP05 STEP06 STEP07

  7. 日期相关---SimpleDateFormat的setLenient(true/false)-----自动计算日期

    有时候我们需要判断用户的日期格式是否正确, 虽然绝大多数会在前台处理,但是也有需要从文件流读入的情况,如果日期不合格就需要抛异常,这时候就需要禁止SimpleDateFormat的自动计算功能. 这时 ...

  8. oralce闪回

    Oracle闪回操作 1. 记录当前时间或SCN 在数据库变动前记录时间或SCN SQL> select  to_char(sysdate,'YYYY-MM-DD HH24:mi:ss') fr ...

  9. 【Tools】maven安装

    安装Maven插件老是报以下的错误,好像少了一个叫guava库的东西,但是在其他机器安装不报这个错误. Cannot complete the install because one or more  ...

  10. python 大文件以行为单位读取方式比对

    http://www.cnblogs.com/aicro/p/3371986.html 先前需要做一个使用python读取大文件(大于1G),并逐条存入内存进行处理的工作.做了很多的尝试,最终看到了如 ...