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);
        }
    }
    ;
}

最新文章

  1. 腾讯WEB前端开发面试经历,一面二面HR面,面面不到!
  2. 模板方法模式(Template Method)
  3. Spring IoC源码解读——谈谈bean的几种状态
  4. 转: Oracle Form 中commit 与do_key(&#39;commit_form&#39;)区别
  5. oracle中的数值函数整理
  6. devstack安装openstack
  7. UITextField,常见属性的罗列和用法
  8. Css3渐变实例Demo(一)
  9. Python标准库:内置函数format(value[, format_spec])
  10. IIS 7.5配置PHP站点
  11. 【原创】相对完整的一套以Jmeter作为工具的性能测试教程(接口性能测试,数据库性能测试以及服务器端性能监测)
  12. 【记录】使用在线KMS激活Office系列
  13. Java-Oracle数据库连接
  14. 使用scrapy爬取海外网学习频道
  15. 09-java学习-数组-冒泡排序-选择排序-数组工具类编写-查找-扩容
  16. genetic model
  17. vlookup+match高亮显示行
  18. MVC扩展Filter, 通过继承AuthorizationAttribute限制IP
  19. Oracle 存储过程错误之PLS-00201: 必须声明标识符
  20. 关于CocoaPods添加第三方库造成项目崩溃

热门文章

  1. 移动设备访问使用百度js跳转
  2. React全家桶之一 react-router之高级
  3. 【C++】模板简述(四):模板为什么不支持分离编译?
  4. [转] NTFS Permission issue with TAKEOWN &amp; ICACLS
  5. table鼠标滑过变颜色
  6. HDU_3792_(素数筛+树状数组)
  7. 解决【npm ERR! Unexpected end of JSON input while parsing near &#39;...sh_time&quot;:141072930277&#39;】方案
  8. pycharm connect to mysql
  9. tcp案例之文件下载器
  10. Insert 语句对 nologging 与 logging表 在不同场景下的优化