hdu 2874 Connections between cities(最近公共祖先(LCA))

来源:互联网 发布:linq实战源码 编辑:程序博客网 时间:2024/05/21 17:15

Problem Description
After World War X, a lot of cities have been seriously damaged, and we need to rebuild those cities. However, some materials needed can only be produced in certain places. So we need to transport these materials from city to city. For most of roads had been totally destroyed during the war, there might be no path between two cities, no circle exists as well.
Now, your task comes. After giving you the condition of the roads, we want to know if there exists a path between any two cities. If the answer is yes, output the shortest path between them.
 

Input
Input consists of multiple problem instances.For each instance, first line contains three integers n, m and c, 2<=n<=10000, 0<=m<10000, 1<=c<=1000000. n represents the number of cities numbered from 1 to n. Following m lines, each line has three integers i, j and k, represent a road between city i and city j, with length k. Last c lines, two integers i, j each line, indicates a query of city i and city j.
 

Output
For each problem instance, one line for each query. If no path between two cities, output “Not connected”, otherwise output the length of the shortest path between them.
 

Sample Input
5 3 21 3 22 4 35 2 31 44 5
 

Sample Output
Not connected6

题意:给定有N个结点的森林和Q次询问,求任意给定的两个结点之间的距离。 N<=100000 , Q<=1000000

算法:LCA离线算法。所不同的是,这题并不是给你一颗树,而是一个森林,因此还需要用并查集将一颗树上的结点放到同一个集合中去,然后对每棵树用Tarjin算法求一次LCA


#include <iostream>#include <string.h>#include <stdio.h>#include <vector>using namespace std;const int MAX=10005;struct node{    int v;    int w;    node(int V=0,int W=0):v(V),w(W)    {}};struct ques{    int v,num;    ques(int V=0,int Num=0):v(V),num(Num)    {}};vector<node>a[MAX];vector<ques>q[MAX];int n,m,fa[MAX],dist[MAX],cnt[MAX*100],par[MAX];bool vis[MAX];void Init(){    for (int i=0; i<=n; i++)    {        a[i].clear();        q[i].clear();        fa[i]=i;        par[i]=i;        vis[i]=false;        dist[i]=0;    }}int Find(int x,int *f){    if(x!=f[x])    {        return f[x]=Find(f[x],f);    }    return f[x];}void tarjan(int u){    vis[u]=true;    for (int i=0; i<q[u].size(); i++)    {        int v=q[u][i].v;        if(vis[v])        {            if(Find(u,par)==Find(v,par))            {    //属于同一个集合                cnt[q[u][i].num]=dist[u]+dist[v]-2*dist[Find(v, fa)]; //该编号代表的顶点与最近公共祖先的距离            }            else            {                cnt[q[u][i].num]=-1;            }        }    }    for (int i=0; i<a[u].size(); i++)    {        int v=a[u][i].v;        if(!vis[v])        {            dist[v]=dist[u]+a[u][i].w;            tarjan(v);            fa[v]=u;        }    }}void Union(int x,int y)  //合并集合{    x=Find(x,par);    y=Find(y,par);    if(x!=y)    {        par[x]=y;    }}int main(){    int t;    while (scanf("%d%d%d",&n,&t,&m)!=EOF)    {        Init();        int u,v,w;        while (t--)        {            scanf("%d%d%d",&u,&v,&w);            Union(u,v);            a[u].push_back(node(v,w));            a[v].push_back(node(u,w));        }        for (int i=0; i<m; i++)        {            scanf("%d%d",&u,&v);            q[u].push_back(ques(v,i));            q[v].push_back(ques(u,i));        }        for (int i=1; i<=n; i++)        {            if(par[i]==i)  //找到各个集合的根            {                tarjan(i);            }        }        for (int i=0; i<m; i++)        {            if(cnt[i]==-1)            {                printf("Not connected\n");            }            else            {                printf("%d\n",cnt[i]);            }        }    }    return 0;}



0 0
原创粉丝点击