SPOJ DISQUERY LCA + 倍增
裸题,如此之水…
Code:
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 100000 + 4;
const int logn = 25;
int f[logn][maxn], head[maxn<<1], to[maxn<<1], nex[maxn<<1], val[maxn<<1], cnt,n,m, F[logn][maxn], G[logn][maxn], dep[maxn];
int minv,maxv;
inline void add_edge(int u,int v,int c)
{
nex[++cnt] = head[u], head[u] = cnt, to[cnt] = v,val[cnt] = c;
}
void dfs(int u,int fa,int c,int cur)
{
f[0][u] = fa, F[0][u] = G[0][u] = c, dep[u] = cur;
for(int v = head[u];v;v = nex[v])
if(to[v] != fa)dfs(to[v],u,val[v],cur + 1);
}
inline void solve(int a,int b)
{
if(dep[a] > dep[b]) swap(a,b);
minv = 50000000, maxv = 0;
if(dep[b] != dep[a])
{
for(int i = 22;i >= 0;--i)
if(dep[f[i][b]] >= dep[a])
{
minv = min(minv, F[i][b]);
maxv = max(maxv, G[i][b]);
b = f[i][b];
}
}
if(a == b) return;
for(int i = 22;i >= 0;--i)
{
if(f[i][a] != f[i][b])
{
minv = min(minv, min(F[i][a], F[i][b]));
maxv = max(maxv, max(G[i][a], G[i][b]));
a = f[i][a], b = f[i][b];
}
}
minv = min(minv, min(F[0][b], F[0][a]));
maxv = max(maxv, max(G[0][b], G[0][a]));
}
int main()
{
freopen("r.in","r",stdin);
freopen("r.out","w",stdout);
scanf("%d",&n);
for(int i = 1;i < n;++i)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);
add_edge(b,a,c);
}
dfs(1,0,0,1);
for(int i = 1; i < logn;++i)
{
for(int j = 1;j <= n;++j)
{
f[i][j] = f[i-1][f[i-1][j]];
F[i][j] = min(F[i-1][j], F[i-1][f[i-1][j]]);
G[i][j] = max(G[i-1][j], G[i-1][f[i-1][j]]);
}
}
scanf("%d",&m);
for(int i = 1;i <= m;++i)
{
int a,b;
scanf("%d%d",&a,&b);
if(a != b)
{
solve(a,b);
printf("%d %d\n",minv,maxv);
}
else printf("0 0\n");
}
fclose(stdin);
fclose(stdout);
return 0;
}