hdu 2586 (LCA)

最近公共祖先。。 表示卡在一个位置卡了很久。。思维还是差了些,在一颗树上记录距离只需记录到根结点的距离,如果要求两个点的距离(一个点时另一个点的祖先)只需将他们到根结点的距离相减就行了.

唉,其他的就是普通的LCA (离线)。

等下去学有限的LCA

How far away ?

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


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
 
 
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
#define N 40040
int n,m;

struct node
{
    int to;
    int next;
    int w;
}edge[N*100];

struct node1
{
    int id,from,to,next;
}edge1[2200];

int cnt,pre[N];
int cnt1,pre1[N];
int mark[N];
int bin[N],bw[N]; // 用bw 来记录
int save[N];

void add_edge(int u,int v,int w)
{
    edge[cnt].to=v;
    edge[cnt].w=w;
    edge[cnt].next=pre[u];
    pre[u]=cnt++;
}
void add_edge1(int u,int v,int id)
{
    edge1[cnt1].id=id;
    edge1[cnt1].from=u;
    edge1[cnt1].to=v;
    edge1[cnt1].next=pre1[u];
    pre1[u]=cnt1++;
}

int find(int v)
{
    if(bin[v]==v) return v;
    return bin[v]=find(bin[v]);
}

void dfs(int s,int w)
{
    mark[s]=1;
    bw[s]=w;
    for(int p=pre[s];p!=-1;p=edge[p].next)
    {
        int v=edge[p].to;
        if(mark[v]==0)
        {
            dfs(v,w+edge[p].w);
            bin[v]=s;
        }
    }
    for(int p=pre1[s];p!=-1;p=edge1[p].next)
    {
        int v=edge1[p].to;
        if(mark[v]==1) // 如果另一点已经查找过
        {
            if(save[edge1[p].id]==-1)
            {
                int x=find(v);
                save[edge1[p].id] = bw[v]-bw[x]+bw[s]-bw[x];
            }
        }
    }

}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {    
        cnt=0;
        memset(pre,-1,sizeof(pre));
        cnt1=0;
        memset(pre1,-1,sizeof(pre1));
        scanf("%d%d",&n,&m);
        for(int i=1;i<n;i++)
        {
            int x,y,key;
            scanf("%d%d%d",&x,&y,&key);
            add_edge(x,y,key);
            add_edge(y,x,key);
        }
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            add_edge1(x,y,i);
            add_edge1(y,x,i);
        }
        memset(save,-1,sizeof(save));
        memset(mark,0,sizeof(mark));
        for(int i=0;i<N;i++)
            bin[i]=i;
        memset(bw,0,sizeof(bw)); // 记录每个点到父亲结点的距离
        dfs(1,0);
        for(int i=0;i<m;i++)
        {
            printf("%d\n",save[i]);
        }
    }
    return 0;
}

 

posted @ 2013-03-15 14:50  chenhuan001  阅读(259)  评论(0编辑  收藏  举报