HDU-2586 How far away?
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 Input2
3 2
1 2 10
3 1 15
1 2
2 3 2 2
1 2 100
1 2
2 1Sample 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?的更多相关文章
- HDU - 2586 How far away ?(LCA模板题)
HDU - 2586 How far away ? Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & ...
- hdu 2586 How far away ?倍增LCA
hdu 2586 How far away ?倍增LCA 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路: 针对询问次数多的时候,采取倍增 ...
- 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 ...
- HDU 2586.How far away ?-离线LCA(Tarjan)
2586.How far away ? 这个题以前写过在线LCA(ST)的,HDU2586.How far away ?-在线LCA(ST) 现在贴一个离线Tarjan版的 代码: //A-HDU25 ...
- HDU 2586 How far away ? (LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 LCA模版题. RMQ+LCA: #include <iostream> #incl ...
- hdu - 2586 How far away ?(最短路共同祖先问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 最近公共祖先问题~~LAC离散算法 题目大意:一个村子里有n个房子,这n个房子用n-1条路连接起 ...
- HDU 2586 How far away ?(LCA在线算法实现)
http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵树,求出树上任意两点之间的距离. 思路: 这道题可以利用LCA来做,记录好每个点距离根结点的 ...
- HDU 2586 How far away ?(LCA模板 近期公共祖先啊)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the vi ...
- HDU 2586 How far away ?【LCA模板题】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给你N个点,M次询问.1~N-1行输入点与点之间的权值,之后M行输入两个点(a,b)之间的最 ...
- 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 ...
随机推荐
- 拼多多sku详情的获取分析以及应用
一.拼多多sku详情获取方式 要获取拼多多SKU详情,需要使用拼多多的API接口,以下是获取拼多多SKU详情的步骤: 1.注册拼多多开放平台账户并创建应用 拼多多创建开发者账户并创建应用,获得应用ID ...
- PowerShell收集信息及绕过PowerShell权限
PowerShell脚本的4种执行权限: Restricted:默认设置,不允许任何script运行 AllSigned:只能运行经过数字证书签名的script RemoteSigned:本地脚本不做 ...
- @Validated指定校验顺序
在Java中,使用@NotNull注解时,可以指定多个参数的顺序.为了指定顺序,你可以使用@GroupSequence注解. 首先,为每个需要校验的参数定义一个接口,并在接口上添加@GroupSequ ...
- 在同一个k8s集群中部署多套nginx-controller
1.nginx-controller部署请参考我的另一篇博客 nginx Ingress Controller Packaged by Bitnami 2.修改values.yaml 不通contro ...
- 宏观上理解blazor中的表单验证
概述 表单验证的最终效果大家都懂,这里不阐述了,主要从宏观角度说说blazor中表单验证框架涉及到的类,以及它们是如何协作的,看完这个,再看官方文档也许能更轻松点. blazor中的验证框架分为两部分 ...
- linux文件、目录权限和所有者
文件.目录权限和所有者 简介:用户对一个文件或目录具有访问权限,这些访问权限决定了谁能访问,以及如何范围这些文件和目录.通过设置权限可以限制或允许以下三种用户访问: 文件的用户所有者(属主) 文件的组 ...
- Maximum Diameter 题解
Maximum Diameter 题目大意 定义长度为 \(n\) 的序列 \(a\) 的权值为: 所有的 \(n\) 个点的第 \(i\) 个点的度数为 \(a_i\) 的树的直径最大值,如果不存在 ...
- Java初始化顺序及使用Spring情况下初始化
Java初始化顺序 1 无继承情况下的Java初始化顺序: class Sample { Sample(String s) { System.out. ...
- 一张图搞懂sql执行顺序
冲浪时发现一张很有意思的图,细分了一个长sql语句的执行顺序
- 四个id 生成器性能比较记录
IdGenerator Seata 优化的雪花算法 Seata基于改良版雪花算法的分布式UUID生成器分析 关于新版雪花算法的答疑 csharp 移植代码 public class IdGenerat ...