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 ...
随机推荐
- 洛谷P1268 树的重量
P1268 树的重量 85通过 141提交 题目提供者该用户不存在 标签树形结构 难度提高+/省选- 提交该题 讨论 题解 记录 最新讨论 有这种情况吗!!!! 题意似乎有问题 题目描述 树可以用来表 ...
- windows在远程桌面连接中使用命令行参数
在此版本的 Windows 中,可以从搜索框("运行"对话框或命令行)启动远程桌面连接,而不是从「开始」菜单启动它. 从"运行"对话框启动远程桌面的步骤 依次单 ...
- KVM: 安装Windows virtio半虚拟化驱动
Install KVM Windows virtio para-virtualized dirver If you can't read Chinese, there's an English ver ...
- WF4 常用类<第二篇>
一.WorkflowInvoker 常用方法如下: 方法 说明 BeginInvoke() 使用指定的 AsyncCallback 和用户提供的状态以异步方式调用工作流 EndInvoke() 返回使 ...
- Elsevier期刊网上投稿指南
一.ELSEVIER期刊介绍:ELSEVIER出版集团创建于1580年,是世界上著名学术期刊出版公司,也是全球最大的科学文献出版发行商.它由Academic Press.Bailliere Tinda ...
- 【C#基础】static 关键字用法小结
静态变量 当我们编写一个类时,其实就是在描述其对象的属性和行为,而并没有产生实质上的对象,只有通过new关键字才会产生出对象,这时系统才会分配内存空间给对象,其方法才可以供外部调用. 有时候,我们希望 ...
- PHP生成静态页面的方法
在PHP网站开发中为了网站推广和SEO等需要,需要对网站进行全站或局部静态化处理,PHP生成静态HTML页面有多种方法,比如利用PHP模板.缓存 等实现页面静态化,今天就以PHP实例教程形 ...
- LINE最新版6.5.0在iOS上的删除信息取证
iOS: 9.3.2版 LINE: 6.5.0版 取出LINE的数据库 Line.sqlite,路径如下所示: 检视删除的信息,发现还有不少残留,虽然都是片段,但拼拼凑凑总还是能有些蛛丝马迹,毕竟,总 ...
- 优秀的富文本编辑器 Kindeditor
<textarea name="txtbody" style="width:100%;height:320px;" > {$article.txt} ...
- 在vs2005中使用SVN进行版本管理
1.软件准备 VisualSVN-Server-2.5.10.msi SVN服务端 WindowsXP-KB942288-v3-x86.exe XP中安装TortoiseSVNSVN1.8必须 Tor ...