离线算法
#include <cstdio> #include <cstring> using namespace std; typedef long long LL; const int maxn = 12; int first[maxn], cnt; int anc[maxn][maxn], n; LL a[maxn]; struct edge { int u, v, next; }e[maxn*2]; int f[maxn], vis[maxn]; void AddEdge(int u, int v) { e[cnt].u = u; e[cnt].v = v; e[cnt].next = first[u]; first[u] = cnt++; e[cnt].u = v; e[cnt].v = u; 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) { f[u] = u; 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); f[v] = u; } for(int i = 1; i <= n; i++) { int v = i; if(vis[v]) { anc[u][v] = anc[v][u] = find(v); } } }