# Distance Queries POJ - 1986 (树上两点的最近公共祖先)

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

using namespace std;

const int MAXN = 80080;
const int MAXNQ = 20020;

int father[MAXN];
int Dist[MAXN];

struct EdgeNode{
int to;
int next ;
int lca;
}Edges[MAXN];

EdgeNode QEdges[MAXN];

int find(int x){
if(x != father[x])
father[x] = find(father[x]);
return father[x];
}

bool vis[MAXN];

int LCA(int u){
father[u] = u;
vis[u] = true;
for(int k = Head[u] ; k != -1 ; k = Edges[k].next){
if(!vis[Edges[k].to]){
Dist[Edges[k].to] = Dist[u] + Edges[k].lca;
LCA(Edges[k].to);
father[Edges[k].to] = u;
}
}
for(int k = QHead[u] ; k != -1 ;k = QEdges[k].next){
if(vis[QEdges[k].to]){
QEdges[k].lca = Dist[u] + Dist[QEdges[k].to] - 2 * Dist[find(QEdges[k].to)];
QEdges[k^1].lca = QEdges[k].lca;
}
}
}

int main(){
int N,M,K,u,v,w,a,b;
char s;
while(~scanf("%d%d",&N,&M)){
memset(father,0,sizeof(father));
memset(vis,0,sizeof(vis));
memset(Edges,0,sizeof(Edges));
memset(QEdges,0,sizeof(QEdges));
memset(Dist,0,sizeof(Dist));
int id = 0;
for(int i = 0 ; i < M ; i ++){
scanf("%d%d%d %c",&u,&v,&w,&s);
Edges[id].to = v;
Edges[id].lca = w;
Edges[id].to = u;
Edges[id].lca = w;
}
scanf("%d",&K);
int iq = 0;
for(int i = 0 ; i < K ; i ++){
scanf("%d%d",&a,&b);
QEdges[iq].to = b;
QEdges[iq].to = a;
}
LCA(1);
for(int i = 0 ; i < iq ; i += 2)
printf("%d\n",QEdges[i].lca);
}
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120