任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2586

How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24439    Accepted Submission(s): 9739

Problem 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

题意概括:

给一颗 N 个结点,N-1条边的树,和 M 次查询(离线)

解题思路:

要查询两点到最近公共祖先的距离和,首先要预处理出根结点到各个结点的距离(DFS)

然后 Tarjan 找出最近的公共祖先,两个结点到根结点的距离之和减去两倍的最近公共祖先到根结点的距离就是该查询的答案了。

AC code:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#define INF 0x3f3f3f3f
#define LL long long
const int MAX_N = 4e4+;
using namespace std; struct Edge{int v, nxt, w;}edge[MAX_N<<]; //静态邻接表
struct Query
{
int v, id;
Query(){};
Query(int _v, int _id):v(_v), id(_id){};
}; vector<Query> q[MAX_N]; // !!! //查询关系动态二维矩阵 int head[MAX_N], cnt; //静态邻接表头节点,边数
int dis[MAX_N]; //各结点到根结点的距离
int fa[MAX_N]; //并查集 祖先数组
bool vis[MAX_N], in[MAX_N]; //深搜用,找根结点用
int ans[MAX_N]; // !!! //各查询答案
int N, M; //结点数,查询数 void init()
{
memset(head, -, sizeof(head)); //初始化
memset(vis, false, sizeof(vis));
memset(in, false, sizeof(in));
memset(dis, , sizeof(dis));
memset(ans, , sizeof(ans));
cnt = ;
} void AddEdge(int from, int to, int weight) //静态邻接表加边操作
{
edge[cnt].v = to;
edge[cnt].w = weight;
edge[cnt].nxt = head[from];
head[from] = cnt++;
} int getfa(int x) //找祖先
{
int root = x;
while(fa[root] != root) root = fa[root]; int tmp;
while(fa[x] != root){
tmp = fa[x];
fa[x] = root;
x = tmp;
}
return root;
} void dfs(int s) //预处理各根结点到祖先的距离
{
int rt = s;
for(int i = head[s]; i != -; i = edge[i].nxt){
dis[edge[i].v] = dis[rt] + edge[i].w;
dfs(edge[i].v);
}
} void Tarjan(int s) // LCA
{
int root = s;
fa[s] = s;
for(int i = head[s]; i != -; i = edge[i].nxt){
int Eiv = edge[i].v;
Tarjan(Eiv);
fa[getfa(Eiv)] = s;
}
vis[s] = true;
for(int i = ; i < q[s].size(); i++){
if(vis[q[s][i].v] && !ans[q[s][i].id]){
ans[q[s][i].id] = dis[q[s][i].v] + dis[s] - *dis[getfa(q[s][i].v)];
}
}
} int main()
{
int T_case;
scanf("%d", &T_case);
while(T_case--){
init();
scanf("%d %d", &N, &M);
for(int i = , u, v, w; i < N; i++){ //建树
scanf("%d %d %d", &u, &v, &w);
AddEdge(u, v, w);
in[v] = true;
} for(int i = , u, v; i <= M; i++){ //储存查询信息(离线)
scanf("%d %d", &u, &v);
q[u].push_back(Query(v, i));
q[v].push_back(Query(u, i));
} int root = ;
for(int i = ; i <= N; i++) if(!in[i]){root = i; break;} //找树根结点 dfs(root); //预处理
Tarjan(root); //LCA for(int i = ; i <= M; i++){
printf("%d\n", ans[i]);
}
//puts("");
}
return ;
}

HDU 2586 How far away ?【LCA】的更多相关文章

  1. HDU 2586 How far away ?【LCA模板题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给你N个点,M次询问.1~N-1行输入点与点之间的权值,之后M行输入两个点(a,b)之间的最 ...

  2. HDU 2586——How far away ?——————【LCA模板题】

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

  3. hdu 2586 How far away ?倍增LCA

    hdu 2586 How far away ?倍增LCA 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路: 针对询问次数多的时候,采取倍增 ...

  4. HDU - 2586 How far away ?(LCA模板题)

    HDU - 2586 How far away ? Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & ...

  5. HDU 2586.How far away ?-离线LCA(Tarjan)

    2586.How far away ? 这个题以前写过在线LCA(ST)的,HDU2586.How far away ?-在线LCA(ST) 现在贴一个离线Tarjan版的 代码: //A-HDU25 ...

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

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

  7. HDU 2586 How far away ?(LCA模板 近期公共祖先啊)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the vi ...

  8. HDU 2586.How far away ?-在线LCA(ST)-代码很认真的写了注释(捞到变形)

    2018.9.10 0:40 重新敲一遍,然后很认真的写了注释,方便自己和队友看,刚过去的一天的下午打网络赛有一题用到了这个,但是没写注释,队友改板子有点伤,因为我没注释... 以后写博客,代码要写注 ...

  9. HDU 2586 How far away ? 离线lca模板题

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

随机推荐

  1. Laplace变换要点

    Laplace变换在求解微分方程.信号系统.自动控制领域都有重要作用.阅读<复变函数与积分变换>华中科大第三版,小结要点. 方便应用,最先给出变换表: 定义: 性质: 周期函数与卷积:

  2. NFS 优化及详解

    一, 启动过程: 所以启动的时候一定一定要先启用——————rpcbind———————————————— 启动 rpcbind  ------>/etc/init.d/rpcbind rest ...

  3. shell特殊字符汇总【转】

    Linux下无论如何都是要用到shell命令的,在Shell的实际使用中,有编程经验的很容易上手,但稍微有难度的是shell里面的那些个符号,各种特殊的符号在我们编写Shell脚本的时候如果能够用的好 ...

  4. python 爬虫系列05--丑事百科

    丑事百科爬虫 import re import requests def parse_page(url): headers = { 'User-Agent':'user-agent: Mozilla/ ...

  5. ListView与ArrayAdapter的搭配使用

    在android中,ListView是一种很重要的控件,一般的使用中,常建立一个所需类型的ArrayList,再通过ArrayAdapter把ListView绑定到ArrayList上,通过Array ...

  6. python实现excel转json的例子

    python实现excel转json的例子(改进版) 由于数值策划给出数值是excel表格,但前台flash程序用的又是json格式.服务器也用了json格式,而json又是utf-8编码的,用C++ ...

  7. 【数据结构】最小生成树之prim算法和kruskal算法

    在日常生活中解决问题经常需要考虑最优的问题,而最小生成树就是其中的一种.看了很多博客,先总结如下,只需要您20分钟的时间,就能完全理解. 比如:有四个村庄要修四条路,让村子能两两联系起来,这时就有最优 ...

  8. C# 批量生成随机密码,必须包含数字和字母,并用加密算法加密

    要求:密码必须包含数字和字母 思路:1.列出数字和字符. 组成字符串 :chars 2.利用randrom.Next(int i)返回一个小于所指定最大值的非负随机数. 3. 随机取不小于chars长 ...

  9. c#-IO和序列化操作

    IO 用到的命名空间:using System.IO; 文件和目录的管理! File类 FileInfo类 Directory类 DirectoryInfo类 操作文件的类! FileStream{ ...

  10. linux安装lua_nginx_module模块

    ngx_lua_module 是一个nginx http模块,它把 lua 解析器内嵌到 nginx,用来解析并执行lua 语言编写的网页后台脚本,可以用来实现灰度发布.另外淘宝的OpenResty也 ...