[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:

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

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

【倍增思想求 LCA】

【Tarjan 算法求 LCA】

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);
}
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);
}
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;
}
```

评论

darkleafin.cf
(该域名已过期且被抢注。。)
darkleafin.github.io

49750

https://github.com/Darkleafin

OPEN AT 2017.12.10

Please refresh the page if the code cannot be displayed normally.

https://visualgo.net/en

- Theme by Qzhai