poj 1986 Distance Queries(LCA:倍增/离线)

时间:2022-01-05 06:15:53

计算树上的路径长度。input要去查poj 1984。

任意建一棵树,利用树形结构,将问题转化为u,v,lca(u,v)三个点到根的距离。输出d[u]+d[v]-2*d[lca(u,v)]。

倍增求解:

 #include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define clr(a,m) memset(a,m,sizeof(a))
using namespace std; const int MAXN=;
const int POW = ; struct Edge{
int v,next,c;
Edge(){}
Edge(int _v,int _c,int _next):v(_v),c(_c),next(_next){}
}edge[MAXN<<]; int head[MAXN],tol;
int p[MAXN][POW],d[MAXN];
int vis[MAXN],dis[MAXN];
queue<int>q; void init()
{
tol=;
clr(head,-);
} void add(int u,int v,int c)
{
edge[tol]=Edge(v,c,head[u]);
head[u]=tol++;
} void bfs(int x)
{
clr(vis,);
clr(dis,);
while(!q.empty())
q.pop();
q.push(x);
vis[x]=;
dis[x]=;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].v;
if(vis[v])
continue;
q.push(v);
vis[v]=;
dis[v]=dis[u]+edge[i].c;
}
}
} void dfs(int u,int fa){
d[u]=d[fa]+;
p[u][]=fa;
for(int i=;i<POW;i++) p[u][i]=p[p[u][i-]][i-];
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v;
if(v==fa) continue;
dfs(v,u);
}
} int lca( int a, int b ){
if( d[a] > d[b] ) a ^= b, b ^= a, a ^= b;
if( d[a] < d[b] ){
int del = d[b] - d[a];
for( int i = ; i < POW; i++ ) if(del&(<<i)) b=p[b][i];
}
if( a != b ){
for( int i = POW-; i >= ; i-- )
if( p[a][i] != p[b][i] )
a = p[a][i] , b = p[b][i];
a = p[a][], b = p[b][];
}
return a;
} void LCA(int n)
{
clr(p,);
d[]=;
dfs(,); bfs(); int k;
scanf("%d",&k);
int u,v;
rep(i,,k){
scanf("%d%d",&u,&v);
printf("%d\n",dis[u]+dis[v]-*dis[lca(u,v)]);
}
} int main()
{
int n,m;
scanf("%d%d",&n,&m);
init();
rep(i,,m){
int u,v,c;
scanf("%d%d%d%*s",&u,&v,&c);
add(u,v,c);
add(v,u,c);
} LCA(n);
return ;
}

又用tarjin离线做了一遍= =

 #include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define clr(a,m) memset(a,m,sizeof(a))
using namespace std; const int MAXN=; struct Edge{
int v,next,c;
Edge(){}
Edge(int _v,int _c,int _next):v(_v),c(_c),next(_next){}
}edge[MAXN<<]; struct EDGE{
int u,v;
int ans;
EDGE(){}
EDGE(int _u,int _v):u(_u),v(_v),ans(-){}
}; int head[MAXN],tol;
int p[MAXN],vis[MAXN],dis[MAXN]; queue<int>q;
vector<int>query[MAXN];
vector<EDGE>G; void init()
{
tol=;
clr(head,-);
} void add(int u,int v,int c)
{
edge[tol]=Edge(v,c,head[u]);
head[u]=tol++;
} void build(int m)
{
init();
rep(i,,m){
int u,v,c;
scanf("%d%d%d%*s",&u,&v,&c);
add(u,v,c);
add(v,u,c);
} int k;
scanf("%d",&k);
rep(i,,k){
int u,v,siz;
scanf("%d%d",&u,&v); G.push_back(EDGE(u,v));
siz=G.size();
query[u].push_back(siz-); G.push_back(EDGE(v,u));
siz=G.size();
query[v].push_back(siz-);
}
} void bfs(int x)
{
clr(vis,);
clr(dis,);
while(!q.empty())
q.pop();
q.push(x);
vis[x]=;
dis[x]=;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].v;
if(vis[v])
continue;
q.push(v);
vis[v]=;
dis[v]=dis[u]+edge[i].c;
}
}
} int find(int x)
{
return (x==p[x])?x:(p[x]=find(p[x]));
} void dfs(int x)
{
vis[x]=;
for(int i=head[x];i!=-;i=edge[i].next)
{
int v=edge[i].v;
if(vis[v])
continue;
dfs(v);
p[v]=x;
} int siz =query[x].size()-;
rep(i,,siz)
{
int t=query[x][i];
if(vis[G[t].v]){
G[t].ans=find(G[t].v);
}
}
} void LCA(int rt,int n)
{
clr(vis,);
rep(i,,n)
p[i]=i;
dfs(rt);
} void PRT()
{
int siz=G.size();
for(int i=;i<siz;i+=)
{
int u=G[i].u;
int v=G[i].v;
if(G[i].ans!=-)
printf("%d\n",dis[u]+dis[v]-*dis[G[i].ans]);
else
printf("%d\n",dis[u]+dis[v]-*dis[G[i^].ans]);
}
} int main()
{
int n,m;
scanf("%d%d",&n,&m);
build(m);
bfs();
LCA(,n);
PRT();
return ;
}