经典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. 什么是响应式编程——响应式Spring的道法术器

    响应式编程之道 1.1 什么是响应式编程? 在开始讨论响应式编程(Reactive Programming)之前,先来看一个我们经常使用的一款堪称“响应式典范”的强大的生产力工具——电子表格. 举个简 ...

  2. 推荐一个好用的sql版本控制和美化工具SQLToolbelt

    SQL Toolbelt含Red Gate公司目前出品的所有SQL Server工具和新版本.也就是说,当你使用SQL Server数据库工作时,您可以很方便的获得您所需要的一套强大的工具,包括SQL ...

  3. code1091 传染病控制

    1.读入图,边是双向的 2.递归建树,同时确定每一层的节点 3.dfs按层搜索,先把这一层所有被传染的(die[pa[k]]=true的)的die置为true 然后循环,每次把一个die为true的变 ...

  4. vim全局替换命令-乾颐堂

    语法为 :[addr]s/源字符串/目的字符串/[option] 全局替换命令为::%s/源字符串/目的字符串/g [addr] 表示检索范围,省略时表示当前行. 如:“1,20” :表示从第1行到2 ...

  5. Django常见出错解决方案汇总-乾颐堂

    一.模板类型错误: 错误原因:在models中漏掉了return: class UserProfile(models.Model): """ 功能说明:        扩 ...

  6. rest-framework-----url控制

    一:自定义路由(原始方式) from django.conf.urls import url from app01 import views urlpatterns = [ url(r'^books/ ...

  7. NBA常识 位置的划分 足球:越位等于抢跑

    篮球:1号位——组织后卫(控球,组织)2号位——得分后卫(中远投篮,突破)3号位-----小前锋(突破,中远投篮)4号位——大前锋(二中锋,篮板,背身单打,禁区防守)5号位——中锋(篮板.背身单打,禁 ...

  8. Fiddldr 教程之:HTTP协议详解(转)

    原文地址:http://www.cnblogs.com/TankXiao/archive/2012/02/13/2342672.html HTTP协议详解 当今web程序的开发技术真是百家争鸣,ASP ...

  9. 设置express ejs模板的后缀名html

    如果使用jade或者ejs模板引擎的话 模板文件的格式为ejs或者jade ,有时候需要将后缀名修改为 html格式的. app.set('view engine','ejs'); app.engin ...

  10. HBASE--MapReduce

    1.查看 HBase 的 MapReduce 任务的执行 $ bin/hbase mapredcp 2.执行环境变量的导入 $ export HBASE_HOME= ~/hadoop_home/hba ...