HDU - 2586 How far away ?(最近公共祖先)

There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this “How far is it if I want to go from house A to house B”? Usually it hard to answer. But luckily int this village the answer is always unique, since the roads are built in the way that there is a unique simple path(“simple” means you can’t visit a place twice) between every two houses. Yout task is to answer all these curious people.
Input
First line is a single integer T(T<=10), indicating the number of test cases.
For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road connecting house i and house j,with length k(0 Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.
Output
For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.

模板+1。
这篇博客就专门用来存板子,把倍增,tarjan,rmq树剖什么的都打一遍。

这题任意一点都能作为根,有点意思。

倍增算法:

#include
using namespace std;
typedef long long ll;
const int maxn = 40005;

struct Node {
	int ch, dis;
};

vector<Node> G[maxn];
int rdis[maxn];
int fa[22][maxn], dep[maxn];

void dfs(int u, int f, int dis) {
	fa[0][u] = f;
	rdis[u] = dis;
	dep[u] = dep[f] + 1;
	// printf("~~~~%d\n",dis);
	for(int i = 0; i < G[u].size(); i++) {
		int v = G[u][i].ch;
		if(v == f) {
			continue;
		}
		dfs(v, u, dis + G[u][i].dis);
	}
}

void init(int n) {
	memset(fa, 0, sizeof(fa));
	dep[0] = 0;
	dfs(1, 0, 0);
	for(int k = 0; k + 1 < 20; k++) {
		for(int v = 1; v <= n + n; v++) {
			if(!fa[k][v]) {
				fa[k + 1][v] = 0;
			} else {
				fa[k + 1][v] = fa[k][fa[k][v]];
			}
		}
	}
}

int lca(int u, int v) {
	if(dep[u] > dep[v]) {
		swap(u, v);
	}
	for(int k = 0; k < 20; k++) {
		if((dep[v] - dep[u]) >> k & 1) {
			v = fa[k][v];
		}
	}
	if(u == v) {
		return u;
	}
	for(int k = 20 - 1; k >= 0; k--) {
		if(fa[k][u] != fa[k][v]) {
			u = fa[k][u];
			v = fa[k][v];
		}
	}
	return fa[0][u];
}

int main() {
	int t, n, m;
	scanf("%d", &t);
	while(t--) {
		scanf("%d%d", &n, &m);

		for(int i = 0; i <= n; i++) {
			G[i].clear();
		}

		int i, j, k;
		for(int c = 0; c < n - 1; c++) {
			scanf("%d%d%d", &i, &j, &k);
			G[i].push_back({j, k});
			G[j].push_back({i, k});
		}
		init(n);
		int a, b;
		while(m--) {
			scanf("%d%d", &a, &b);
			int ans = rdis[a] + rdis[b] - rdis[lca(a, b)] * 2;
			printf("%d\n", ans);
		}
	}
	return 0;
}

tarjan:

#include
using namespace std;

const int maxn = 40005;

int t, n, m;
int fa[maxn], rdis[maxn];
int x[maxn], y[maxn], z[maxn];
bool vis[maxn];

struct Node {
	int ch, dis;
};

vector<Node> G[maxn];


int find(int x) {
	return x == fa[x] ? x : fa[x] = find(fa[x]);
}

void merge(int x, int y) {
	x = find(x);
	y = find(y);
	if(x == y) return;
	fa[y] = x;
}

void init() {
	rdis[1] = 0;
	memset(vis, 0, sizeof(vis));
	memset(fa, 0, sizeof(fa));
	for(int i = 1; i <= n; i++) {
		G[i].clear();
	}
}

void tarjan(int u) {
	vis[u] = 1;
	fa[u] = u;
	for(int i = 0; i < m; i++) {
		if(x[i] == u && vis[y[i]]) {
			z[i] = find(y[i]);
		}
		if(y[i] == u && vis[x[i]]) {
			z[i] = find(x[i]);
		}
	}
	for(int i = 0; i < G[u].size(); i++) {
		int v = G[u][i].ch;
		if(!vis[v]) {
			rdis[v] = rdis[u] + G[u][i].dis;
			tarjan(v);
			fa[v] = u;
		}
	}
}

int main() {
	scanf("%d", &t);
	while(t--) {
		init();
		scanf("%d%d", &n, &m);
		int u, v, dis;
		for(int i = 0; i < n - 1; i++) {
			scanf("%d%d%d", &u, &v, &dis);
			G[u].push_back({v, dis});
			G[v].push_back({u, dis});
		}

		for(int i = 0; i < m; i++) {
			scanf("%d%d", &x[i], &y[i]);
		}

		tarjan(1);

		for(int i = 0; i < m; i++) {
			// printf("%d %d %d\n", x[i], y[i], z[i]);
			printf("%d\n", rdis[x[i]] + rdis[y[i]] - rdis[z[i]] * 2);
		}
	}
	return 0;
}

你可能感兴趣的:(ACM,LCA)