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. 转 C#开发微信门户及应用(2)--微信消息的处理和应答

    微信应用如火如荼,很多公司都希望搭上信息快车,这个是一个商机,也是一个技术的方向,因此,有空研究下.学习下微信的相关开发,也就成为计划的安排事情之一了.本系列文章希望从一个循序渐进的角度上,全面介绍微 ...

  2. StrongReference

    原创作品:未经本人允许,不得转载前段时间写项目时遇到了一个问题,就是从网络获取图片资源的问题,总是出现OOM异常,经过几天的努力,终于处理的还算是可以使用,OOM的处理一直都是很头疼的问题.对于三级缓 ...

  3. jquery roundabout幻灯片插件3d图片切换支持图片旋转切换滚动

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. TCP/IP笔记 应用层(3)——HTTP

    1. URL URL(Uniform Resource Locator) 相当于一个文件名在网络范围的扩展. 1.1 格式 schema://host[:port#]/path/.../[?query ...

  5. WINDOWS 7下安装CVXOPT

    闹腾了好几天,终于将CVXOPT安装成功,这里和大家分享安装过程: 从www.python.org下载并安装Python.接下来,使用Python 2.7.5(32bit)版本(注意:64位win 7 ...

  6. C++编译器合成Default Constructor的4种情况

    笔记C++编译器为编译器需要合成Default Constructor的4种情况. 1,Class A内含Class B对象,Class A没有Default Constructor时会在编译时合成D ...

  7. sublime3快捷键汇总

    !+tab生成html结构文档选择类 Ctrl+D 选中光标所占的文本,继续操作则会选中下一个相同的文本.Alt+F3 选中文本按下快捷键,即可一次性选择全部的相同文本进行同时编辑.举个栗子: 快速选 ...

  8. Coursera《machine learning》--(2)单变量线性回归(Linear Regression with One Variable)

    本笔记为Coursera在线课程<Machine Learning>中的单变量线性回归章节的笔记. 2.1 模型表示 参考视频: 2 - 1 - Model Representation ...

  9. open files

    /* * * Copyright (c) International Business Machines Corp., 2001 * * This program is free software; ...

  10. ORACLE 字符串操作

    1 字符串连接   SQL> select 'abc' || 'def' from dual; 'ABC'|------abcdef 2 小写SQL>select lower('ABC01 ...