这道题我用了求LCA问题的Tarjan算法。题目同样很基础,适合初学者。
这个算法是一个离线算法,所以要先读入查询的问题,才能解决问题,当然这并不是这个算法的难点,只是一个特点。而且这个算法基于深度优先搜索框架,并且要借助并查集查找祖先。大概思路是,假设查询u,v的公共祖先,首先从树的根开始进行深度优先搜索,如果先搜到节点u,而u未被访问,则标记已访问,并继续深搜,如果搜索到u节点,此时v已经被搜索过,那么他们此时最近公共祖先就是此时他们共同的祖先。细节参考下面的代码。更具体可以参考《ACM—ICPC程序设计系列 图论及应用》(哈尔滨工业大学出版社)P61。
不过这道题并不是直接求公共祖先,而且题目在描述输出格式上有误,并不需要输出额外的空行。
代码(G++):
#include <cstdlib>
#include <iostream>
#define MAX 40009
using namespace std;
struct Edge{
int to;
int next;
int w;
};
Edge edge[2*MAX];
struct Ques{
int to;
int next;
int lca;
};
Ques array[210*2];
int head[MAX],faw[MAX],fa[MAX],pre[MAX],index[MAX]; //index数组也要开到MAX,而不是210
bool vis[MAX];
int find(int a)
{
if(pre[a]!=a) pre[a]=find(pre[a]);
return pre[a];
}
void Tarjan(int u)
{
int i,v;
pre[u]=u;
vis[u]=true;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].to;
if(!vis[v])
{
faw[v]=edge[i].w;
fa[v]=u;
Tarjan(v);
pre[v]=u; //这句要放在函数的后面
}
}
for(i=index[u];i!=-1;i=array[i].next)
{
v=array[i].to;
if(vis[v])
{
array[i].lca=find(v);
array[i^1].lca=array[i].lca;
}
}
}
int main(int argc, char *argv[])
{
int t,n,m,i,j,u,v,k,c,lca,x,ans;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
c=0;
memset(head,-1,sizeof(head));
memset(vis,false,sizeof(vis));
for(i=1;i<n;i++)
{
scanf("%d %d %d",&u,&v,&k);
edge[c].to=v;
edge[c].w=k;
edge[c].next=head[u];
head[u]=c;
c++;
edge[c].to=u;
edge[c].w=k;
edge[c].next=head[v];
head[v]=c;
c++;
}
c=0;
memset(index,-1,sizeof(index));
for(i=0;i<m;i++)
{
scanf("%d %d",&u,&v);
array[c].to=v;
array[c].next=index[u];
index[u]=c;
c++;
array[c].to=u;
array[c].next=index[v];
index[v]=c;
c++;
}
fa[1]=1;
Tarjan(1);
for(i=0;i<m*2;i+=2)
{
ans=0;
lca=array[i].lca;
x=array[i].to;
while(x!=lca)
{
ans+=faw[x];
x=fa[x];
}
x=array[i+1].to;
while(x!=lca)
{
ans+=faw[x];
x=fa[x];
}
cout<<ans<<endl;
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
题目( http://acm.hdu.edu.cn/showproblem.php?pid=2586):
How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Problem Description
There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int this village the answer is always unique, since the roads are built in the way that there is a unique simple path("simple" means you can't visit a place twice) between every two houses. Yout task is to answer all these curious people.
Input
First line is a single integer T(T<=10), indicating the number of test cases.
For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n.
Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.
For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n.
Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.
Output
For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.
Sample Input
2 3 2 1 2 10 3 1 15 1 2 2 3 2 2 1 2 100 1 2 2 1
Sample Output
10 25 100 100