LCA tarjan 的离线算法
#include <cstdio> #include <cstring> #include <vector> using namespace std; const int maxn = 40010; int first[maxn], head[maxn], cnt, sum; struct edge { int u, v, w, next; }e[maxn*2], qe[maxn], Q[maxn]; int ans[maxn]; int f[maxn], vis[maxn]; int d[maxn]; void AddEdge(int u, int v, int w) { e[cnt].u = u; e[cnt].v = v; e[cnt].w = w; e[cnt].next = first[u]; first[u] = cnt++; e[cnt].u = v; e[cnt].v = u; e[cnt].w = w; e[cnt].next = first[v]; first[v] = cnt++; } int find(int x) { if(f[x] != x) return f[x] = find(f[x]); return f[x]; } void LCA(int u, int k) { f[u] = u; d[u] = k; vis[u] = true; for(int i = first[u]; i != -1; i = e[i].next) { int v = e[i].v; if(vis[v]) continue; LCA(v, k + e[i].w); f[v] = u; } for(int i = head[u]; i != -1; i = qe[i].next) { int v = qe[i].v; if(vis[v]) { ans[qe[i].w] = find(v); } } }
原文地址:http://blog.csdn.net/u011686226/article/details/37565637