[HDU]P2586

How far away ?

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

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

ECJTU
2009 Spring Contest

Recommend

lcy   |   We
have carefully selected several similar problems for you:  3486 2874 2888 3234 2818


这道题就是很裸的LCA,主要是练一下倍增,今天考试一道有关LCA的,我用树剖打竟然T了?(感觉效率有保证,不知道是不是数据问题)

可恶啊,打了很久诶,于是就来学习一下倍增。

代码:

 //2017.11.7
 //lca
 #include<iostream>
 #include<cstdio>
 #include<cstring>
 using namespace std;
 inline int read();
 namespace lys{
      ;
     struct edge{
         int to;
         int next;
         int w;
     }e[N*];
     ],dis[N][],dep[N],pre[N];
     int n,m,cnt;
     void swap(int &a,int &b){int t=a;a=b;b=t;}
     void add(int x,int y,int w){
         e[++cnt].to=y;e[cnt].next=pre[x];pre[x]=cnt;e[cnt].w=w;
         e[++cnt].to=x;e[cnt].next=pre[y];pre[y]=cnt;e[cnt].w=w;
     }
     void dfs(int node,int deep){
         dep[node]=deep;
         int i,v;
         ;i<=;i++) anc[node][i]=anc[anc[node][i-]][i-],dis[node][i]=dis[node][i-]+dis[anc[node][i-]][i-];
         for(i=pre[node];i;i=e[i].next){
             v=e[i].to;
             ]) continue ;
             anc[v][]=node;
             dis[v][]=e[i].w;
             dfs(v,deep+);
         }
     }
     int lca(int x,int y){
         ,i;
         if(dep[x]<dep[y]) swap(x,y);
         ;i>=;i--)
             if(dep[y]<=dep[anc[x][i]]) res+=dis[x][i],x=anc[x][i];
         if(x==y) return res;
         ;i>=;i--)
             if(anc[x][i]!=anc[y][i]) res+=dis[x][i]+dis[y][i],x=anc[x][i],y=anc[y][i];
         ]+dis[y][];
     }
     int main(){
         memset(pre,,sizeof pre);
         int i,u,v,w;
         n=read(); m=read();
         cnt=;
         ;i<n;i++){
             u=read(); v=read(); w=read();
             add(u,v,w);
         }
         dfs(,);
         while(m--){
             u=read(); v=read();
             printf("%d\n",lca(u,v));
         }
         ;
     }
 }
 int main(){
     int T=read();
     while(T--) lys::main();
     ;
 }
 inline int read(){
     ,ff=;
     char c=getchar();
     '){
         ;
         c=getchar();
     }
     +c-',c=getchar();
     return kk*ff;
 }

[HDU]P2586 How far away?[LCA]的更多相关文章

  1. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  2. hdu 6203 ping ping ping(LCA+树状数组)

    hdu 6203 ping ping ping(LCA+树状数组) 题意:给一棵树,有m条路径,问至少删除多少个点使得这些路径都不连通 \(1 <= n <= 1e4\) \(1 < ...

  3. HDU 3078:Network(LCA之tarjan)

    http://acm.hdu.edu.cn/showproblem.php?pid=3078 题意:给出n个点n-1条边m个询问,每个点有个权值,询问中有k,u,v,当k = 0的情况是将u的权值修改 ...

  4. HDU 2874 Connections between cities (LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 题意是给你n个点,m条边(无向),q个询问.接下来m行,每行两个点一个边权,而且这个图不能有环路 ...

  5. 【HDU 4547 CD操作】LCA问题 Tarjan算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4547 题意:模拟DOS下的cd命令,给出n个节点的目录树以及m次查询,每个查询包含一个当前目录cur和 ...

  6. HDU 5452 Minimum Cut(LCA)

    http://acm.hdu.edu.cn/showproblem.php?pid=5452 题意: 有一个连通的图G,先给出图中的一棵生成树,然后接着给出图中剩余的边,现在要删除最少的边使得G不连通 ...

  7. HDU 2460 Network(桥+LCA)

    http://acm.hdu.edu.cn/showproblem.php?pid=2460 题意:给出图,求每次增加一条边后图中桥的数量. 思路: 先用tarjan算法找出图中所有的桥,如果lowv ...

  8. HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

    题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点 ...

  9. hdu 2586(最近公共祖先LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路:在求解最近公共祖先的问题上,用到的是Tarjan的思想,从根结点开始形成一棵深搜树,非常好 ...

随机推荐

  1. 阿里云 Centos7 部署 Django 项目

    前期准备 阿里云服务器 mysql数据库 已经本地运行成功的项目 阿里云服务器的环境配置 Git #代码管理 Gitlab #代码托管,要求服务器内存不低于2G,我选择放弃 Mysql #连接数据库 ...

  2. webservice的一些理解

    web services中如果用.NET的话,DataSet可以作为与客户端交互的一个返回值,因为DataSet实质上是XML.而SOAP WSDL等都是基于XML的. --------------- ...

  3. mysql: show full processlist 详解

    show full processlist 是显示用户正在运行的线程,需要注意的是,除了 root 用户能看到所有正在运行的线程外,其他用户都只能看到自己正在运行的线程,看不到其它用户正在运行的线程. ...

  4. cdh平台问题

    问题背景:内容的不懂之处,可以私信博主.友好交流使用.主要针对的问题种类有:网络桥接报错.网卡文件问题(该问题主要看你的安装脚本文件里面写的是否和主机对应,也是运行环境的问题).scm表中没有节点信息 ...

  5. Luogu P5444 [APIO2019]奇怪装置

    题目 这种题目看上去就是有循环节的对吧. 在考场上,一个可行的方式是打表. 现在我们手推一下这个循环节. 记函数\(f(t)=(((t+\lfloor\frac tB\rfloor)\%A),(t\% ...

  6. mac键盘在ubuntu下开启fn功能按键

    转载:http://wiki.ubuntu.org.cn/UbuntuHelp:AppleKeyboard Change Function Key behavior This section of t ...

  7. RabbitMq学习1-介绍、安装和配置

    一.简介 1.MQ框架非常之多,比较流行的有RabbitMq.ActiveMq.ZeroMq.kafka,以及阿里开源的RocketMQ       2.AMQP是消息队列的一个协议. 3.Rabbi ...

  8. W10: Warning: Changing a readonly file 解决办法

    在linux上编辑文件的时候,明明是使用的root登录的,可是这种至高无上的权限在按下i的时候被那串红色错误亵渎了W10: Warning: Changing a readonly file. 困扰两 ...

  9. 搜索专题: HDU1372Knight Moves

    Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  10. 用Java构建一个简单的WebSocket聊天室

    前言 首先对于一个简单的聊天室,大家应该都有一定的概念了,这里我们省略用户模块的讲解,而是单纯的先说说聊天室的几个功能:自我对话.好友交流.群聊.离线消息等. 今天我们要做的demo就能帮我们做到这一 ...