LCA
题意:LCA模板题,输入n和m,表示n个点m条边,下面m行是边的信息,两端点和权,后面的那个字母无视掉,没用的。接着k,下面k个询问lca,输出即可
有人说要考虑不连通的情况,我没考虑AC了,另外可能有u,u这样的询问,不过这不影响,照样是写模板,没有特判,一样能过
还是Tarjan快一些
LCA转RMQ在线算法
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; #define N 40010 #define M 25 int tot; int __pow[M]; int head[N]; struct edge{ int u,v,w,next; }e[2*N]; int ver[2*N],R[2*N],first[N],dir[N]; int dp[2*N][25]; bool vis[N]; inline void add(int u , int v ,int w ,int &k) { e[k].u = u; e[k].v = v; e[k].w = w; e[k].next = head[u]; head[u] = k++; u = u^v; v = u^v; u = u^v; e[k].u = u; e[k].v = v; e[k].w = w; e[k].next = head[u]; head[u] = k++; } void dfs(int u ,int dep) { vis[u] = true; first[u] = ++tot; ver[tot] = u; R[tot] = dep; for(int k=head[u]; k!=-1; k=e[k].next) if( !vis[e[k].v] ) { int v = e[k].v , w = e[k].w; dir[v] = dir[u] + w; dfs(v,dep+1); ver[++tot] = u; R[tot] = dep; } } void ST(int len) { int K = (int)(log((double)(len)) / log(2.0)); for(int i=1; i<=len; i++) dp[i][0] = i; for(int j=1; j<=K; j++) for(int i=1; i+__pow[j]-1 <= len; i++) { int a = dp[i][j-1] , b = dp[i+__pow[j-1]][j-1]; if(R[a] < R[b]) dp[i][j] = a; else dp[i][j] = b; } } int RMQ(int x ,int y) { int K = (int)(log((double)(y-x+1)) / log(2.0)); int a = dp[x][K] , b = dp[y-__pow[K]+1][K]; if(R[a] < R[b]) return a; else return b; } int LCA(int u ,int v) { int x = first[u] , y = first[v]; if(x > y) swap(x,y); int index = RMQ(x,y); return ver[index]; } int main() { for(int i=0; i<M; i++) __pow[i] = (1<<i); int n,m,k,str[10]; while(scanf("%d%d",&n,&m)!=EOF) { k = 0; memset(head,-1,sizeof(head)); memset(vis,false,sizeof(vis)); while(m--) { int u,v,w; scanf("%d%d%d%s",&u,&v,&w,str); add(u,v,w,k); } tot = dir[1] = 0; dfs(1,1); ST(tot); int q; scanf("%d",&q); while(q--) { int u,v,lca; scanf("%d%d",&u,&v); lca = LCA(u,v); printf("%d\n",dir[u] + dir[v] - 2*dir[lca]); } } return 0; }
Tarjan离线算法
#include <iostream> #include <cstdio> #include <cstring> using namespace std; #define N 40010 #define M 20010 int head[N]; struct edge{ int u,v,w,next; }e[2*N]; int __head[N]; struct ask{ int u,v,lca,next; }ea[M]; int fa[N],ance[N],dir[N]; bool vis[N]; inline void add_edge(int u ,int v ,int w ,int &k) { e[k].u = u; e[k].v = v; e[k].w = w; e[k].next = head[u]; head[u] = k++; u = u^v; v = u^v; u = u^v; e[k].u = u; e[k].v = v; e[k].w = w; e[k].next = head[u]; head[u] = k++; } inline void add_ask(int u ,int v ,int &k) { ea[k].u = u; ea[k].v = v; ea[k].lca = -1; ea[k].next = __head[u]; __head[u] = k++; u = u^v; v = u^v; u = u^v; ea[k].u = u; ea[k].v = v; ea[k].lca = -1; ea[k].next = __head[u]; __head[u] = k++; } int find(int x){ return x == fa[x] ? x : fa[x] = find(fa[x]); } void Tarjan(int u) { vis[u] = true; ance[u] = fa[u] = u; for(int k=head[u]; k!=-1; k=e[k].next) if( !vis[e[k].v] ) { int v = e[k].v , w = e[k].w; dir[v] = dir[u] + w; Tarjan(v); fa[v] = u; } for(int k=__head[u]; k!=-1; k=ea[k].next) if( vis[ea[k].v] ) ea[k].lca = ea[k^1].lca = ance[find(ea[k].v)]; } int main() { int n,m,q,k; char str[10]; while(scanf("%d%d",&n,&m)!=EOF) { memset(head,-1,sizeof(head)); memset(__head,-1,sizeof(__head)); memset(vis,false,sizeof(vis)); k = 0; while(m--) { int u,v,w; scanf("%d%d%d%s",&u,&v,&w,str); add_edge(u,v,w,k); } scanf("%d",&q); k = 0; for(int i=0; i<q; i++) { int u ,v; scanf("%d%d",&u,&v); add_ask(u,v,k); } dir[1] = 0; Tarjan(1); for(int i=0; i<q; i++) { int s = i*2 , u = ea[s].u , v = ea[s].v , lca = ea[s].lca; printf("%d\n",dir[u] + dir[v] - 2*dir[lca]); } } return 0; }