hdu 2586 How far away ?

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
终于知道Lca还有那么一个需要注意的地方,上一篇不知道为什么没有栈溢出,这个溢出了,原来是我的Lca是先算距离,那样会dfs很多次,应该直接Lca,下面两个代码,第一个溢出,第二个AC 31ms
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define  inf 0x0f0f0f0f
using namespace std;

const int maxn=40000+10;
struct node
{
    int c,v;
};

vector<node>tree[maxn],que[maxn];
int num[maxn],dis[maxn],father[maxn];
bool mark[maxn],vis[maxn];

void init(int n)
{
    for (int i=0;i<=n;i++)
    {
        tree[i].clear();
        que[i].clear();
        num[i]=0;
        vis[i]=0;
        mark[i]=0;
        father[i]=i;
        dis[i]=0;
    }
}

int find(int x)
{
    return x==father[x]?x:father[x]=find(father[x]);
}

void Lca(int u)
{
    father[u]=u;
    for (int i=0;i<tree[u].size();i++)
    {
        int v=tree[u][i].v;
        if (!mark[v])
        {
            mark[v]=true;
            dis[v]=dis[u]+tree[u][i].c;
            Lca(v);
            father[v]=u;
        }
    }
    vis[u]=true;
    for (int i=0;i<que[u].size();i++)
    {
        int v=que[u][i].v;
        if (vis[v])
        {
            num[que[u][i].c]=dis[u]+dis[v]-2*dis[find(v)];
        }
    }
}

int main()
{
    int t,n,m,x,y,c;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d%d",&n,&m);
        init(n);
        for (int i=1;i<n;i++)
        {
            scanf("%d%d%d",&x,&y,&c);
            node temp;
            temp.c=c; temp.v=y;
            tree[x].push_back(temp);
            temp.v=x;
            tree[y].push_back(temp);
        }
        for (int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            node temp;
            temp.v=y; temp.c=i;
            que[x].push_back(temp);
            temp.v=x;
            que[y].push_back(temp);
        }
        mark[1]=true;
        Lca(1);
        for (int i=0;i<m;i++)
        {
            printf("%d
",num[i]);
        }
        //printf("
");
    }
    return 0;
}
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define  inf 0x0f0f0f0f
using namespace std;

const int maxn=40000+10;
struct node
{
    int c,v;
};

vector<node>tree[maxn],que[maxn];
int num[maxn],dis[maxn],father[maxn];
bool vis[maxn];

void init(int n)
{
    for (int i=0;i<=n;i++)
    {
        tree[i].clear();
        que[i].clear();
        num[i]=0;
        vis[i]=0;
        father[i]=i;
        dis[i]=0;
    }
}

int find(int x)
{
    return x==father[x]?x:father[x]=find(father[x]);
}

void Lca(int u)
{
    father[u]=u;
    vis[u]=true;
    for (int i=0;i<que[u].size();i++)
    {
        int v=que[u][i].v;
        if (vis[v])
        {
            num[que[u][i].c]=dis[u]+dis[v]-2*dis[find(v)];
        }
    }
    for (int i=0;i<tree[u].size();i++)
    {
        int v=tree[u][i].v;
        if (!vis[v])
        {
            dis[v]=dis[u]+tree[u][i].c;
            Lca(v);
            father[v]=u;
        }
    }
}

int main()
{
    int t,n,m,x,y,c;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d%d",&n,&m);
        init(n);
        for (int i=1;i<n;i++)
        {
            scanf("%d%d%d",&x,&y,&c);
            node temp;
            temp.c=c; temp.v=y;
            tree[x].push_back(temp);
            temp.v=x;
            tree[y].push_back(temp);
        }
        for (int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            node temp;
            temp.v=y; temp.c=i;
            que[x].push_back(temp);
            temp.v=x;
            que[y].push_back(temp);
        }
        Lca(1);
        for (int i=0;i<m;i++)
        {
            printf("%d
",num[i]);
        }
        //printf("
");
    }
    return 0;
}
至少做到我努力了
原文地址:https://www.cnblogs.com/chensunrise/p/3751085.html