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 ..+M: Same format as "Navigation Nightmare" * Line +M: A single integer, K. <= K <= , * Lines +M..+M+K: Each line corresponds to a distance query and contains the indices of two farms.
Output
* Lines ..K: For each distance query, output on a single line an integer giving the appropriate distance.
Sample Input
E
E
S
N
W
S
Sample Output
Hint
Farms and are ++= apart.
Source
题意:给一棵带权重的树,共有k个查询,每次查询树中2个结点的距离。结点数n最大为40000,k最大10000
分析:首先我们将无根树转为有根树,可以在O(n)时间内得到每个结点到根结点的距离。由于在树中从一个结点走到另一个结点的路径是唯一的,所以a到b的路径一定经过lca(a,b),设lca(a,b)=c。此时不难发现d(a,b)=d(a,root)+d(b,root)-2*d(c,root)。
这里用的是并查集的方法查找LCA,时间1000+ms,感觉有点慢
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 100006
#define inf 1e12
int n,m; int tot,qsize;
int head[N],qhead[N];
int vis[N];//标记
int dis[N];//距离
int fa[N]; struct Node
{
int from;
int to;
int next;
int cost;
}edge[N<<],qe[N];
void init()
{
tot=; qsize=; memset(head,-,sizeof(head));
memset(qhead,-,sizeof(qhead));
memset(vis,,sizeof(vis));
memset(fa,,sizeof(fa));
memset(dis,,sizeof(dis));
memset(edge,,sizeof(edge));
memset(qe,,sizeof(qe));
}
void addEdge(int s,int u,int c)//邻接矩阵函数
{
edge[tot].from=s;
edge[tot].to=u;
edge[tot].cost=c;
edge[tot].next=head[s];
head[s]=tot++;
}
void addQedge(int s,int u){
qe[qsize].from=s;
qe[qsize].to=u;
qe[qsize].next=qhead[s];
qhead[s]=qsize++;
} /////////////////////////////////////////////////////////////
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
/////////////////////////////////////////////////////////////
void tarjan(int u)//tarjan算法找出图中的所有强连通分支
{
fa[u]=u;
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
if(!vis[v]){
dis[v]=dis[u]+edge[i].cost;
tarjan(v);
fa[v]=u;
}
}
for(int i=qhead[u];i!=-;i=qe[i].next){
int v=qe[i].to;
if(vis[v]){
qe[i].cost=dis[u]+dis[v]-*dis[find(v)];
qe[i^].cost=qe[i].cost;
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)==){
init();
int a,b,c;
char s[];
for(int i=;i<m;i++){
scanf("%d%d%d%s",&a,&b,&c,s);
addEdge(a,b,c);
addEdge(b,a,c);
}
int q;
scanf("%d",&q);
while(q--){
scanf("%d%d",&a,&b);
addQedge(a,b);
addQedge(b,a);
}
tarjan();
for(int i=;i<qsize;i+=){
printf("%d\n",qe[i].cost);
}
}
return ;
}
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 200000
#define inf 1e12
vector<pair<int,int> >edge[N];
vector<pair<int,int> >que[N];
int n,m,q;
int ans[N];
int dis[N];
int fa[N];
int vis[N];
int sum;
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
void LCA(int u,int p){
fa[u]=u;
for(int i=;i<edge[u].size();i++){
int v=edge[u][i].first;
if(v==p) continue;
dis[v]=dis[u]+edge[u][i].second;
LCA(v,u);
fa[v]=u;
}
vis[u]=;
if(sum==q) return;
for(int i=;i<que[u].size();i++){
int v=que[u][i].first;
if(vis[v]){
ans[que[u][i].second]=dis[u]+dis[v]-*dis[find(v)];
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)==){ for(int i=;i<N;i++){
edge[i].clear();
que[i].clear();
}
sum=;
memset(vis,,sizeof(vis)); char s[];
for(int i=;i<m;i++){
int a,b,c;
scanf("%d%d%d%s",&a,&b,&c,s);
edge[a].push_back(make_pair(b,c));
edge[b].push_back(make_pair(a,c));
} scanf("%d",&q);
for(int i=;i<q;i++){
int x,y;
scanf("%d%d",&x,&y);
que[x].push_back(make_pair(y,i));
que[y].push_back(make_pair(x,i));
ans[i]=;
}
dis[]=;
LCA(,); for(int i=;i<q;i++){
printf("%d\n",ans[i]);
}
}
return ;
}