Description:
Y岛风景美丽宜人,气候温和,物产丰富。Y岛上有N个城市,有N-1条城市间的道路连接着它们。每一条道路都连接某两个城市。幸运的
是,小可可通过这些道路可以走遍Y岛的所有城市。神奇的是,乘车经过每条道路所需要的费用都是一样的。小可可,小卡卡和小YY经常
想聚会,每次聚会,他们都会选择一个城市,使得3个人到达这个城市的总费用最小。 由于他们计划中还会有很多次聚会,每次都选择一
个地点是很烦人的事情,所以他们决定把这件事情交给你来完成。他们会提供给你地图以及若干次聚会前他们所处的位置,希望你为他们
的每一次聚会选择一个合适的地点。
Input:
第一行两个正整数,N和M。分别表示城市个数和聚会次数。后面有N-1行,每行用两个正整数A和B表示编号为A和编号为B的城市之间有
一条路。城市的编号是从1到N的。再后面有M行,每行用三个正整数表示一次聚会的情况:小可可所在的城市编号,小卡卡所在的城市
编号以及小YY所在的城市编号。
Output:
一共有M行,每行两个数Pos和Cost,用一个空格隔开。表示第i次聚会的地点选择在编号为Pos的城市,总共的费用是经过Cost条道路所
花费的费用。

思路:思路很一眼,就是对每一组输入,有三种情况,A走到B和C的LCA出,B走到A和C的LCA处,C走到A和B的LCA处,不过细节小心,不要写挂

#include<bits/stdc++.h>
using namespace std;
const int N = 5e5 + ; int head[N], now;
struct edges{
int to, next;
}edge[N<<];
void add(int u,int v){ edge[++now] = {v, head[u]}; head[u] = now;} int n, m, fa[N][], dep[N];
void dfs(int x, int pre){
fa[x][] = pre;
for(int i = head[x]; i; i = edge[i].next){
int v = edge[i].to;
if(v == pre) continue;
dep[v] = dep[x] + ;
dfs(v, x);
}
}
int LCA(int u,int v){
if(dep[v] > dep[u]) swap(u, v);
int k=dep[u]-dep[v];
for(int i=;i<=;i++)
if((<<i)&k) u=fa[u][i];
if(u == v) return u;
for(int i = ; i >= ; i--)
if(fa[u][i] != fa[v][i])
u = fa[u][i], v = fa[v][i];
return fa[u][]; }
int main(){
scanf("%d%d",&n, &m);
int x, y, z;
for(int i = ; i < n; i++){
scanf("%d%d",&x, &y);
add(x, y); add(y, x);
}
dep[] = ; dfs(, );
for(int j = ; j <= ; j++)
for(int i = ; i <= n; i++)
fa[i][j + ] = fa[fa[i][j]][j];
for(int i = ; i <= m; i++){
scanf("%d%d%d",&x, &y, &z);
int lca, pos, tmp, tot = , ans = 1e9; lca = LCA(x, y); tot += dep[x] + dep[y] - *dep[lca];
tmp = LCA(lca, z); tot += dep[z] + dep[lca] - *dep[tmp];
if(tot < ans) ans = tot, pos = lca; swap(x, z); tot = ;
lca = LCA(x, y); tot += dep[x] + dep[y] - *dep[lca];
tmp = LCA(lca, z); tot += dep[z] + dep[lca] - *dep[tmp];
if(tot < ans) ans = tot, pos = lca; swap(y, z); tot = ;
lca = LCA(x, y); tot += dep[x] + dep[y] - *dep[lca];
tmp = LCA(lca, z); tot += dep[z] + dep[lca] - *dep[tmp];
if(tot < ans) ans = tot, pos = lca; printf("%d %d\n", pos, ans);
}
return ;
}
05-11 21:59