<题目链接>

题目大意:

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

解题分析:
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 其他 MBeans 和高 CPU 消耗线程

    其他 MBeans 希望监控 Hibernate 和 Hazelcast(仅针对 Confluence 数据中心)你需要在你的 setenv.sh / setenv.bat 文件中添加下面的内容. s ...

  2. js原生回到顶部,并平滑过渡---- 记录

    window.scrollTo({ top: 0, behavior: 'smooth', });

  3. nginx实践(五)之代理服务(正向代理与反向代理介绍)

    正向代理 正向代理代理是为客户端服务,代理负责DNS解析域名到对应ip,并进行访问服务端,返回响应给客户端 反向代理 客户端自己负责请求DNS解析域名到对应ip,服务端通过代理分发流量,进行负载均衡 ...

  4. laravel 中with关联查询限定查询字段

    学习了下laravel5.6框架,果然很优雅,比如ActiveJieSuan model中作如下关联:(laravel模型关联关系可以查看https://laravelacademy.org/post ...

  5. GetComputerNameEx()

    昨晚看了MSDN提供的GetComputerNameEx function(参考:https://msdn.microsoft.com/en-us/library/windows/desktop/ms ...

  6. 微信小程序 如何获取用户code

    1.首先需要获取code 使用 wx.login({ success: function(res) { console.log(res);//这里的返回值里面便包含code }, fail: func ...

  7. less 写关键帧动画

    @keyframes 关键帧动画写在less里的时候,务必要写在所有的{}之外,不能被{}包裹 甚至务必写在代码的最后 不然报错 Compilation resulted in incorrect C ...

  8. OpenCV trackbar 避免使用全局变量

    OpenCV trackbar 避免使用全局变量 在OpenCV中使用trackbar是交互式地图像处理的一种方式,例如用于图像阈值分割,用户可以一边调整阈值一边看效果.但是很不幸,绝大多数教程上使用 ...

  9. 腾讯应用宝Android 应用加固(乐固)操作说明(转)

    此处引用腾讯云对加固的优点说明如下: 为什么应用需要加固? 若应用不做任何安全防护,极易被病毒植入.广告替换.支付渠道篡改.钓鱼.信息劫持等,严重侵害开发者的利益. 应用进行安全防护,防止应用分发后, ...

  10. Hadoop java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils

    .jar 学习好友推荐案例的时候,提交运行时报错找不到StringUtils java.lang.ClassNotFoundException: org.apache.commons.lang3.St ...