<题目链接>

题目大意:

给出一棵树,问任意两个点的最近公共祖先的编号。

解题分析:
LCA模板题,下面用的是树上倍增求解。

 #include <iostream>
 #include <cstdio>
 #include <cstring>
 #include <algorithm>
 #include <cmath>
 using namespace std;

 ;
 const int INF = 0x3f3f3f3f;
 struct Edge{
     int to, next;
 }edge[N<<];
 int n,cnt, head[N], in[N];
 ];
 void add_edge(int v, int u){
     edge[cnt].to = u, edge[cnt].next = head[v], head[v] = cnt++;
 }
 void dfs(int u, int fa){   //得到所有节点的深度
     ; i = edge[i].next){
         int v = edge[i].to;
         if(v == fa) continue;
         if(!dep[v]){
             dep[v] = dep[u] + ;
             f[v][] = u;
             dfs(v, u);
         }
     }
 }
 void init(){   //树上倍增预处理
     ; (<<j) <= n; j++)
         ; i <= n; i++)
             f[i][j] = f[f[i][j-]][j-];
 }
 int LCA(int v, int u){
     if(dep[v] < dep[u])swap(v, u);   //v为深度更深的节点
     int d = dep[v] - dep[u];
     ; (d>>i) != ; i++)
         ) v = f[v][i]; //以上的操作是处理较深的节点,使两节点深度一致
     if(v == u) return v;   //如果深度一致时,两节点相同,那么直接返回即可
     ;i>= ;i--)
         if(f[v][i] != f[u][i])   //跳2^i步不一样,就跳,否则不跳
             v = f[v][i],u = f[u][i];   //两点一起向上跳2^i步
     ];  //经证明,上述操作做完,两点的LCA都在上一层,所以再走一步即可
 }
 int main(){
     int t, a, b;
     scanf("%d", &t);
     while(t--){
         scanf("%d", &n);
         cnt = ;
         memset(head, -, sizeof head);
         memset(, sizeof in);
         ; i < n; i++){
             scanf("%d%d", &a, &b);
             add_edge(a, b);
             in[b]++;
         }
         memset(dep, , sizeof dep);
         int root;
         ; i <= n; i++)
             if(!in[i]) root = i;
         dep[root] = ;
         dfs(root, -);
         init();   //注意这里init要放在dfs后面,因为f[i][0]需要通过dfs预处理得到
         scanf("%d%d", &a, &b);
         printf("%d\n", LCA(a, b));
     }
     ;
 }

2018-10-18

POJ 1330 Nearest Common Ancestors (模板题)【LCA】的更多相关文章

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

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

  2. poj 1330 Nearest Common Ancestors 单次LCA/DFS

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

  3. POJ 1330 Nearest Common Ancestors(裸LCA)

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

  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

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...

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

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

  7. POJ 1330 Nearest Common Ancestors(lca)

    POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...

  8. POJ 1330 Nearest Common Ancestors 倍增算法的LCA

    POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节 ...

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

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

随机推荐

  1. Modbus库开发笔记之五:Modbus RTU Slave开发

    Modbus在串行链路上分为Slave和Master,这一节我们就来开发Slave.对于Modbus RTU从站来说,需要实现的功能其实与Modbus TCP的服务器端是一样的.其操作过程也是一样的. ...

  2. Confluence 6 为登录失败配置使用验证码

    如果你具有 Confluence 管理员的权限,你可以限制 Confluence 登录失败的最大尝试次数.在给予最大登录失败尝试(默认为 3 次)次数后,Confluence 将会在用户进行再次尝试的 ...

  3. Confluence 6 数据库表-杂项(Miscellaneous)

    这些部分是一些其他的表格,这些表格有必要在这里提及下能帮你更好的了解系统. os_propertyentry 有关实体和属性相关的特性. bandana 所有的持久层.这个表格包含的的内容有用户设置和 ...

  4. windows与mac下安装nginx

    window下 下载链接,自己选一个版本下载 nginx官网下载 本人放在D盘: 启动nginx 有很多种方法启动nginx (1)直接双击nginx.exe,双击后一个黑色的弹窗一闪而过 (2)打开 ...

  5. laravel 关联查询

  6. MySQL慢查询 - 开启慢查询

    一.简介 开启慢查询日志,可以让MySQL记录下查询超过指定时间的语句,通过定位分析性能的瓶颈,才能更好的优化数据库系统的性能. 二.参数说明 slow_query_log 慢查询开启状态 slow_ ...

  7. Oracle logminer 日志挖掘

    Table of Contents 1. LOGMNR简介 2. 创建数据字典 2.1. 外部文件存储数据字典 2.2. redo log 存储数据字典 3. 添加需要分析的文件 4. 开始分析文件 ...

  8. ZOJ 4057 XOR Clique(位运算)

    XOR Clique BaoBao has a sequence a​1​,a​2,...,a​n. He would like to find a subset S of {1,2,...,n} s ...

  9. 【第一部分】10Leetcode刷题

    一.删除链表的倒数第N个节点 题目:19. Remove Nth Node From End of List 分析:典型的利用双指针法解题.首先让指针first指向头节点,然后让其向后移动n步,接着让 ...

  10. shell 写的 jrottenberg/ffmpeg 转码

    #!/bin/bash ];then echo "The argument must be 2" exit; else echo "$1 $2" fi VIDE ...