【模板】LCA
CODE1: DFS序+RMQ
Time cost:1106ms
1 // luogu-judger-enable-o2 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 #include<queue> 7 #define ms(a,b) memset(a,b,sizeof a) 8 #define rep(i,a,n) for(int i = a;i <= n;i++) 9 #define per(i,n,a) for(int i = n;i >= a;i--) 10 #define inf 2147483647 11 using namespace std; 12 typedef long long ll; 13 ll read() { 14 ll as = 0,fu = 1; 15 char c = getchar(); 16 while(c < '0' || c > '9') { 17 if(c == '-') fu = -1; 18 c = getchar(); 19 } 20 while(c >= '0' && c <= '9') { 21 as = as * 10 + c - '0'; 22 c = getchar(); 23 } 24 return as * fu; 25 } 26 const int N = 1000005; 27 //head 28 int n,m,s,ans; 29 int head[N],nxt[N],mo[N],cnt; 30 void add(int x,int y) { 31 mo[++cnt] = y; 32 nxt[cnt] = head[x]; 33 head[x] = cnt; 34 } 35 36 int dfn[N],idx; 37 int a[N<<1],top; 38 int dep[N]; 39 void dfs(int x,int f,int d) { 40 dep[x] = d; 41 a[++top] = x; 42 dfn[x] = top; 43 for(int i = head[x];i;i = nxt[i]) { 44 int sn = mo[i]; 45 if(sn == f) continue; 46 dfs(sn,x,d+1); 47 a[++top] = x; 48 } 49 } 50 51 52 int st[25][N],pos[25][N]; 53 void initst() { 54 rep(i,1,top) st[0][i] = dep[a[i]],pos[0][i] = a[i]; 55 int r = log2(top); 56 rep(i,1,r+1) { 57 rep(j,1,top+1-(1<<i)) { 58 if(st[i-1][j] <= st[i-1][j+(1<<i-1)]) { 59 pos[i][j] = pos[i-1][j]; 60 st[i][j] = st[i-1][j]; 61 } else { 62 pos[i][j] = pos[i-1][j+(1<<i-1)]; 63 st[i][j] = st[i-1][j+(1<<i-1)]; 64 } 65 } 66 } 67 } 68 69 int query(int l,int r) { 70 if(r < l) swap(l,r); 71 int len = log2(r-l+1); 72 if(st[len][l] < st[len][r+1-(1<<len)]) ans = pos[len][l]; 73 else ans = pos[len][r+1-(1<<len)]; 74 return ans; 75 } 76 77 int main() { 78 n = read(); 79 m = read(); 80 s = read(); 81 rep(i,2,n) { 82 int x = read(); 83 int y = read(); 84 add(y,x),add(x,y); 85 } 86 dfs(s,0,0); 87 initst(); 88 while(m--) { 89 int x = read(); 90 int y = read(); 91 printf("%d\n",query(dfn[x],dfn[y])); 92 } 93 return 0; 94 }
CODE2: 树上倍增
Time cost:1157ms
1 // luogu-judger-enable-o2 2 #include<cstdio> 3 #include<iostream> 4 #include<cmath> 5 #include<algorithm> 6 #define rep(i,a,n) for(int i = a;i <= n;i++) 7 #define per(i,n,a) for(int i = n;i >= a;i--) 8 using namespace std; 9 typedef long long LL; 10 int read() { 11 int ans = 0; 12 char ch = getchar(),last=' '; 13 while(ch < '0' || ch > '9') { 14 last = ch; 15 ch = getchar(); 16 } 17 while(ch >= '0' && ch <= '9') { 18 ans = ans * 10 + ch - '0'; 19 ch = getchar(); 20 } 21 if(last == '-') 22 ans = -ans; 23 return ans; 24 } 25 const int N = 500005; 26 //head 27 28 int n,m,k; 29 30 struct edge { 31 int nxt;//ÓëiÏàÁ¬µÄÉÏÒ»Ìõ±ß 32 int mo;//Ä¿µÄµØ 33 } p[N<<1]; 34 35 int head[N],dep[N],fa[21][N],cnt; 36 int lg[N]; 37 38 void add(int x,int y) { 39 p[++cnt].mo = y; 40 p[cnt].nxt = head[x]; 41 head[x] = cnt; 42 } 43 44 void dfs(int u,int pa) { 45 dep[u] = dep[pa] + 1; 46 fa[0][u] = pa; 47 for(int i = 1; 1<<i <= dep[u]; i++) 48 fa[i][u] = fa[i-1][fa[i-1][u]]; 49 for(int i = head[u]; i; i = p[i].nxt) { 50 int sn = p[i].mo; 51 if(sn == pa) continue; 52 dfs(sn,u); 53 } 54 return; 55 } 56 57 int LCA(int x,int y) { 58 if(dep[x] < dep[y]) swap(x,y); //x±ÈyÉî 59 while(dep[x] > dep[y]) //±£Ö¤x,yͬһ²ã 60 x = fa[ lg[dep[x]-dep[y]] - 1][x]; 61 if(x == y) return y; 62 per(i,lg[dep[x]],0) //Ìøµ½LCAµÄÁ½¸ö¶ù×Ó´¦ 63 if(fa[i][x] != fa[i][y]) { 64 x = fa[i][x]; 65 y = fa[i][y]; 66 } 67 return fa[0][x]; 68 } 69 70 int main() { 71 n = read(); 72 m = read(); 73 k = read(); 74 rep(i,1,n-1) { 75 int x = read(); 76 int y = read(); 77 add(x,y),add(y,x); 78 } 79 dfs(k,0); 80 rep(i,1,n) lg[i] = lg[i-1] + (1<<lg[i-1] == i); 81 while(m--) { 82 int x = read(); 83 int y = read(); 84 printf("%d\n",LCA(x,y)); 85 } 86 return 0; 87 }
CODE3: 树剖
Time cost:1013ms
1 // luogu-judger-enable-o2 2 #include<cstdio> 3 #include<iostream> 4 #include<algorithm> 5 #include<cstring> 6 #include<cmath> 7 #define rep(i,a,n) for(int i = a;i <= n;i++) 8 #define per(i,n,a) for(int i = n;i >= a;i--) 9 #define ls t<<1 10 #define rs t<<1|1 11 using namespace std; 12 typedef long long ll; 13 int read() { 14 int as = 0,fu = 1; 15 char c = getchar(); 16 while(c < '0' || c > '9') { 17 if(c == '-') fu = -1; 18 c = getchar(); 19 } 20 while(c >= '0' && c <= '9') { 21 as = as * 10 + c - '0'; 22 c = getchar(); 23 } 24 return as * fu; 25 } 26 const int N = 1000005; 27 //head 28 29 int n,T,s,mod; 30 int cnt,cur,head[N],nxt[N],mo[N]; 31 int pa[N],mson[N],top[N],sz[N],dep[N]; 32 33 void addline(int x,int y) { 34 mo[++cnt] = y; 35 nxt[cnt] = head[x]; 36 head[x] = cnt; 37 mo[++cnt] = x; 38 nxt[cnt] = head[y]; 39 head[y] = cnt; 40 return; 41 } 42 43 void dfs1(int x,int fa,int d) { 44 dep[x] = d; 45 pa[x] = fa; 46 sz[x] = 1; 47 int maxson = -1; 48 for(int i = head[x]; i; i = nxt[i]) { 49 int sn = mo[i]; 50 if(sn == fa) continue; 51 dfs1(sn,x,d+1); 52 sz[x] += sz[sn]; 53 if(sz[sn] > maxson) { 54 mson[x] = sn; 55 maxson = sz[sn]; 56 } 57 } 58 } 59 void dfs2(int x,int topx) { 60 top[x] = topx; 61 if(mson[x] == 0) return; 62 dfs2(mson[x],topx); 63 for(int i = head[x]; i; i = nxt[i]) { 64 int sn = mo[i]; 65 if(sn == mson[x] || sn == pa[x]) continue; 66 dfs2(sn,sn); 67 } 68 return; 69 } 70 void LCA(int x,int y) { 71 while(top[x] != top[y]) { 72 if(dep[top[x]] < dep[top[y]]) swap(x,y); 73 x = pa[top[x]]; 74 } 75 dep[x] > dep[y] ? printf("%d\n",y) : printf("%d\n",x) ; 76 return; 77 } 78 79 int main() { 80 n = read(); 81 T = read(); 82 s = read(); 83 rep(i,2,n) addline(read(),read()); 84 dfs1(s,0,0); 85 dfs2(s,s); 86 while(T--) { 87 int x = read(); 88 int y = read(); 89 LCA(x,y); 90 } 91 return 0; 92 }
> 别忘了 总有人在等着你

浙公网安备 33010602011771号