题目连接

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 ?的更多相关文章

  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 - Tarjan算法 离线 模板题)

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. HDU 2586 How far away ?(LCA在线算法实现)

    http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵树,求出树上任意两点之间的距离. 思路: 这道题可以利用LCA来做,记录好每个点距离根结点的 ...

  9. hdu 2586 How far away ? 带权lca

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) P ...

随机推荐

  1. c# 获取excel所有工作表

    var filePath="f:\xx.xlsx" string connStr = "Provider=Microsoft.Ace.OleDb.12.0;" ...

  2. python中operator.itemgetter

    直接上例子: rs=  [...     {...       "datetime": "Mon, 31 Aug 2015 23:00:00 GMT",...  ...

  3. 关于cookie

    CookieHelper.WriteCookie("DEPID", "theway", depid);   //先写入cookie //再读取cookie Us ...

  4. js日期的写法,获取girdviw的行数、提示信息、验证数量信息

    //制订日期(js日期的写法) var myDate = new Date(); var theDate = myDate.toLocaleDateString();  //获取今天的日期 //获取控 ...

  5. powerdesigner逆向导出oracle数据库结构显示备注

    最近接到命令,要将oracle数据库的结构导出为pdm文件供其他同事使用,逆向工程导出数据库结构比较方便,但是发现导出的数据库结构没有注释,这是很郁闷的事情: 查过网上很多资料都是sqlserver的 ...

  6. c# 将页面导出到word(含图片及控件)

    /// <summary> /// 创建word /// <param name="filePath">文件路径 </param> /// &l ...

  7. java基础回顾(一)—— sleep和wait的区别

    sleep是Thread类的一个方法,wait是Object类的一个方法 sleep是线程被调用时,占着cpu去睡觉,其他线程不能占用cpu,os认为该线程正在工作,不会让出系统资源 wait是进入等 ...

  8. web关键词搜索高亮代码

    <script type="text/javascript"> /* * 参数说明: * obj: 对象, 要进行高亮显示的html标签节点. * hlWords: 字 ...

  9. SVN与TortoiseSVN实战:标签与分支

    最近在写<IOS性能调优系列>,今天偷个懒,写写SVN与TortoiseSVN实战的第二篇,标签与分支. 第一篇详见<SVN与TortoiseSVN实战:从入门到精通> SVN ...

  10. myecplise 添加svn插件

    myecplise svn插件下载地址 http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID=2240 1.SVN下载地址 ...