POJ1330 Nearest Common Ancestors (JAVA)
经典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)的更多相关文章
- POJ1330 Nearest Common Ancestors
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24587 Acce ...
- POJ1330 Nearest Common Ancestors(最近公共祖先)(tarjin)
A - Nearest Common Ancestors Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld &am ...
- [POJ1330]Nearest Common Ancestors(LCA, 离线tarjan)
题目链接:http://poj.org/problem?id=1330 题意就是求一组最近公共祖先,昨晚学了离线tarjan,今天来实现一下. 个人感觉tarjan算法是利用了dfs序和节点深度的关系 ...
- POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...
- POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14902 Accept ...
- A. Nearest Common Ancestors
A. Nearest Common Ancestors Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 10000KB 64-bi ...
- JDOJ 3055: Nearest Common Ancestors
JDOJ 3055: Nearest Common Ancestors JDOJ传送门 Description 给定N个节点的一棵树,有K次查询,每次查询a和b的最近公共祖先. 样例中的16和7的公共 ...
- POJ 1330 Nearest Common Ancestors(Targin求LCA)
传送门 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26612 Ac ...
- [最近公共祖先] POJ 1330 Nearest Common Ancestors
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27316 Accept ...
随机推荐
- labelme2COCO
# -*- coding:utf-8 -*-# !/usr/bin/env python import argparseimport jsonimport matplotlib.pyplot as p ...
- 49. Group Anagrams同义词合并
[抄题]: Given an array of strings, group anagrams together. Example: Input: ["eat", "te ...
- QScopedPointer
QScopedpointer detailed description the QScopedpointer class stores a pointer to a dynamically alloc ...
- Bigtable:一个分布式的结构化数据存储系统
Bigtable:一个分布式的结构化数据存储系统 摘要 Bigtable是一个管理结构化数据的分布式存储系统,它被设计用来处理海量数据:分布在数千台通用服务器上的PB级的数据.Google的很多项目将 ...
- DecoratorPattern(23种设计模式之一)
参考书籍:设计模式-可复用面向对象软件基础(黑皮书) 书中写到,装饰者模式的意图是动态的给对象添加一些额外的职责.就增加功能来说,Decorator模式相比生成子类更为灵活.装饰者模式的另一个别名是包 ...
- dll总结
[转]http://www.cnblogs.com/cswuyg/archive/2011/09/30/dll.html 动态链接库dll的使用有两种方式,一种是显式调用.一种是隐式调用. (1) ...
- c语言条件编译#ifdef与#if defined
c语言条件编译#ifdef与#if defined c语言条件编译#ifdef与#if defined 摘自:https://www.cnblogs.com/zhangshenghui/p/566 ...
- HBase数据读写流程(1.3.1)
===数据写入流程=== 源码:https://github.com/apache/hbase/blob/master/hbase-server/src/main/java/org/apache/ha ...
- mvc中使用Hangfire处理后台任务
考虑下如下代码,在数据保存后,需要发送邮件,发送邮件是个耗时的工作. 我们的目的是,数据保存成功后,就可以返回响应了,发送邮件不重要,不需要等待邮件发送成功 [HttpPost] public Act ...
- SpringMVC的问题No mapping found for HTTP request with URI
做了一个屏蔽进数据库的操作: Applicaition.xml配置: <?xml version="1.0" encoding="UTF-8"?> ...