How far away ?

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

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<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 41000
using namespace std;
int t,n,m,x,y,z,ans,tot;
int fa[N],top[N],size[N],deep[N],head[N],dis[N];
struct Edge
{
    int to,dis,from,next;
}edge[N<<];
int add(int x,int y,int z)
{
    tot++;
    edge[tot].to=y;
    edge[tot].dis=z;
    edge[tot].next=head[x];
    head[x]=tot;
}
int read()
{
    ,f=; char ch=getchar();
    ; ch=getchar();}
    +ch-'; ch=getchar();}
    return x*f;
}
int begin()
{
    ans=;tot=;
    memset(fa,,sizeof(fa));
    memset(top,,sizeof(top));
    memset(dis,,sizeof(dis));
    memset(edge,,sizeof(edge));
    memset(deep,,sizeof(deep));
    memset(size,,sizeof(size));
    memset(head,,sizeof(head));
}
int dfs(int x)
{
    size[x]=;
    deep[x]=deep[fa[x]]+;
    for(int i=head[x];i;i=edge[i].next)
    {
        int to=edge[i].to;
        if(fa[x]==to) continue;
        dis[to]=dis[x]+edge[i].dis;
        fa[to]=x,dfs(to);
        size[x]+=size[to];
    }
}
int dfs1(int x)
{
    ;
    if(!top[x]) top[x]=x;
    for(int i=head[x];i;i=edge[i].next)
    {
        int to=edge[i].to;
        if(fa[x]!=to&&size[t]<size[to]) t=to;
    }
    if(t) top[t]=top[x],dfs1(t);
    for(int i=head[x];i;i=edge[i].next)
    {
        int to=edge[i].to;
        if(fa[x]!=to&&t!=to)
         dfs1(to);
    }
}
int lca(int x,int y)
{
    for(;top[x]!=top[y];x=fa[top[x]])
      if(deep[top[x]]<deep[top[y]]) swap(x,y);
    if(deep[x]>deep[y]) swap(x,y);
    return x;
}
int main()
{
    t=read();
    while(t--)
    {
        n=read();m=read();begin();
        ;i<n;i++)
        {
            x=read(),y=read(),z=read();
            add(x,y,z),add(y,x,z);
        }
        dfs(),dfs1();
        ;i<=m;i++)
        {
            x=read(),y=read();
            ans=dis[x]+dis[y]-*dis[lca(x,y)];
            printf("%d\n",ans);
        }
    }
    ;
}

hdu——2586 How far away ?的更多相关文章

  1. HDU - 2586 How far away ?(LCA模板题)

    HDU - 2586 How far away ? Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & ...

  2. hdu 2586 How far away ?倍增LCA

    hdu 2586 How far away ?倍增LCA 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路: 针对询问次数多的时候,采取倍增 ...

  3. HDU 2586 How far away ?【LCA】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2586 How far away ? Time Limit: 2000/1000 MS (Java/Oth ...

  4. HDU 2586.How far away ?-离线LCA(Tarjan)

    2586.How far away ? 这个题以前写过在线LCA(ST)的,HDU2586.How far away ?-在线LCA(ST) 现在贴一个离线Tarjan版的 代码: //A-HDU25 ...

  5. HDU 2586 How far away ? (LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 LCA模版题. RMQ+LCA: #include <iostream> #incl ...

  6. hdu - 2586 How far away ?(最短路共同祖先问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 最近公共祖先问题~~LAC离散算法 题目大意:一个村子里有n个房子,这n个房子用n-1条路连接起 ...

  7. HDU 2586 How far away ?(LCA在线算法实现)

    http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵树,求出树上任意两点之间的距离. 思路: 这道题可以利用LCA来做,记录好每个点距离根结点的 ...

  8. HDU 2586 How far away ?(LCA模板 近期公共祖先啊)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the vi ...

  9. HDU 2586 How far away ?【LCA模板题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给你N个点,M次询问.1~N-1行输入点与点之间的权值,之后M行输入两个点(a,b)之间的最 ...

  10. hdu 2586 How far away ?(LCA - Tarjan算法 离线 模板题)

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. AJPFX总结关于Java中过滤出字母、数字和中文的正则表达式

    1.Java中过滤出字母.数字和中文的正则表达式 (1)过滤出字母的正则表达式      [^(A-Za-z)] (2) 过滤出 数字 的正则表达式   [^(0-9)] (3) 过滤出 中文 的正则 ...

  2. Django model 反向引用中的related_name

    转自:https://blog.csdn.net/lanyang123456/article/details/68962515 问题: 定义表Apple: class Apple( models.Mo ...

  3. scala打印error,debug,info

    1.以wordcount为例 package org.apache.spark.examples import org.apache.spark.examples.SparkPi.logger imp ...

  4. postgresql update from

    1,update   from   关联表的更新 update table a set name=b.name from table B b  where a.id=b.id; update test ...

  5. C++学习_继承覆盖重载

    今天通过对实验二继承,重载,覆盖的学习,让我更深一步理解了这些概念的区别. 首先来明确一个概念,函数名即地址,也就是说函数名就是个指针. 编译阶段,编译器为每个函数的代码分配一个地址空间并编译函数代码 ...

  6. leetcode_1015. Numbers With Repeated Digits

    https://leetcode.com/problems/numbers-with-repeated-digits/ 与leetcode_357. Count Numbers with Unique ...

  7. 6-Java-C(小题答案)

    1.15 2.36 3.0.58198 4.return v.size()-v.indexOf(n) 5."%"+(width-s.length()-2)/2+"s%s% ...

  8. HTML5小時鐘

    <!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/T ...

  9. JavaSE-05 数组

    学习要点 数组的基本用法 数组的典型应用 数组相关概念 问题 Java考试结束后,老师给小强分配了一项任务,让他计算全班(30人)的平均分,按照目前的知识结构,如何实现? 问题分析 数组 定义:数组是 ...

  10. java中mkdir()和mkdirs()区别

    mkdirs()可以建立多级文件夹 mkdir()只会建立一级的文件夹 例如: new File("/file/one/two").mkdirs(); 可建立/file/one/t ...