hud 2586 How far away ?
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=2586
How far away ?
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
tarjan离线求LCA。。
#include <bits/stdc++.h>
using namespace std;
const int N = 40010;
struct Tarjan_Lca {
bool vis[N];
vector<pair<int, int>> A, Q[N];
int tot, par[N], ans[210], head[N], dist[N];
struct edge { int to, w, next; }G[N << 1];
inline void init(int n) {
A.clear();
for(int i = 0; i < n + 2; i++) {
par[i] = i;
vis[i] = false;
head[i] = -1;
dist[i] = 0;
Q[i].clear();
}
}
inline void add_edge(int u, int v, int w) {
G[tot] = { v, w, head[u] }, head[u] = tot++;
G[tot] = { u, w, head[v] }, head[v] = tot++;
}
inline void built(int n, int m) {
int u, v, w;
while(n-- > 1) {
scanf("%d %d %d", &u, &v, &w);
add_edge(u, v, w);
}
for(int i = 0; i < m; i++) {
scanf("%d %d", &u, &v);
A.push_back(pair<int, int>(u, v));
Q[u].push_back(pair<int, int>(v, i));
Q[v].push_back(pair<int, int>(u, i));
}
}
inline int find(int x) {
while(x != par[x]) {
x = par[x] = par[par[x]];
}
return x;
}
inline void tarjan(int u, int fa) {
for(int i = head[u]; ~i; i = G[i].next) {
edge &e = G[i];
if(e.to == fa) continue;
dist[e.to] = dist[u] + e.w;
tarjan(e.to, u);
vis[e.to] = true;
par[e.to] = u;
}
for(auto &r: Q[u]) {
if(vis[r.first]) ans[r.second] = find(r.first);
}
}
inline void solve(int n, int m) {
init(n);
built(n, m);
tarjan(1, 1);
for(int i = 0; i < m; i++) {
printf("%d\n", dist[A[i].first] + dist[A[i].second] - 2 * dist[ans[i]]);
}
}
}go;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int t, n, m;
scanf("%d", &t);
while(t--) {
scanf("%d %d", &n, &m);
go.solve(n, m);
}
return 0;
}
hud 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 - Tarjan算法 离线 模板题)
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 2586 How far away ?(LCA在线算法实现)
http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵树,求出树上任意两点之间的距离. 思路: 这道题可以利用LCA来做,记录好每个点距离根结点的 ...
- hdu 2586 How far away ? 带权lca
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) P ...
随机推荐
- 慕课网-安卓工程师初养成-3-6 Java中的逻辑运算符
来源: http://www.imooc.com/code/1301 逻辑运算符主要用于进行逻辑运算.Java 中常用的逻辑运算符如下表所示: 我们可以从“投票选举”的角度理解逻辑运算符: 1. 与: ...
- 在shell中通过fifo与服务器交互
首先,需要说的是:1.在shell中,运行的每一个命令至少启动一个新进程,且:$$:获取当前shell的进程号(PID)$! :执行上一个指令的PID2.重定向与管道有点类似,例子:cmd1 < ...
- 【SSH 1】SSH框架的基本理解
导读:在结束了BS之后,接触到的第一个项目算是网上商城了.这次用到了和之前都不一样的框架:SSH.这个项目就和之前学牛腩一样,有着里程碑的意义.当然了,这也就意味着,什么都是新鲜的,接触到的东西,有时 ...
- MFC六大核心机制之一:MFC程序的初始化
很多做软件开发的人都有一种对事情刨根问底的精神,例如我们一直在用的MFC,很方便,不用学太多原理性的知识就可以做出各种窗口程序,但喜欢钻研的朋友肯定想知道,到底微软帮我们做了些什么,让我们在它的框架下 ...
- 获取AX的窗口所有控件的lableID及内容
思路,穷举Forms\TargetFormName 在AOT上面的路径得到TreeNode, 遍历各控件的属性. a1,先读Label属性,没有就读Caption属性及Text属性. a2,若a1取不 ...
- 简短总结一下C#里跨线程更新UI
摘自: http://my.oschina.net/sdqxcxh/blog/53707 跨线程更新UI是写多线程程序尤其是通信类的程序经常遇到的问题,这里面主要的问题是冲突,比如数据线程想要更新UI ...
- Use BEC to do mobile phone forensics
Belkasoft Evidence Center makes me very impressed that it supports lots of evidence type. I have to ...
- IP配置
1: #vi /etc/sysconfig/network-scripts/ifcfg-eth0 2: 实验环境-网络设置 公司域网: IP=162.168.16.0/24 netmask=255.2 ...
- Android IOS WebRTC 音视频开发总结(三九)-- win10升级为何要p2p
本文主要介绍webrtc p2p的应用场景,文章来自博客园RTC.Blacker,支持原创,转载请说明出处. P2P最简单的解释就是两个客户端之间直接进行数据交互,不经过服务端转发. 最早接触P2P是 ...
- rsync 安装与配置
1.什么是rsync Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的“Rsync算法”来使本地和远 程两个 ...