旅行(LCA)

Description

N-1座桥连接着N个岛屿,每座桥都连接着某两个不同的岛屿,从任意一个岛屿都可以到达所有的其他岛屿,过桥需要缴纳人民币1元的过桥费。
由于某些不可透露的原因,Jason和他的2个小伙伴可以在任意一个岛屿集合,但是希望总过桥费最少。
现在,由你来确定集合的岛屿编号,使得总过桥费最少。

Input Format

第一行有两个整数N和M,表示岛屿的个数和询问次数,岛屿编号从1到N。
接下来N-1行,每行有两个正整数X、Y,表示编号为X的岛屿和编号为Y的岛屿之间有一座桥。
最后还有M行,每行有三个正整数A、B、C,表示Jason和他的两个小伙伴所在岛屿的编号。

Output Format

一共有M行,每行两个整数P、Q,用一个空格隔开。其中第i行表示第i次询问集合的地点在编号为P的岛屿,需要花费的最少过桥费为Q。

Sample Output

5 5
1 2
1 3
2 4
2 5
1 1 1
1 1 2
1 2 3
4 5 3
5 2 3

Sample Output

1 0
1 1
1 2
2 4
2 3

Hint

30%的数据中,N≤200,M≤200;
60%的数据中,N≤2,000,M≤2,000;
100%的数据中,N≤200,000,M≤200,000;

Solution

显然这是一个树形结构,可以发现,3个地点A,B,C,一定存在2对的LCA相同,例如LCA(A,B)==LCA(A,C),那么集合点就在LCA(B,C),画一画图会更好理解

还有几种特殊的情况经检验也符合上述情况

Code

#include <cstdio>
#include <algorithm>
#include <cmath>
#define N 200010
#define M 400010

struct info {int to, nex;} e[M];
int n, m, tot, head[M], fa[N][22], dep[N], _log;
bool vis[N];

void dfs(int u) {
	vis[u] = 1;
	for (int i = 1; dep[u] >= (1 << i); ++i)
		fa[u][i] = fa[fa[u][i - 1]][i - 1];

	for (int i = head[u]; i; i = e[i].nex) {
		int v = e[i].to;
		if (vis[v]) continue;
		dep[v] = dep[u] + 1;
		fa[v][0] = u;
		dfs(v);
	}
}

inline int read() {
	int x = 0, f = 1; char ch = getchar();
	while (ch < '0' || ch > '9') {if (ch == '-')f = -1; ch = getchar();}
	while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
	return x * f;
}

inline void add_edge(int u, int v) {
	e[++tot].to = v;
	e[tot].nex = head[u];
	head[u] = tot;
}

int LCA(int u, int v) {
	if (dep[u] > dep[v])
		std::swap(u, v);

	int d = dep[v] - dep[u];
	for (int i = 0; i <= _log; ++i)
		if (d & (1 << i))
			v = fa[v][i];

	if (u == v) return u;

	for (int i = _log; i >= 0; i--)
		if (fa[u][i] != fa[v][i]) {
			u = fa[u][i];
			v = fa[v][i];
		}

	return fa[u][0];
}

inline void work(int a, int b, int c) {
	int ab = LCA(a, b);
	int ans = dep[a] + dep[b] - 2 * dep[ab] + dep[ab] + dep[c] - 2 * dep[LCA(ab, c)];
	printf("%d %d\n", ab, ans);
}

int main() {
	n = read(), m = read();
	_log = log(n) / log(2);
	for (int i = 1; i < n; ++i) {
		int u = read(), v = read();
		add_edge(u, v);
		add_edge(v, u);
	}
	dfs(1);

	while (m--) {
		int a = read(), b = read(), c = read();
		int ab = LCA(a, b), ac = LCA(a, c), bc = LCA(b, c);
		if (ac == bc) work(a, b, c);
		else if (ab == bc) work(a, c, b);
		else if (ab == ac) work(b, c, a);
	}
	return 0;
}
posted @ 2017-10-18 11:38  void_f  阅读(245)  评论(0编辑  收藏  举报