人丑常数大,总是卡在1000多ms。。。

 #include <cstdio>
#include <cstring>
#include <iostream>
#define maxn 5000005
#define jump_max 19
struct Edge{
int from,to;
};
Edge edge[maxn];
int n,m,s,x,y,a,b;
int dep[maxn];
int f[maxn][];
int head[maxn];
int tot = ;
inline void swap_ele(int &a,int &b){
int c = a;
a = b;
b = c;
}
inline int read(){
int num = ;
char c;
bool flag = false;
while ((c = getchar()) == ' ' || c == '\n' || c == '\r');
if (c == '-')
flag = true;
else
num = c - '';
while (isdigit(c = getchar()))
num = num * + c - '';
return (flag ? - : ) * num;
}
void add_edge(int from,int to){
edge[++tot].from = head[from];
edge[tot].to = to;
head[from] = tot;
}
void dfs(int step){
for (int i=head[step];i!=;i = edge[i].from){
int v = edge[i].to;
if (dep[v] == ){
dep[v] = dep[step] + ;
f[v][] = step;
dfs(v);
}
}
}
void init(){ for (register int i=;i<=jump_max;i++)
for (register int j=;j<=n;j++)
f[j][i] = f[ f[j][i-] ][ i- ];
} int lca(int x,int y){
if (dep[x] < dep[y])
swap_ele(x,y);
for (register int i=jump_max;i>=;--i){
if (dep[f[x][i]] >= dep[y])
x = f[x][i];
}
if (x == y)
return y;
for (register int i=jump_max;i>=;--i)
if (f[x][i] != f[y][i]){
x = f[x][i];
y = f[y][i];
} return f[x][];
} int main(){
n = read();m = read();s = read();
for (register int i=;i<=n-;++i){
x = read();y = read();
add_edge(x,y);add_edge(y,x);
}
dep[s] = ;
dfs(s);
init();
for (register int i=;i<=m;++i){
a = read();b = read();
printf("%d\n",lca(a,b));
}
return ;
}

自用lca模板的更多相关文章

  1. LCA模板

    /*********--LCA模板--***************/ //设置好静态参数并构建好图的邻接表,然后调用lca_setquery()设置查询 //最后调用lca_start(),在lca ...

  2. 倍增求lca模板

    倍增求lca模板 https://www.luogu.org/problem/show?pid=3379 #include<cstdio> #include<iostream> ...

  3. HDU 2586——How far away ?——————【LCA模板题】

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. 算法复习——LCA模板(POJ1330)

    题目: Description A rooted tree is a well-known data structure in computer science and engineering. An ...

  5. hdu 2586 How far away?(LCA模板题+离线tarjan算法)

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. LCA模板(数剖实现)

    题目链接:https://www.luogu.org/problemnew/show/P3379 题意:LCA模板题. 思路:今天开始学树剖,先拿lca练练.树剖解lca,两次dfs复杂度均为O(n) ...

  7. POJ 1330 Nearest Common Ancestors(LCA模板)

    给定一棵树求任意两个节点的公共祖先 tarjan离线求LCA思想是,先把所有的查询保存起来,然后dfs一遍树的时候在判断.如果当前节点是要求的两个节点当中的一个,那么再判断另外一个是否已经访问过,如果 ...

  8. HDU2586 How far away ?(LCA模板题)

    题目链接:传送门 题意: 给定一棵树,求两个点之间的距离. 分析: LCA 的模板题目 ans = dis[u]+dis[v] - 2*dis[lca(u,v)]; 在线算法:详细解说 传送门 代码例 ...

  9. 最近公共祖先(LCA)模板

    以下转自:https://www.cnblogs.com/JVxie/p/4854719.html 首先是最近公共祖先的概念(什么是最近公共祖先?): 在一棵没有环的树上,每个节点肯定有其父亲节点和祖 ...

随机推荐

  1. hdu_1045Fire Net(二分图匹配)

    hdu_1045Fire Net(二分图匹配) 标签: 图论 二分图匹配 题目链接 Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...

  2. Wolf and Rabbit

    http://acm.hdu.edu.cn/showproblem.php?pid=1222 Wolf and Rabbit Time Limit: 2000/1000 MS (Java/Others ...

  3. Ugly Numbers(STL应用)

    题目链接:http://poj.org/problem?id=1338 Ugly Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  4. 一对多(多对一)关系中的inverse和cascade属性

    转载请标明出处 http://www.cnblogs.com/haozhengfei/p/6049276.html 首先说一下inverse: "inverse" 直译过来就是&q ...

  5. CSS3总结学习(一):CSS3用户界面

    在CSS3中,新的用户界面属性有很多,本文重点介绍resize,box-sizing,offset. 浏览器支持,如下图,图片源于W3school 1.CSS Resizing 在css3,resiz ...

  6. React源码解析:ReactElement

    ReactElement算是React源码中比较简单的部分了,直接看源码: var ReactElement = function(type, key, ref, self, source, owne ...

  7. K8S API 调用

    不好意,本人比较懒,OneNote 复制粘贴的时候就是自动变成图片了.请各位看官多多见谅. 遗留问题: 目前pod仅支持修改 * and(),so...

  8. JavaScript String(字符串对象)

    String 对字符串的支持 String.charAt( n ) 返回字符串中的第n个字符 n 是下标 String.charCodeAt( ) 返回字符串中的第n个字符的代码 String.con ...

  9. java8-新特性--(接口的默认方法与静态方法)

    Java 8用默认方法与静态方法这两个新概念来扩展接口的声明. public interface Inte{ void method(); default void defaultMethod(){ ...

  10. 存储管理工具StorageExplorer的基本使用

    本文主要介绍Azure StorageExplorer工具的安装及基本使用 1.打开Azure官方链接:https://www.azure.cn/downloads/ 2.按照向导进行安装 3.可以通 ...