<题目链接>

题目大意:

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

解题分析:
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. SQL Server 函数之日期格式化函数

    SQL Server 函数之日期格式化函数 高文龙关注0人评论612人阅读2017-09-23 13:47:07 SQL Server 函数之日期格式化函数 对于一些经常写SQL Server执行语句 ...

  2. Confluence 6 附件存储文件系统的分级

    从 Confluence 3.0 开始,附件的存储方式有了重大的改变和升级.如果你是从 Confluence 2.10 及其早期版本升级上来的,请参考 Upgrading Confluence 页面中 ...

  3. C#关于线程的问题

    1.通过System.threading.Thread类可以创建新的线程,并在线程堆栈中运行静态和动态的实例,可以通过Thread类的构造方法传递一个无参数,并且不返回的委托, class Progr ...

  4. git 注意事项

    1,用户凭证 github的两种url地址 http      ssh :由于Git和Github交互操作可能会很频繁,那么一定少了用户授权的操作,为了防止每次操作重复输入用户名和密码,Git提供了两 ...

  5. str类型

    str:字符串类型,用单引号或双引号. #索引 s1 = s[0] 切片 : 顾头不顾尾 : s[首:尾:步长] 字符串的操作: 1.首字母大写: s = 'alexWUsir' s1 = s.cap ...

  6. 异常小结:上一张图搞清楚Java的异常机制

    下面是Java异常类的组织结构,红色区域的异常类表示是程序需要显示捕捉或者抛出的. Throwable Throwable是Java异常的顶级类,所有的异常都继承于这个类. Error,Excepti ...

  7. C++ Primer 笔记——语句

    switch 内部的变量定义 1.因为C++语言规定,不允许跨过变量的初始化语句直接跳转到该变量作用域内的另一位置,所以有了如下情况: bool bsuccess = false; switch (b ...

  8. pycharm导入本地py文件时,模块下方出现红色波浪线时如何解决

    有时候导入本地模块或者py文件时,下方会出现红色的波浪线,但不影响程序的正常运行,但是在查看源函数文件时,会出现问题 问题如下:  解决方案: 1. 进入设置,找到Console下的Python Co ...

  9. OpenCV-Python教程8-图像混合

    一.图片相加 要叠加两张图片,使用cv2.add(),相加两幅图片的形状(高度.宽度.通道数)必须相同.numpy中可以直接用res = img1 + img2相加.但是两者的结果并不相同 impor ...

  10. web应用启动后发现被自动访问

    为了找到原因,做了以下操作,发现是eclipse访问的,但是具体原因未知