pku 1330 Nearest Common Ancestors

题目链接:

http://poj.org/problem?id=1330

题目大意:

给定一棵树的边关系,注意是有向边,因为这个WA一发。然后N个顶点给出了N-1有向边,求一对点之间的最近公共祖先

思路:

裸的离线tarjan Lca即可,但注意是有向边,需要先找出根节点,数组标记。其次要注意前向星存的时候只存一条边即可

代码:

#include <iostream>
#include <string.h>
using namespace std;
const int maxn = 10005;
struct node {
int to,next;
} edges[maxn*2];
int n,head[maxn],f[maxn],vis[maxn],cnt,u,v,res,fir[maxn];
inline void addedge(int u, int v) {
edges[cnt].to=v;
edges[cnt].next=head[u];
head[u]=cnt++;
}
inline void init() {
memset(vis,0,sizeof(vis));
memset(head,-1,sizeof(head));
memset(fir,1,sizeof(fir));
for(int i=1; i<=n; ++i) f[i]=i;
cnt=0;
}
inline int Find(int x) {
return x == f[x] ? x : f[x] = Find(f[x]);
}
inline void tarjan(int s) {
vis[s]=1;
int t;
for(int i=head[s]; i!=-1; i=edges[i].next) {
t=edges[i].to;
if(!vis[t]) {
tarjan(t);
f[t]=s;
}
}
if(s==u&&vis[v]) res=Find(v);
else if(s==v&&vis[u]) res=Find(u);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t,root;
cin>>t;
while(t--) {
cin>>n;
init();
for(int i=1; i<n; ++i) {
cin>>u>>v;
fir[v]=0;
addedge(u,v);
}
cin>>u>>v;
for(int i=1; i<=n; ++i) {
if(fir[i]) {
root=i;
break;
}
}
tarjan(root);
cout<<res<<endl;
}
return 0;
}

pku 1330 Nearest Common Ancestors LCA离线的更多相关文章

  1. POJ.1330 Nearest Common Ancestors (LCA 倍增)

    POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...

  2. POJ 1330 Nearest Common Ancestors LCA题解

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

  3. poj 1330 Nearest Common Ancestors lca 在线rmq

    Nearest Common Ancestors Description A rooted tree is a well-known data structure in computer scienc ...

  4. POJ 1330 Nearest Common Ancestors(Tarjan离线LCA)

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

  5. poj 1330 Nearest Common Ancestors LCA

    题目链接:http://poj.org/problem?id=1330 A rooted tree is a well-known data structure in computer science ...

  6. [POJ1330]Nearest Common Ancestors(LCA, 离线tarjan)

    题目链接:http://poj.org/problem?id=1330 题意就是求一组最近公共祖先,昨晚学了离线tarjan,今天来实现一下. 个人感觉tarjan算法是利用了dfs序和节点深度的关系 ...

  7. POJ 1330 Nearest Common Ancestors(LCA模板)

    给定一棵树求任意两个节点的公共祖先 tarjan离线求LCA思想是,先把所有的查询保存起来,然后dfs一遍树的时候在判断.如果当前节点是要求的两个节点当中的一个,那么再判断另外一个是否已经访问过,如果 ...

  8. POJ 1330 Nearest Common Ancestors (LCA,倍增算法,在线算法)

    /* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...

  9. POJ - 1330 Nearest Common Ancestors(基础LCA)

    POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %l ...

随机推荐

  1. ssm框架的整合

    首先创建一个web工程,我这里使用的IDE为eclipse. 结果目录如下: 添加相关的jar包: 接下来是完成配置文件 首先我们先配置web.xml: <?xml version=" ...

  2. .10-Vue源码之Watcher(1)

    上一节最后再次调用了mount函数,我发现竟然跳到了7000多行的那个函数,之前我还说因为声明早了被覆盖,看来我错了! 就是这个函数: // Line-7531 Vue$3.prototype.$mo ...

  3. 基于netfilter和LVM的密码窃取

    一:要求: 编写一个基于netfilter的模块,该模块的功能是捕获如mail.ustc.edu.cn等使用明文传输用户名和密码的网站的用户名和密码:并在接收到特定的ICMP数据包之后将捕获的用户名和 ...

  4. HDU1019 Least Common Multiple(多个数的最小公倍数)

    The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...

  5. 【转载】SQL注入

             "SQL注入"是一种利用未过滤/未审核用户输入的攻击方法("缓存溢出"和这个不同),意思就是让应用运行本不应该运行的SQL代码.如果应用毫无防 ...

  6. 英语学习笔记---01---新概念第一册---Lesson 1 Excuse me!

    Lesson 1   Excuse me!   [词汇] excuse [iks5kju:z]                      v. 原谅      劳驾.借光 me             ...

  7. Java一点输入输出技巧

    输入: 格式1:Scanner sc = new Scanner(System.in); 格式2:Scanner sc = new Scanner(new BufferedInputStream(Sy ...

  8. HTML DOM应用案例2

    <html> <head> <title>day03</title> <script type="text/javascript&quo ...

  9. Vue js 的生命周期详解

    Vue 实例的生命周期 Vue 实例有一个完整的生命周期,也就是从开始创建.初始化数据.编译模板.挂载Dom→渲染.更新→渲染.卸载等一系列 过程,我们称这是 Vue 的生命周期.通俗说就是 Vue ...

  10. vs2015添加T4模版

    <#@ template language="C#" debug="false" hostspecific="true"#> & ...