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.

InputFirst 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.OutputFor 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

 1 #include<iostream>
2 #include<stdio.h>
3 #include<vector>
4 #include<algorithm>
5 #include<string.h>
6 using namespace std;
7 int ans[250],vis[40500];
8 vector< pair <int ,int > > vec[40500];
9 vector <pair <int ,int > > query[40500];
10 struct Node{
11 int f;
12 int len;
13 } pre[40500];
14 Node Find(int x)
15 {
16 if(pre[x].f==x)
17 {
18 return pre[x];
19 }
20 else
21 {
22 Node now;
23 now=Find(pre[x].f);
24 pre[x].len+=now.len;
25 pre[x].f=now.f;
26 return pre[x];
27 }
28 }
29 int dfs(int u,int fa,int length)
30 {
31 pre[u].f=u;
32 pre[u].len=0;
33 vis[u]=1;
34 for(int i=0;i<vec[u].size();i++)
35 {
36 int v=vec[u][i].first;
37 int len=vec[u][i].second;
38 if(v==fa) continue;
39 dfs(v,u,len);
40 }
41 for(int i=0;i<query[u].size();i++)
42 {
43 int v=query[u][i].first;
44 int id=query[u][i].second;
45 if(vis[v]==1)
46 {
47 ans[id]=Find(v).len+Find(u).len;
48 }
49 }
50 pre[u].f=fa;
51 pre[u].len+=length;
52 }
53 int main()
54 {
55 int T;
56 cin>>T;
57 while(T--)
58 {
59 int n,q,x,y,k;
60 cin>>n>>q;
61 for(int i=0;i<n-1;i++)
62 {
63 cin>>x>>y>>k;
64 vec[x].push_back({y,k});
65 vec[y].push_back({x,k});
66 vis[y]=1;
67 }
68 for(int i=0;i<q;i++)
69 {
70 cin>>x>>y;
71 query[x].push_back({y,i});
72 query[y].push_back({x,i});
73 }
74 for(int i=1;i<=n;i++)
75 {
76 if(vis[i]==0)
77 {
78 dfs(i,i,0);
79 break;
80 }
81 }
82 for(int i=0;i<q;i++)
83 cout<<ans[i]<<endl;
84 for(int i=0;i<=n;i++)
85 {
86 vec[i].clear();
87 query[i].clear();
88 }
89 memset(pre,0,sizeof(pre));
90 memset(vis,0,sizeof(vis));
91 }
92 return 0;
93 }



												

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. Conda 命令深入指南

    Conda 命令深入指南 Conda 是一个功能强大的包管理系统,允许您为不同的项目创建和管理隔离的环境,从而更轻松地处理不同的依赖项集. 安装 可以按照 Conda 官方网站 (https://co ...

  2. 《Python魔法大冒险》008 石像怪的挑战:运算符之旅

    小鱼和魔法师继续深入魔法森林.不久,他们来到了一个巨大的魔法石圈旁边.石圈中心有一个闪闪发光的魔法水晶,周围则是一些神秘的符号.但令人意外的是,水晶的旁边还有一个巨大的石像怪,它的眼睛散发着红色的光芒 ...

  3. 如何实现一个数据库的 UDF?图数据库 NebulaGraph UDF 功能背后的设计与思考

    大家好,我是来自 BOSS直聘的赵俊南,主要负责安全方面的图存储相关工作.作为一个从 v1.x 用到 v3.x 版本的忠实用户,在见证 NebulaGraph 发展的同时,也和它一起成长. BOSS直 ...

  4. MutationObserver监听dom元素结构及属性变化

    工作中埋码需求,当某些动态插入的元素出现时触发埋码事件,因此需要对插入元素的父节点进行监听,子节点发生变化时触发相应埋码逻辑. 方法一 监听页面结构及子元素变化: (function () { //事 ...

  5. Blazor Server 发起HttpPost请求,但是多参数

    一.介绍 今天突然想起之前工作上遇到的一个问题,在做Blazor 开发时后端给的一个接口请求方式是Post ,但是他需要携带多个参数,新建一个公共类又觉得麻烦,我就尝试着怎么在Post请求中携带多个参 ...

  6. 5. 用Rust手把手编写一个Proxy(代理), 通讯协议建立, 为内网穿透做准备

    用Rust手把手编写一个Proxy(代理), 通讯协议建立, 为内网穿透做准备 项目 ++wmproxy++ gite: https://gitee.com/tickbh/wmproxy github ...

  7. 19c上ADG主库sys密码修改会影响备库同步吗?

    一套Oracle 19c的ADG集群要修改sys密码,由于之前遇见过11g上sys密码修改导致同步问题的情况,所以改之前特意查了下文档,发现其实12cR2开始,在主库修改密码就会自动同步到备库了,以下 ...

  8. 探索Redis与MySQL的双写问题

    本文已收录至GitHub,推荐阅读 Java随想录 微信公众号:Java随想录 原创不易,注重版权.转载请注明原作者和原文链接 目录 双写一致问题 缓存读写策略 Cache-Aside Pattern ...

  9. 一个Node.js的小爬虫

    爬虫其实就是对网页内特定id.class.标签内容的提取,多是循环出来的,对我们爬取非常便利. 1.安装node node官网下载安装包安装,后在命令行工具中输入node -v查看node安装的版本. ...

  10. 谱图论:Laplacian算子及其谱性质

    1 Laplacian 算子 给定无向图\(G=(V, E)\),我们在上一篇博客<谱图论:Laplacian二次型和Markov转移算子>中介绍了其对应的Laplacian二次型: \[ ...