# [bzoj1787][Ahoi2008]Meet 紧急集合

$\therefore lca_{a,b,c}==lca_{a,b}==lca_{a,c}$
\begin{align*} ans&=d_{b,c}+d_{lca_{b,c},a}\\ &=d_{b,lca_{b,c}}+d_{lca_{b,c},c}+d_{lca_{b,c},lca_{a,b,c}}+d_{lca_{a,b,c},a}\\ &=depth_b+depth_c-2 \cdot depth_{lca_{b,c}}+depth_{lca_{b,c}}+depth_a-2\cdot depth_{lca_{a,b}}\\ &=depth_a+depth_b+depth_c+depth_{lca_{b,c}}-2\cdot depth_{lca_{a,b}} \end{align*}

$$ans=depth_a+depth_b+depth_c+depth+\max\{depth_{lca_{a,b}},depth_{lca_{a,c}},depth_{lca_{b,c}}\}-2\cdot\min\{depth_{lca_{a,b}},depth_{lca_{a,c}},depth_{lca_{b,c}}\}$$

C++ Code：

#include <cstdio>
#define maxn 500010
#define lb(x) (x & -x)
using namespace std;
int n, m;
inline void swap(int &a, int &b) {a ^= b ^= a ^= b;}

struct Edge {
int to, nxt;
} e[maxn << 1];
void addE(int a, int b) {
}

void dfs(int rt) {
for (int i = head[rt]; i; i = e[i].nxt) {
int v = e[i].to;
dep[v] = dep[rt] + 1;
dfs(v);
}
}
}
void init() {
for (int i = 1; i < 19; i++) {
for (int j = 1; j <= n; j++) {
}
}
}
int LCA(int x, int y) {
if (x == y) return x;
if (dep[x] < dep[y]) swap(x, y);
for (int t = dep[x] - dep[y]; t; t &= t - 1) x = dad[x][__builtin_ctz(lb(t))];
if (x == y) return x;
}

int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i < n; i++) {
int a, b;
scanf("%d%d", &a, &b);
}
dep[1] = 1;
dfs(1);
init();
while (m --> 0) {
int a, b, c, ans = 0;
scanf("%d%d%d", &a, &b, &c);
int lca_ab = LCA(a, b), lca_ac = LCA(a, c), lca_bc = LCA(b, c);
printf("%d ", lca_ab ^ lca_ac ^ lca_bc);
if (lca_ab == lca_ac) ans = dep[b] + dep[c] - dep[lca_bc] + dep[a] - (dep[lca_ac] << 1);
else {
if (lca_bc == lca_ab) ans = dep[a] + dep[c] - dep[lca_ac] + dep[b] - (dep[lca_ab] << 1);
else ans = dep[a] + dep[b] - dep[lca_ab] + dep[c] - (dep[lca_ac] << 1);
}
printf("%d\n", ans);

}
return 0;
}


posted @ 2018-08-21 18:38  Memory_of_winter  阅读(115)  评论(0编辑  收藏  举报