[jzyzoj2021]lca模板题

查找最近公共祖先...我也不知道这东西有什么用,在线写法,非常之慢....

存代码

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 using namespace std;
 7 int n,m;
 8 struct nod{
 9     int y;
10     int next;
11 }e[200020];
12 int head[100010]={};
13 int dep[100010]={};
14 int size[100010]={};
15 int son[100010]={};
16 int top[100010]={};
17 int f[100010]={};
18 int tot=0;
19 void init(int x,int y){
20     e[++tot].next=head[x];
21     head[x]=tot;
22     e[tot].y=y;
23 }
24 void dfs1(int x){
25     dep[x]=dep[f[x]]+1;
26     size[x]=1;
27     for(int i=head[x];i;i=e[i].next){
28         if(e[i].y!=f[x]&&!f[e[i].y]){
29             f[e[i].y]=x;
30             dfs1(e[i].y);
31             size[x]+=size[e[i].y];
32             if(size[son[x]]<size[e[i].y]) son[x]=e[i].y;
33         }
34     }
35 }
36 void dfs2(int x){
37     if(x==son[f[x]])top[x]=top[f[x]];
38     else top[x]=x;
39     for(int i=head[x];i;i=e[i].next){
40         if(f[e[i].y]==x) dfs2(e[i].y);
41     }
42 }
43 int ask(int x,int y){
44     while(top[x]!=top[y]){
45         if(dep[top[x]]>dep[top[y]]) x=f[top[x]];
46         else y=f[top[y]];
47     }
48     if(dep[x]<dep[y]) return x;
49     else return y;
50 }
51 int main(){
52     scanf("%d%d",&n,&m);
53     int x,y;
54     for(int i=1;i<n;i++){
55         scanf("%d%d",&x,&y);
56         init(x,y);
57         init(y,x);
58     }
59     dfs1(1);
60     dfs2(1);
61     for(int i=1;i<=m;i++){
62         scanf("%d%d",&x,&y);
63         printf("%d\n",ask(x,y));
64     }
65     return 0;
66 }

时间: 2024-08-07 22:48:21

[jzyzoj2021]lca模板题的相关文章

hdu 2586 LCA模板题(离线算法)

http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B&quo

HDU2586 How far away ?(LCA模板题)

题目链接:传送门 题意: 给定一棵树,求两个点之间的距离. 分析: LCA 的模板题目 ans = dis[u]+dis[v] - 2*dis[lca(u,v)]; 在线算法:具体讲解 传送门 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 40010; struc

poj1470(LCA模板题)

题目连接:http://poj.org/problem?id=1470 被输入卡到死...... 开始也看到输入格式的问题了,但是我以为查询的两个点的格式就是(u v)这样,其它地方可以无限空格... 为这一直WA啊,以为是算法哪块写挂了,一直找..... 其实哪都可能有一堆空格(其实题目说的也很清楚了-_-||).... 教训.. 还有一点,刚开始以为给出的第一个节点就是祖先,其实题目并没有这么说,看着图理解就误以为了那样了... 1 #include<cstdio> 2 #include&

HDU 2586 LCA模板题

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2586 题目大意:在一个无向树上,求一条链权和. 解题思路: 0 | 1 /   \ 2      3 设dist[i]为i到根0的链和,求法(Dfs过程中dist[v]=dist[u]+e[i].w) 对于树中任意两点形成的链,可以通过LCA最近公共祖先剖分. 比如2->3,就可以经过LCA点1:  2->1->3 链和=dist[u]+dist[v]-2*dist[LCA[u,v]] (

HDU 2586 How far away ? 离线lca模板题

How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8712    Accepted Submission(s): 3047 Problem Description There are n houses in the village and some bidirectional roads connecting

LCA模板(数剖实现)

题目链接:https://www.luogu.org/problemnew/show/P3379 题意:LCA模板题. 思路:今天开始学树剖,先拿lca练练.树剖解lca,两次dfs复杂度均为O(n),每次查询为logn,因此总复杂度为:O(2*n+m*logn). 代码: #include<cstdio> #include<cstring> using namespace std; const int maxn=500005; struct node{ int v,next; }

POJ 1330(LCA模板)

链接:http://poj.org/problem?id=1330 题意:q次询问求两个点u,v的LCA 思路:LCA模板题,首先找一下树的根,然后dfs预处理求LCA(u,v) AC代码: 1 #include<iostream> 2 #include<algorithm> 3 #include<cmath> 4 #include<cstring> 5 #include<set> 6 #include<string> 7 #incl

几道树剖模板题

寒假后半段一直都在外出旅游..颓了好久..qaq 旅游期间写了几道树剖模板题,贴上来.. BZOJ 1036 没啥好说的,裸题 1 #include <cstdio> 2 #include <algorithm> 3 4 #define LEFT (segt[cur].l) 5 #define RIGHT (segt[cur].r) 6 #define MID (segt[cur].mid) 7 #define SUM (segt[cur].Sum) 8 #define MAX (

小机房的树——LCA 启蒙题

大概从一个月前的学考准备阶段就没好好干过什么事,五月底六月初因为要突击文科而削减了奥赛,班主任也说中午暂时别去机房.然后高考放假在家里准备炎德(间断地打了十五盘Dota 2,十五盘是凤凰,输了三盘),本来带了几本文科的资料但是没动.高考完了我们会学校学考.学考完那天下午只有一堂英语,于是我就一个人待在机房,考完回寝室听人说班主任中午说晚上要考试.然后就考试,用十三号和十四号两天的晚自习考了高考卷子(湖南的全国卷乙).成绩是语文一百,数学九十一,英语一百三十五(其实当时因为时间关系而没有做听力,我