码迷,mamicode.com
首页 > 其他好文 > 详细

(tarjan LCA) hdu 2586

时间:2015-03-27 23:34:31      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6727    Accepted Submission(s): 2497


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.
 

 

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
 

 

Source
 

 

 

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <vector>

using namespace std;
using namespace std;
const int maxn=50010;
vector<int> v[maxn],w[maxn],query[maxn],num[maxn];
int tt,n,m,dist[maxn],ans[maxn],fa[maxn];
bool vis[maxn];
void init()
{
    for(int i=1;i<=n;i++)
    {
        fa[i]=i;
        v[i].clear();
        w[i].clear();
        query[i].clear();
        num[i].clear();
        dist[i]=0;
        vis[i]=0;
        ans[i]=0;
    }
}
int find(int x)
{
    if(x!=fa[x])
        fa[x]=find(fa[x]);
    return fa[x];
}
void Union(int x,int y)
{
    int fx=find(x);
    int fy=find(y);
    if(fx!=fy)
        fa[fy]=fx;
}
void tarjan(int cur,int val)
{
    vis[cur]=1;
    dist[cur]=val;
    for(int i=0;i<v[cur].size();i++)
    {
        int temp=v[cur][i];
        if(vis[temp]) continue;
        tarjan(temp,val+w[cur][i]);
        Union(cur,temp);
    }
    for(int i=0;i<query[cur].size();i++)
    {
        int temp=query[cur][i];
        if(!vis[temp]) continue;
        ans[num[cur][i]]=dist[cur]+dist[temp]-2*dist[find(temp)];
    }
}
int main()
{
    int x,y,z;
    scanf("%d",&tt);
    while(tt--)
    {
        scanf("%d%d",&n,&m);
        init();
        for(int i=0;i<n-1;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            v[x].push_back(y);
            v[y].push_back(x);
            w[x].push_back(z);
            w[y].push_back(z);
        }
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            query[x].push_back(y);
            query[y].push_back(x);
            num[x].push_back(i);
            num[y].push_back(i);
        }
        tarjan(1,0);
        for(int i=0;i<m;i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}

  

(tarjan LCA) hdu 2586

标签:

原文地址:http://www.cnblogs.com/a972290869/p/4372951.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!