去博客园看该题解

题意

第一行输入T,有T组数据。
对于每组数据,给出一棵树,先输入n,然后n-1行,每行两个数a,b,表示a是b的父亲;第n行输入两个数A,B表示询问A和B的最近公共祖先。

题解

LCA模板题。

参见LCA学习笔记

LCA倍增算法&POJ1330标程

#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
const int N=+;
vector <int> son[N];
int T,n,depth[N],fa[N][],in[N],a,b;
void dfs(int prev,int rt){
depth[rt]=depth[prev]+;
fa[rt][]=prev;
for (int i=;(<<i)<=depth[rt];i++)
fa[rt][i]=fa[fa[rt][i-]][i-];
for (int i=;i<son[rt].size();i++)
dfs(rt,son[rt][i]);
}
int LCA(int a,int b){
if (depth[a]>depth[b])
swap(a,b);
for (int i=depth[b]-depth[a],j=;i>;i>>=,j++)
if (i&)
b=fa[b][j];
if (a==b)
return a;
int k;
for (k=;(<<k)<=depth[a];k++);
for (;k>=;k--)
if ((<<k)<=depth[a]&&fa[a][k]!=fa[b][k])
a=fa[a][k],b=fa[b][k];
return fa[a][];
}
int main(){
scanf("%d",&T);
while (T--){
scanf("%d",&n);
for (int i=;i<=n;i++)
son[i].clear();
memset(in,,sizeof in);
for (int i=;i<n;i++){
scanf("%d%d",&a,&b);
son[a].push_back(b);
in[b]++;
}
depth[]=-;
int rt=;
for (int i=;i<=n&&rt==;i++)
if (in[i]==)
rt=i;
dfs(,rt);
scanf("%d%d",&a,&b);
printf("%d\n",LCA(a,b));
}
return ;
}

POJ1330Nearest Common Ancestors的更多相关文章

  1. poj1330Nearest Common Ancestors 1470 Closest Common Ancestors(LCA算法)

    LCA思想:http://www.cnblogs.com/hujunzheng/p/3945885.html 在求解最近公共祖先为问题上,用到的是Tarjan的思想,从根结点开始形成一棵深搜树,非常好 ...

  2. poj----1330Nearest Common Ancestors(简单LCA)

    题目连接  http://poj.org/problem?id=1330 就是构建一棵树,然后问你两个节点之间最近的公共父节点是谁? 代码: /*Source Code Problem: 1330 U ...

  3. POJ1330Nearest Common Ancestors——近期公共祖先(离线Tarjan)

    http://poj.org/problem? id=1330 给一个有根树,一个查询节点(u,v)的近期公共祖先 836K 16MS #include<iostream> #includ ...

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

    LCA离线算法 它需要一次输入所有的询问,然后有根节点开始进行深度优先遍历(DFS),在深度优先遍历的过程中,进行并查集的操作,同时查询询问,返回结果. 题意: 求A ,B两点的最近公共祖先 分析: ...

  5. poj1330Nearest Common Ancestors(LCA小结)

    题目请戳这里 题目大意:意如其名. 题目分析:本题只有一个查询,所以可以各种乱搞过去. 不过对于菜鸟而言,还是老老实实练习一下LCA算法. LCA有很多经典的算法.按工作方式分在线和离线2种. tar ...

  6. POJ1330Nearest Common Ancestors最近公共祖先LCA问题

    用的离线算法Tarjan 该算法的详细解释请戳 http://www.cnblogs.com/Findxiaoxun/p/3428516.html 做这个题的时候,直接把1470的代码copy过来,改 ...

  7. 【LCA倍增】POJ1330-Nearest Common Ancestors

    [知识点:离线算法&在线算法] 一个离线算法,在开始时就需要知道问题的所有输入数据,而且在解决一个问题后就要立即输出结果. 一个在线算法是指它可以以序列化的方式一个个的处理输入,也就是说在开始 ...

  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. layui 左侧三级菜单栏

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  2. 任意N位二进制的补码实现——队列存放

    正在学习计算机组织与结构,为了写一些底层的算术操作模拟,比如一个二进制补码数的加减乘除,发现这很麻烦,因为不管是什么语言,都只提供了8位.32.64位等部分位数的补码形式,那么怎么实现任意任意位的补码 ...

  3. vue.js的计算机属性学习

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  4. 性能测试四十一:sql案例之慢sql配置、执行计划和索引

    MYSQL 慢查询使用方法MYSQL慢查询介绍分析MySQL语句查询性能的问题时候,可以在MySQL记录中查询超过指定时间的语句,我们将超过指定时间的SQL语句查询称为“慢查询”.MYSQL自带的慢查 ...

  5. idea 设置选中代码得背景颜色

  6. Loadrunner11.0 录制手机App脚本的方法一

    使用Loadrunner录制手机终端App脚本 1. 说明 目前手机APP上的功能日益丰富,对手机应用功能的性能测试需求也越来越多.公司比较抠门没有花钱买Loadrunner,可怜我们工作中一直用的破 ...

  7. python接收邮件

    # -*- coding: utf-8 -*- import poplib import email from email.parser import Parser from email.header ...

  8. python 内置数据类型之数字

    目录: 1.2. 数字 1.2.1. 数字类型 1.2.2. 浮点数 1.2.3. 进制记数 1.2.4. 设置小数精度 1.2.5. 分数 1.2.6. 除法 1.2 数字   1.2.1 数字类型 ...

  9. Centos7.4上Apache(http)编译安装

    前提:1.这个centos操作系统能上网 2.yum 安装apr,apr-util,zlib-devel,groupinstall  Development  Tools,gcc 1.在apache的 ...

  10. 用 DocumentFormat.OpenXml 和Microsoft.Office.Interop.Word 写入或者读取word文件

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...