<题目链接>

题目大意:

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

解题分析:
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. Confluence 6 用户宏示例 - Formatted Panel

    下面的用演示了如果还写一个用户宏,并在这个宏中创建一个格式化的面板,并且指定颜色.将会创建下面的面板: (Title)   注意:这个面板的标题为空,如果你没有给这个面板标题参数的话. Macro n ...

  2. 10,关于在vs2017中的netcore项目使用BundlerMinifier 配置问题

    查阅资料 https://github.com/madskristensen/BundlerMinifier/issues/230 今天下载了vs2017 rc 创建了个netcore的web项目,突 ...

  3. JPA核心类与使用

    点击访问:JPA环境配置(一) Persistence: Persistence用于获取EntityManagerFactory实例,这个类中包含一个名为createEntityManagerFact ...

  4. 基于DBUtils实现数据库连接池

    小知识: 1.子类继承父类的三种方式 class Dog(Animal): #子类 派生类 def __init__(self,name,breed, life_value,aggr): # Anim ...

  5. 【ES】学习9-聚合2

    按时间统计:date_histogram GET /cars/transactions/_search { , "aggs": { "sales": { &qu ...

  6. light1236 素数打表,质因数分解

    不知道为什么会错 /* 求出 lcm(i,j)==n 的对数, 分解质因数,p1^e1 * p2^e2 * p3^e3 那么 i,j中必定有一个数有e1个p1因子,另一个任意即可 那么最终的结果就是 ...

  7. java设置字符串编码、转码

    Unicode(统一码.万国码.单一码)是计算机科学领域里的一项业界标准,包括字符集.编码方案等.Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一 ...

  8. python爬虫点触验证码的识别思路(图片版)

  9. Python(字符串操作实例1)一个字符串用空格隔开

    # 将字符中单词用空格隔开# 已知传入的字符串中只有字母,每个单词的首字母大写,# 请将每个单词用空格隔开,只保留第一个单词的首字母大写传入:“HelloMyWorld”# 返回“Hello my w ...

  10. Just oj 2018 C语言程序设计竞赛(高级组)H: CBT?

    H: CBT? 时间限制: 1 s      内存限制: 128 MB      提交 我的状态 题目描述 对于二叉树,如果这棵树的节点排布是按行从上到下,每行从左到右挨个放置,中间不会有空闲的节点. ...