hdu2586(最近公共祖先)LCA+RMQ

How far away ?

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


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
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   3486  2874  2888  3234  2818 
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<map>
#include<vector>
#include<stack>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
#define N 41003
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
typedef long long ll;

/*给出一个无根树,求u和v的距离;
先预处理每个结点到根节点的距离(1为根节点)dis
对于每次询问,找出u和v的最近公共祖先fa,
即为dis[u]+dis[v]-2*dis[fa];*/

struct E//存图
{
    int v,ne,d;//原点、临接点、两点距离
    E() {}
    E(int v,int n,int d):v(v),ne(n),d(d) {}
} e[N*2];

int size,head[N],dis[N],dep[N*2],pos[N],t[N*2];
//size为边数,head为
//pos为时间戳,t为当前时间对应的节点,dep为时间对应节点深度
int dp[N*2][25],tot;
bool vis[N];

void init()
{
    size=tot=0;
    memset(vis,false,sizeof(vis));
    memset(dis,0,sizeof(dis));
    memset(head,-1,sizeof(head));
}

void add(int u,int v,int d)
{
    e[size]=E(v,head[u],d);
    head[u]=size++;
}

void dfs(int u,int de)//de为深度,tot为时间
{
    int v,d;
    if(!vis[u])
    {
        vis[u]=true;
        pos[u]=tot;
    }
    dep[tot]=de,t[tot++]=u;
    for(int i=head[u]; i!=-1; i=e[i].ne)
    {
        v=e[i].v,d=e[i].d;
        if(vis[v]) continue;
        dis[v]=dis[u]+d;
        printf("从%d搜索到%d\n",u,v);
        dfs(v,de+1);
        dep[tot]=de,t[tot++]=u;
    }
}

int RMQ(int u,int v)
{
    int len=v-u+1,k=0;
    k=log(len*1.0)/log(2.0);
    //返回值为深度最小的点,也就是u和v的最近公共祖先
    return dep[dp[u][k]]<dep[dp[v-(1<<k)+1][k]]?t[dp[u][k]]:t[dp[v-(1<<k)+1][k]];
}

int LCA(int u,int v)
{
    if(pos[u]>pos[v]) swap(v,u);//按时间戳排
    return RMQ(pos[u],pos[v]);
}
int main()
{
    int t,n,i,j,m,u,v,d;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        init();
        for(i=1; i<n; i++)
        {
            scanf("%d%d%d",&u,&v,&d);
            add(u,v,d),add(v,u,d);
        }
        for(int i=0;i<4;i++)
        {
            cout<<"head  "<<head[i]<<endl;
            //cout<<e[i].v<<" "<<e[i].ne<<" "<<e[i].d<<endl;
        }

        dfs(1,0);
        for(j=0; (1<<j)<tot; j++)
            for(i=1; i+(1<<j)<tot; i++)
            {
                if(j)//dp为区间最小值,也是深度最小
                {
                    dp[i][j]=dep[dp[i][j-1]]<dep[dp[i+(1<<j-1)][j-1]]?dp[i][j-1]:dp[i+(1<<j-1)][j-1];
                }
                else//初始值为该点
                    dp[i][j]=i;
            }
            while(m--)
            {
                scanf("%d%d",&u,&v);
                int fa=LCA(u,v);
                printf("%d\n",dis[u]+dis[v]-2*dis[fa]);
            }
    }
    return 0;
}















猜你喜欢

转载自blog.csdn.net/kyrie_10/article/details/79250103