hdu 2586 LCA tarjan 模板

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.
InputFirst 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.OutputFor 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

题意: 求两个点的路程。  

思路: 两个点到根的路程-2*最近公共祖先到根的路程。

代码 : 

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>

using namespace std;
const int N =40005;
const int Q =205;

struct node
{
    int w;
    int v;
    int next;
}edge[N*2];
struct node1
{
    int v;
    int index;
    int next;
}query[Q*2];

int n,q;
int ans[Q*2];
int tt;
int tot;
int head[N];
int he[N];
int f[N];
int ancestor[N];
int vis[N];
int dis[N];
pair<int ,int >p[Q];

void init()
{
    tot=tt=0;
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
    memset(dis,0,sizeof(dis));
    memset(he,-1,sizeof(he));
    memset(ans,0,sizeof(ans));
    memset(ancestor,-1,sizeof(ancestor));
    memset(f,-1,sizeof(f));
    //for(int i=0;i<=n;i++) f[i]=i;
}

void add(int u,int v,int w)
{
    edge[++tot].v=v;  edge[tot].w=w;  edge[tot].next=head[u]; head[u]=tot;
}

void add_q(int u,int v,int index)
{
    query[++tt].v=v; query[tt].index=index; query[tt].next=he[u]; he[u]=tt;
    query[++tt].v=u; query[tt].index=index; query[tt].next=he[v]; he[v]=tt;
}

int getf(int x)
{
    if(f[x]==-1) return x;
    return f[x]=getf(f[x]);
    //return f[x]==x?x:(f[x]=getf(f[x]));
}

void merge(int x,int y)
{
    int t1=getf(x);
    int t2=getf(y);
    if(t1!=t2) f[t1]=t2;
}

void LCA(int u,int d)
{
    //printf("u : %d  d : %d\n",u,d);
    ancestor[u]=u;
    vis[u]=1;
    dis[u]=d;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        int w=edge[i].w;
        if(vis[v]) continue;
        LCA(v,d+w);
        merge(u,v);
        ancestor[getf(u)]=u;
    }

    for(int i=he[u];i!=-1;i=query[i].next)
    {
        int v=query[i].v;
        if(vis[v]){
            ans[query[i].index]=ancestor[getf(v)];
        }
    }
}

int main()
{
    int T;
    int u,v;
    int w;
    cin>>T;
    while(T--)
    {
        cin>>n>>q;
        init();
        for(int i=1;i<n;i++){
            cin>>u>>v>>w;
            add(u,v,w);
            add(v,u,w);
        }

        for(int i=1;i<=q;i++){
            cin>>p[i].first>>p[i].second;
            add_q(p[i].first,p[i].second,i);
        }

        LCA(1,0);
        //for(int i=1;i<=n;i++) cout<<dis[i]<<" ";
        //cout<<endl;

        int d;
        for(int i=1;i<=q;i++)
        {
            d=dis[p[i].first]+dis[p[i].second]-dis[ans[i]]*2;
            cout<<d<<endl;
        }

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yjt9299/article/details/80358175
今日推荐