hdu 2586(Tarjan 离线算法)
How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14809 Accepted Submission(s): 5621
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.
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.
#include<iostream>
#include<cstdio>
#include<cmath>
#include<map>
#include<cstdlib>
#include<vector>
#include<set>
#include<queue>
#include<cstring>
#include<string.h>
#include<algorithm>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int N=+;
int head[N];
int vis[N];
int parent[N];
int ans[N];
int cnt;
vector<int>q[N];
vector<int>Q[N];
int dis[N];
int n;
int ancestor[N];
struct node{
int to,next,w;
}edge[*N+];
void init(){
cnt=;
memset(ans,,sizeof(ans));
memset(dis,,sizeof(dis));
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++){
parent[i]=i;
Q[i].clear();
q[i].clear();
}
}
void add(int u,int v,int w){
edge[cnt].to=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int find(int x){
int r=x;
while(r!=parent[x])r=parent[r];
int i=x;
int j;
while(parent[i]!=r){
j=parent[i];
parent[i]=r;
i=j;
}
return r;
}
void Union(int x,int y){
x=find(x);
y=find(y);
if(x!=y)parent[y]=x;
}
void Tarjan(int x,int w){
vis[x]=;
dis[x]=w;
//cout<<dis[x]<<endl;
for(int i=head[x];i!=-;i=edge[i].next){
int v=edge[i].to;
if(vis[v])continue;
Tarjan(v,w+edge[i].w);
Union(x,v);
ancestor[find(x)]=x;
}
for(int i=;i<q[x].size();i++){
int u=q[x][i];
if(vis[u]){
int t=ancestor[find(u)];
ans[Q[x][i]]=dis[x]+dis[u]-dis[t]*;
}
}
}
int main(){
int m;
int t;
scanf("%d",&t);
while(t--){
init();
scanf("%d%d",&n,&m);
int u,v,w;
for(int i=;i<n-;i++){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
for(int i=;i<m;i++){
scanf("%d%d",&u,&v);
q[u].push_back(v);
q[v].push_back(u);
Q[u].push_back(i);
Q[v].push_back(i);
}
Tarjan(,);
// for(int i=1;i<=n;i++)cout<<dis[i]<<" ";
//cout<<endl;
for(int i=;i<m;i++){
// cout<<4<<endl;
cout<<ans[i]<<endl;
}
}
}
hdu 2586(Tarjan 离线算法)的更多相关文章
- HDU 2586 /// tarjan离线求树上两点的LCA
题目大意: 询问一棵树里 u 到 v 的距离 可由 dis[ u到根 ] + dis[ v到根 ] - 2*dis[ lca(u,v) ] 得到 https://blog.csdn.net/csyzc ...
- LCA(最近公共祖先)--tarjan离线算法 hdu 2586
HDU 2586 How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- LCA问题的ST,tarjan离线算法解法
一 ST算法与LCA 介绍 第一次算法笔记这样的东西,以前学算法只是笔上画画写写,理解了下,刷几道题,其实都没深入理解,以后遇到新的算法要把自己的理解想法写下来,方便日后回顾嘛>=< R ...
- 最近公共祖先LCA Tarjan 离线算法
[简介] 解决LCA问题的Tarjan算法利用并查集在一次DFS(深度优先遍历)中完成所有询问.换句话说,要所有询问都读入后才开始计算,所以是一种离线的算法. [原理] 先来看这样一个性质:当两个节点 ...
- LCA 最近公共祖先 Tarjan(离线)算法的基本思路及其算法实现
首先是最近公共祖先的概念(什么是最近公共祖先?): 在一棵没有环的树上,每个节点肯定有其父亲节点和祖先节点,而最近公共祖先,就是两个节点在这棵树上深度最大的公共的祖先节点. 换句话说,就是两个点在这棵 ...
- LCA最近公共祖先(Tarjan离线算法)
这篇博客对Tarjan算法的原理和过程模拟的很详细. 转载大佬的博客https://www.cnblogs.com/JVxie/p/4854719.html 第二次更新,之前转载的博客虽然胜在详细,但 ...
- HDU-2586-How far away(LCA Tarjan离线算法)
链接:https://vjudge.net/problem/HDU-2586 题意: 勇气小镇是一个有着n个房屋的小镇,为什么把它叫做勇气小镇呢,这个故事就要从勇气小镇成立的那天说起了,修建小镇的时候 ...
- POJ 1330 Nearest Common Ancestors 【最近公共祖先LCA算法+Tarjan离线算法】
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20715 Accept ...
- HDU ACM 2586 How far away ?LCA->并查集+Tarjan(离线)算法
题意:一个村子有n个房子,他们用n-1条路连接起来,每两个房子之间的距离为w.有m次询问,每次询问房子a,b之间的距离是多少. 分析:近期公共祖先问题,建一棵树,求出每一点i到树根的距离d[i],每次 ...
随机推荐
- oracle 用户的操作
语法: CREATE USER user IDENTIFIED { BY password | EXTERNALLY [ AS 'certificate_DN' ] ...
- mt_rand()和rand()两者的区别
在随机读取中使用了mt_rand(),而不适用rand(),他们两者的区别: mt_rand()是更好地随机数生成器,因为它跟rand()相比播下了一个更好地随机数种子:而且性能上比rand()快4倍 ...
- zTree 模糊搜索
/** * 搜索树,高亮显示并展示[模糊匹配搜索条件的节点s] * @param treeId * @param searchConditionId 搜索条件Id */ function search ...
- API开发管理平台eoLinker AMS 4.1版本发布:加入聚合空间,发布AMS专业版等
eoLinker AMS是集API文档管理.API自动化测试.开发协作三位一体的综合API开发管理平台,是中国最大的在线API管理平台. eoLinker AMS 4.1更新内容: 1.新增" ...
- 解决hibernate删除时的异常 deleted object would be re-saved by cascade (remove deleted object from associa
今天在做项目时,需要删除一个对象,由于关联关系是一对多和多对一的关系,于是在代码中需要删除多的一方的对象时出现了 deleted object would be re-saved by cascade ...
- C# Thu Mar 1 00:00:00 UTC+0800 2012 如何转换为2012-03-01
string s = "Thu Mar 1 00:00:00 UTC+0800 2012"; DateTime dt = DateTime.ParseExact(s, " ...
- [HDU5807] Keep In Touch
\(Keep\ In\ Touch\):保持联络 \(Informatik\ verbindet\ dich\ und\ mich.\) 信息将你我连结? 发现这个方程很容易列出来. \(f[i][j ...
- git 删除某次指定的提交
reset命令有3种方式: 1:git reset –mixed:此为默认方式,不带任何参数的git reset,即时这种方式,它回退到某个版本,只保留源码,回退commit和index信息 2:gi ...
- 如何实现在scrapy调试爬虫
# -*- coding:utf-8 -*- from scrapy.cmdline import execute import sys import os '''在爬虫文件夹下面自定义一个main. ...
- noip2013 Day2 T2 花匠 解题报告
题目: 3289 花匠 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description 花匠栋栋种了一排花,每株花都有自己的高度.花儿越长越大, ...