[POJ 1986] Distance Queries【倍增LCA / TarjanLCA】

  • 2018-01-09
  • 0
  • 0

Problem:

Time Limit: 2000MS Memory Limit: 30000K
Case Time Limit: 1000MS

Description

Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible!

Input

* Lines 1..1+M: Same format as "Navigation Nightmare"

* Line 2+M: A single integer, K. 1 <= K <= 10,000

* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms.

Output

* Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance.

Sample Input

7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6
1 4
2 6

Sample Output

13
3
36

Hint

Farms 2 and 6 are 20+3+13=36 apart.

Source

USACO 2004 February

Solution:

本题是最近公共祖先 (Least Common Ancestor, LCA) 的模板题。

求 LCA 常用的方法有倍增TarjanDFS + ST 表

倍增是在线算法,时间复杂度为 O(NlogN+M+Q) ,理论上较优,模板见 Code #1

Tarjan 是离线算法,时间复杂度为 O(N+(M+Q)*α(M+Q,N)) ≈ O(N+M+Q),模板见 Code #2

DFS + ST 表是在线算法,然而我没有写。。

【倍增思想求 LCA】

首先通过 DFS 预处理出每个点的父节点 fa[] 和深度(即与根节点的距离)dep[]。

再通过倍增(字面上理解即“成倍加增”)的思想预处理出每个点 u 向根节点方向2i 步所到达的点 jmp[i][u]。

每次询问时,先将较深点跳到与较浅点相同深度,然后从大到小枚举 i,每当两点均向根节点方向跳 2i 步后不重合就向上跳,LCA 就是最后到达的点的父节点

【Tarjan 算法求 LCA】

这篇文章讲的比较透彻:https://www.cnblogs.com/JVxie/p/4854719.html

Code #1: O(NlogN+M+Q) [4832K, 188MS]

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int N, M, K;
char tmp[5];

struct Edge{
	int np, val;
	Edge *nxt;
};

struct Graph{
	Edge *V[40002], E[80002];
	int tope;
	
	inline void addedge(int u, int v, int w){
		E[++tope].np = v, E[tope].val = w;
		E[tope].nxt = V[u], V[u] = &E[tope];
	}
} G;

int fa[40002], dep[40002], dis[40002];
int jmp[16][40002];

inline void dfs(int u){
	for(register Edge *ne = G.V[u]; ne; ne = ne->nxt){
		if(ne->np == fa[u]) continue;
		fa[ne->np] = u, dep[ne->np] = dep[u] + 1, dis[ne->np] = dis[u] + ne->val;
		dfs(ne->np);
	}
}

inline void init_LCA(){
	for(register int i = 1; i <= N; i++) jmp[0][i] = fa[i];
	for(register int ex = 1; ex < 16; ex++)
		for(register int i = 1; i <= N; i++)
			jmp[ex][i] = jmp[ex - 1][jmp[ex - 1][i]];
}

inline int get_LCA(int u, int v){
	if(dep[u] < dep[v]) u ^= v, v ^= u, u ^= v;
	int ddep = dep[u] - dep[v];
	for(register int ex = 15; ex >= 0; ex--)
		if(ddep & 1 << ex) u = jmp[ex][u];
	if(u == v) return v;
	for(register int ex = 15; ex >= 0; ex--)
		if(jmp[ex][u] != jmp[ex][v])
			u = jmp[ex][u], v = jmp[ex][v];
	return fa[v];
}

int main(){
	scanf("%d%d", &N, &M);
	for(register int i = 1; i <= M; i++){
		int u, v, w;
		scanf("%d%d%d", &u, &v, &w), fgets(tmp, 5, stdin);
		G.addedge(u, v, w), G.addedge(v, u, w);
	}
	fa[1] = 0, dep[1] = 1, dis[1] = 0;
	dfs(1), init_LCA();
	scanf("%d", &K);
	for(register int i = 1; i <= K; i++){
		int u, v, lca;
		scanf("%d%d", &u, &v), lca = get_LCA(u, v);
		printf("%d\n", dis[u] + dis[v] - (dis[lca] << 1));
	}
	return 0;
}

Code #2: O(N+(M+Q)*α(M+Q,N)) [3336K, 172MS]

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int N, M, K;
char tmp[5];

struct Edge{
	int np, val, id;
	Edge *nxt;
};

struct Graph{
	Edge *V[40002], E[80002];
	int tope;
	
	inline void addedge(int u, int v, int w){
		E[++tope].np = v, E[tope].val = w;
		E[tope].nxt = V[u], V[u] = &E[tope];
	}
} G, Q;

struct Disjoint_Set{
	int fa[40002];
	
	inline void Init() {for(register int i = 1; i <= N; i++) fa[i] = i;}
	
	inline int Find(int u){
		if(fa[u] == u) return u;
		return fa[u] = Find(fa[u]);
	}
	
	inline void Union(int u, int v) {fa[Find(u)] = Find(v);}
} ds;

int dis[40002], ans[10002];
bool vis[40002];

inline void Tarjan(int u){
	vis[u] = 1;
	for(register Edge *ne = G.V[u]; ne; ne = ne->nxt)
		if(!vis[ne->np]){
			dis[ne->np] = dis[u] + ne->val;
			Tarjan(ne->np);
			ds.Union(ne->np, u);
		}
	for(register Edge *ne = Q.V[u]; ne; ne = ne->nxt)
		if(vis[ne->np]) ne->val = ds.Find(ne->np);
}

int main(){
	scanf("%d%d", &N, &M);
	for(register int i = 1; i <= M; i++){
		int u, v, w;
		scanf("%d%d%d", &u, &v, &w), fgets(tmp, 5, stdin);
		G.addedge(u, v, w), G.addedge(v, u, w);
	}
	scanf("%d", &K);
	for(register int i = 1; i <= K; i++){
		int u, v;
		scanf("%d%d", &u, &v);
		Q.addedge(u, v, 0), Q.E[Q.tope].id = i;
		Q.addedge(v, u, 0), Q.E[Q.tope].id = i;
	}
	ds.Init(), Tarjan(1);
	for(register int i = 1; i <= N; i++)
		for(register Edge *ne = Q.V[i]; ne; ne = ne->nxt)
			if(ne->val) ans[ne->id] = dis[i] + dis[ne->np] - (dis[ne->val] << 1);
	for(register int i = 1; i <= K; i++) printf("%d\n", ans[i]);
	return 0;
}

评论

还没有任何评论,你来说两句吧



常年不在线的QQ:
49750

不定期更新的GitHub:
https://github.com/Darkleafin


OPEN AT 2017.12.10

如遇到代码不能正常显示的情况,请刷新页面。
If the code cannot be displayed normally, please refresh the page.


发现一个优美的网站:
https://visualgo.net/en
















- Theme by Qzhai