标签:输出 距离 blog efi def sof logs ros microsoft
题目描述
输入
输出
样例输入
3 1 2
1 2
2 3
3 4
1 1
样例输出
3 2
题解
树上倍增+LCA,裸题不需要过多解释= =
直接求LCA,得出两点之间距离,如果能够走到就走,走不到的话,最终位置一定是两点之一的k级祖先,树上倍增求解。
#include <cstdio> #include <cstring> #include <algorithm> #define N 1000010 using namespace std; int head[N] , to[N << 1] , next[N << 1] , cnt , fa[N][22] , deep[N] , log[N]; void add(int x , int y) { to[++cnt] = y , next[cnt] = head[x] , head[x] = cnt; } void dfs(int x) { int i; for(i = 1 ; (1 << i) <= deep[x] ; i ++ ) fa[x][i] = fa[fa[x][i - 1]][i - 1]; for(i = head[x] ; i ; i = next[i]) if(to[i] != fa[x][0]) fa[to[i]][0] = x , deep[to[i]] = deep[x] + 1 , dfs(to[i]); } int lca(int x , int y) { int i; if(deep[x] < deep[y]) swap(x , y); for(i = log[deep[x] - deep[y]] ; ~i ; i -- ) if((1 << i) <= deep[x] - deep[y]) x = fa[x][i]; if(x == y) return x; for(i = log[deep[x]] ; ~i ; i -- ) if((1 << i) <= deep[x] && fa[x][i] != fa[y][i]) x = fa[x][i] , y = fa[y][i]; return fa[x][0]; } int solve(int x , int k) { int i; for(i = log[k] ; ~i ; i -- ) if((1 << i) <= k) x = fa[x][i] , k -= (1 << i); return x; } int main() { int n , p , m , i , x , y , t; scanf("%d%d%d" , &n , &p , &m); for(i = 2 ; i <= n ; i ++ ) scanf("%d%d" , &x , &y) , add(x , y) , add(y , x) , log[i] = log[i >> 1] + 1; dfs(1); for(i = 1 ; i <= m ; i ++ ) { scanf("%d%d" , &x , &y) , t = lca(p , x); if(deep[p] + deep[x] - 2 * deep[t] <= y) p = x; else if(deep[p] - deep[t] >= y) p = solve(p , y); else p = solve(x , deep[p] + deep[x] - 2 * deep[t] - y); printf("%d " , p); } return 0; }
【bzoj4281】[ONTAK2015]Zwi?zek Harcerstwa Bajtockiego 树上倍增+LCA
标签:输出 距离 blog efi def sof logs ros microsoft
原文地址:http://www.cnblogs.com/GXZlegend/p/7434632.html