poj 1986 Distance Queries(LCA)
Description
Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible!
Input
* Lines ..+M: Same format as "Navigation Nightmare" * Line +M: A single integer, K. <= K <= , * Lines +M..+M+K: Each line corresponds to a distance query and contains the indices of two farms.
Output
* Lines ..K: For each distance query, output on a single line an integer giving the appropriate distance.
E
E
S
N
W
S
Sample Output
Hint
Farms and are ++= apart.
Source
题意:给一棵带权重的树,共有k个查询,每次查询树中2个结点的距离。结点数n最大为40000,k最大10000
分析:首先我们将无根树转为有根树,可以在O(n)时间内得到每个结点到根结点的距离。由于在树中从一个结点走到另一个结点的路径是唯一的,所以a到b的路径一定经过lca(a,b),设lca(a,b)=c。此时不难发现d(a,b)=d(a,root)+d(b,root)-2*d(c,root)。
这里用的是并查集的方法查找LCA,时间1000+ms,感觉有点慢
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 100006
#define inf 1e12
int n,m; int tot,qsize;
int head[N],qhead[N];
int vis[N];//标记
int dis[N];//距离
int fa[N]; struct Node
{
int from;
int to;
int next;
int cost;
}edge[N<<],qe[N];
void init()
{
tot=; qsize=; memset(head,-,sizeof(head));
memset(qhead,-,sizeof(qhead));
memset(vis,,sizeof(vis));
memset(fa,,sizeof(fa));
memset(dis,,sizeof(dis));
memset(edge,,sizeof(edge));
memset(qe,,sizeof(qe));
}
void addEdge(int s,int u,int c)//邻接矩阵函数
{
edge[tot].from=s;
edge[tot].to=u;
edge[tot].cost=c;
edge[tot].next=head[s];
head[s]=tot++;
}
void addQedge(int s,int u){
qe[qsize].from=s;
qe[qsize].to=u;
qe[qsize].next=qhead[s];
qhead[s]=qsize++;
} /////////////////////////////////////////////////////////////
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
/////////////////////////////////////////////////////////////
void tarjan(int u)//tarjan算法找出图中的所有强连通分支
{
fa[u]=u;
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
if(!vis[v]){
dis[v]=dis[u]+edge[i].cost;
tarjan(v);
fa[v]=u;
}
}
for(int i=qhead[u];i!=-;i=qe[i].next){
int v=qe[i].to;
if(vis[v]){
qe[i].cost=dis[u]+dis[v]-*dis[find(v)];
qe[i^].cost=qe[i].cost;
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)==){
init();
int a,b,c;
char s[];
for(int i=;i<m;i++){
scanf("%d%d%d%s",&a,&b,&c,s);
addEdge(a,b,c);
addEdge(b,a,c);
}
int q;
scanf("%d",&q);
while(q--){
scanf("%d%d",&a,&b);
addQedge(a,b);
addQedge(b,a);
}
tarjan();
for(int i=;i<qsize;i+=){
printf("%d\n",qe[i].cost);
}
}
return ;
}
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 200000
#define inf 1e12
vector<pair<int,int> >edge[N];
vector<pair<int,int> >que[N];
int n,m,q;
int ans[N];
int dis[N];
int fa[N];
int vis[N];
int sum;
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
void LCA(int u,int p){
fa[u]=u;
for(int i=;i<edge[u].size();i++){
int v=edge[u][i].first;
if(v==p) continue;
dis[v]=dis[u]+edge[u][i].second;
LCA(v,u);
fa[v]=u;
}
vis[u]=;
if(sum==q) return;
for(int i=;i<que[u].size();i++){
int v=que[u][i].first;
if(vis[v]){
ans[que[u][i].second]=dis[u]+dis[v]-*dis[find(v)];
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)==){ for(int i=;i<N;i++){
edge[i].clear();
que[i].clear();
}
sum=;
memset(vis,,sizeof(vis)); char s[];
for(int i=;i<m;i++){
int a,b,c;
scanf("%d%d%d%s",&a,&b,&c,s);
edge[a].push_back(make_pair(b,c));
edge[b].push_back(make_pair(a,c));
} scanf("%d",&q);
for(int i=;i<q;i++){
int x,y;
scanf("%d%d",&x,&y);
que[x].push_back(make_pair(y,i));
que[y].push_back(make_pair(x,i));
ans[i]=;
}
dis[]=;
LCA(,); for(int i=;i<q;i++){
printf("%d\n",ans[i]);
}
}
return ;
}
poj 1986 Distance Queries(LCA)的更多相关文章
- poj 1986 Distance Queries(LCA:倍增/离线)
计算树上的路径长度.input要去查poj 1984. 任意建一棵树,利用树形结构,将问题转化为u,v,lca(u,v)三个点到根的距离.输出d[u]+d[v]-2*d[lca(u,v)]. 倍增求解 ...
- POJ 1986 Distance Queries(Tarjan离线法求LCA)
Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 12846 Accepted: 4552 ...
- POJ 1986 Distance Queries (Tarjan算法求最近公共祖先)
题目链接 Description Farmer John's cows refused to run in his marathon since he chose a path much too lo ...
- POJ1986 Distance Queries (LCA)(倍增)
Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 12950 Accepted: 4577 ...
- POJ - 1986 Distance Queries(离线Tarjan算法)
1.一颗树中,给出a,b,求最近的距离.(我没考虑不联通的情况,即不是一颗树的情况) 2.用最近公共祖先来求, 记下根结点到任意一点的距离dis[],这样ans = dis[u] + dis[v] - ...
- POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)
POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...
- POJ.1986 Distance Queries ( LCA 倍增 )
POJ.1986 Distance Queries ( LCA 倍增 ) 题意分析 给出一个N个点,M条边的信息(u,v,w),表示树上u-v有一条边,边权为w,接下来有k个询问,每个询问为(a,b) ...
- POJ 1986 Distance Queries LCA两点距离树
标题来源:POJ 1986 Distance Queries 意甲冠军:给你一棵树 q第二次查询 每次你问两个点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + d ...
- POJ 1986 Distance Queries 【输入YY && LCA(Tarjan离线)】
任意门:http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total ...
随机推荐
- python高级编程 编写一个包1
#目的是:编写,发行python包可重复过程"""1:是缩短开始真正工作之前所需要的设置时间,也就是提供模板2:提供编写包的标准化方法3:简化测试驱动开发方法的使用4:为 ...
- 关于java读取和写入properties配置文件的内容
一般通过使用流的方式进行读取 代码示例如下: package com.zznode.transmit.util; import java.io.FileInputStream; import java ...
- 文件atime未变问题的研究
1. atime, ctime 以及mtime 这三个名词属于文件/文件夹的属性,存在于inode数据结构之中. 通过系统调用stat可以获取stat结构,其中包括:atime(accesstime) ...
- R6010 -abort() has been called错误分析及其解决方法
近期使用vs2010编程出现下面问题.在网上收集了大家的意见之后,整理了一下 导致出现这种原因有: 1.非法指针訪问和内存泄漏 2.大家再查查吧.一定是指针出现故障了.设置的指针范围跟你执行的不正确 ...
- 跟我学系列教程——《13天让你学会Redis》火热报名中
学习目标 每天2小时,13天让你学会Redis. 本课程针对Redis新手,甚至连Redis是什么都没有听说过的同学.课程会具体介绍Redis是什么以及为什么要使用Redis,结合项目实践旨在让学生从 ...
- JAVA设计模式-工厂模式(代码示例)
结构 MySort.java 实际业务中我们可能会使用任意一种排序方法 package pers.zander.edu.arithmetic.sort; /** * 排序接口 * @author * ...
- menu控件绑定sql数据库
public int treeID; protected void Page_Load(object sender, EventArgs e) { SqlConnection CN = new Sql ...
- 企业qq代码,工作中用到的
<div id="xixi" onmouseover="toBig()" style="top: 120px; left: 0; positio ...
- Android-----输入法的显示和隐藏
/** * 控制手机虚拟键盘的显示和隐藏 */public class InputMethodUtil { /** * 隐藏虚拟键盘 * @param v 参数v为获取焦点对象view */ pub ...
- div没有设置高度时背景颜色不显示(浮动)
在使用div+css进行网页布局时,如果外部div有背景颜色或者边框,而不设置其高度,在IE浏览器下显示正常.但是使用Firefox/opera浏览时却出现最外层Div的背景颜色和边框不起作用的问题. ...