zoukankan      html  css  js  c++  java
  • 倍增LCA模板

    //https://www.luogu.org/problemnew/show/P3379
    #include
    <bits/stdc++.h> #define maxn 500010 #define maxm 1000010 using namespace std; struct edge{ int to,next; }e[maxm]; int point[maxn],n,m,rt,cnt; int fa[maxn][20],depth[maxn]; void addedge(int x,int y) { e[++cnt].next=point[x]; e[cnt].to=y; point[x]=cnt; } void dfs(int x,int from) { depth[x]=depth[from]+1; fa[x][0]=from; for(int i=point[x];i;i=e[i].next) { int y=e[i].to; if(y==from) continue; dfs(y,x); } } int go_up(int x,int d) { for(int j=19;j>=0;j--) if((1<<j)&d) x=fa[x][j]; return x; } int main() { scanf("%d%d%d",&n,&m,&rt); for(int i=1;i<=n-1;i++) { int x,y; scanf("%d%d",&x,&y); addedge(x,y); addedge(y,x); } dfs(rt,rt); for(int j=1;j<=19;j++) for(int i=1;i<=n;i++) fa[i][j]=fa[fa[i][j-1]][j-1]; for(int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); if(depth[x]>depth[y]) x=go_up(x,depth[x]-depth[y]); if(depth[y]>depth[x]) y=go_up(y,depth[y]-depth[x]); for(int j=19;j>=0;j--) if(fa[x][j]!=fa[y][j]) x=fa[x][j],y=fa[y][j]; if(x!=y) x=fa[x][0]; printf("%d ",x); } return 0; }
  • 相关阅读:
    JVM Inline
    Lattice
    编译技术
    sql-server-on-linux
    concurrency 方面的books
    Linux debugger lldb
    javaperformanceoptimization
    Understanding The Linux Virtual Memory Manager
    web performance tu ning
    linux io architecture
  • 原文地址:https://www.cnblogs.com/Tidoblogs/p/11217259.html
Copyright © 2011-2022 走看看