经典LCA操作。。

贴AC代码

import java.lang.reflect.Array;
import java.util.*;
public class POJ1330 {
// 并查集部分
static int[] p;
static int find(int u){
if(p[u]!=u){
p[u] = find(p[u]);
}
return p[u];
}
static class Edge{
int to;
int next;
}
// 链式前向星
static int[] head;
static Edge[] edges; static boolean[] vist;
// 所求LCA的两点
static int qfrom;
static int qto;
// 找根
static boolean[] isroot; static boolean LCA(int u){
// 在以自己为根的子树中计算LCA
// 如果没找到就回溯到父节点,再向下去兄弟子树计算
// 先建立以自己为代表的并查集
p[u]=u;
vist[u]=true; for(int k=head[u];k>=0;k=edges[k].next){
int to = edges[k].to;
if(!vist[to]) {
// 计算子树
// 如果在某棵子树计算过程中算出结果,就返回true
if(LCA(to))
return true;
// 子树计算完,把子树的并查集代表节点设为当前节点
p[to] = u;
}
}
// 所有子树计算完毕,还没发现结果
// 试试用当前节点计算结果
if(u == qfrom){
if(vist[qto]){
System.out.println(find(qto));
return true;
}
}else if(u==qto){
if(vist[qfrom]){
System.out.println(find(qfrom));
return true;
}
} // 还是没算出结果,向上回溯,准备去兄弟子树计算
return false;
} public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while (n-- > 0){
int m = sc.nextInt();
p = new int[m+1];
head = new int[m+1];
Arrays.fill(head,-1);
edges = new Edge[m];
vist = new boolean[m+1];
isroot = new boolean[m+1];
Arrays.fill(isroot,true);
// 链式前向星构有向图
for(int i=0;i<m-1;i++){
int from = sc.nextInt();
int to = sc.nextInt();
Edge edge = new Edge();
edge.to=to;
edge.next=head[from];
edges[i]=edge;
head[from]=i;
// 有父节点的不是根
isroot[to]=false;
}
qfrom = sc.nextInt();
qto = sc.nextInt();
// 找根
int root=0;
for(int i=1;i<isroot.length;i++){
if(isroot[i]) {
root = i;
break;
}
}
// 计算LCA
LCA(root);
}
}
}

  

POJ1330 Nearest Common Ancestors (JAVA)的更多相关文章

  1. POJ1330 Nearest Common Ancestors

      Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24587   Acce ...

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

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

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

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

  4. POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)

    POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...

  5. POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)

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

  6. A. Nearest Common Ancestors

    A. Nearest Common Ancestors Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 10000KB   64-bi ...

  7. JDOJ 3055: Nearest Common Ancestors

    JDOJ 3055: Nearest Common Ancestors JDOJ传送门 Description 给定N个节点的一棵树,有K次查询,每次查询a和b的最近公共祖先. 样例中的16和7的公共 ...

  8. POJ 1330 Nearest Common Ancestors(Targin求LCA)

    传送门 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26612   Ac ...

  9. [最近公共祖先] POJ 1330 Nearest Common Ancestors

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

随机推荐

  1. 安装了Anaconda之后,Maya运行报错,Python 找不到 Maya 的 Python 模块

    以前Maya用的好好地,结果安装了Anaconda之后,maya启动以后,日志就会报错(如下),只能自主建模,不能打开以前创建的模型,也不能导入fbx,错误提示就是Maya找不到Python模块,在网 ...

  2. SQL Server横向扩展:设计,实现与维护(2)- 分布式分区视图

    为了使得朋友们对分布式分区视图有个概念,也为了方便后面的内容展开,我们先看看下面一个图:     讲述分布式分区视图之前,很有必要将之与我们常常熟悉的分区表和索引进行区别. 首先,分布式分区视图是一个 ...

  3. CentOS 7 更换 阿里云/清华大学 yum 软件源

    阿里云参考:https://opsx.alibaba.com/mirror?lang=zh-CN 清华参考:https://mirrors.tuna.tsinghua.edu.cn/help/cent ...

  4. SourceTree 3.0.8 跳过登陆注册

    3.0.8普通用户版account.json跳过登陆注册方法已失效,请安装企业版 https://www.sourcetreeapp.com/enterprise 企业版默认安装在 %programf ...

  5. 做个简单的Redis监控(源码分享)[转载]

    Redis监控 Redis 是目前应用广泛的NoSQL,我做的项目中大部分都是与Redis打交道,发现身边的朋友也更多人在用,相对于memcached 来说,它的优势也确实是可圈可点.在随着业务,数据 ...

  6. 15 输入三个整数x,y,z,请把这三个数由小到大输出。

    题目:输入三个整数x,y,z,请把这三个数由小到大输出. public class _015ThreeNumberSort { public static void main(String[] arg ...

  7. HDU 3366 Passage (概率DP)

    题意:T组测试数据,一个人困在了城堡中,有n个通道,m百万money ,每个通道能直接逃出去的概率为 P[i] ,遇到士兵的概率为 q[i], 遇到士兵得给1百万money,否则会被杀掉,还有 1-p ...

  8. 编写高质量代码改善C#程序的157个建议——建议152:最少,甚至是不要注释

    建议152:最少,甚至是不要注释 以往,我们在代码中不写上几行注释,就会被认为是钟不负责任的态度.现在,这种观点正在改变.试想,如果我们所有的命名全部采用有意义的单词或词组,注释还有多少存在的价值. ...

  9. Python之模块一

    1 >模块介绍: 模块,用一坨代码实现了某个功能的代码集合,类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的          重用性和代码间的吻合, ...

  10. Middleware / 中间件

    中间件格式 module.exports = options => { return (ctx, next) => { // do something } } 中间件格式为一个高阶函数,外 ...