poj——1986 Distance Queries
| Time Limit: 2000MS | Memory Limit: 30000K | |
| Total Submissions: 14392 | Accepted: 5066 | |
| Case Time Limit: 1000MS | ||
Description
Input
* Line 2+M: A single integer, K. 1 <= K <= 10,000
* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms.
Output
Sample Input
7 6 1 6 13 E 6 3 9 E 3 5 7 S 4 1 3 N 2 4 20 W 4 7 2 S 3 1 6 1 4 2 6
Sample Output
13 3 36
Hint
Source
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 51000
using namespace std;
int n,m,x,y,z,t,tot,ans;
int fa[N],dis[N],top[N],deep[N],size[N],head[N];
struct Edge
{
int from,to,dis,next;
}edge[N<<];
int read()
{
,f=; char ch=getchar();
; ch=getchar();}
+ch-'; ch=getchar();}
return x*f;
}
int add(int x,int y,int z)
{
tot++;
edge[tot].to=y;
edge[tot].dis=z;
edge[tot].next=head[x];
head[x]=tot;
}
int dfs(int x)
{
size[x]=;
deep[x]=deep[fa[x]]+;
for(int i=head[x];i;i=edge[i].next)
{
int to=edge[i].to;
if(fa[x]==to) continue;
dis[to]=dis[x]+edge[i].dis;
fa[to]=x,dfs(to);size[x]+=size[to];
}
}
int dfs1(int x)
{
;
if(!top[x]) top[x]=x;
for(int i=head[x];i;i=edge[i].next)
{
int to=edge[i].to;
if(fa[x]!=to&&size[t]<size[to]) t=to;
}
if(t) top[t]=top[x],dfs1(t);
for(int i=head[x];i;i=edge[i].next)
{
int to=edge[i].to;
if(fa[x]!=to&&to!=t) dfs1(to);
}
}
int lca(int x,int y)
{
for(;top[x]!=top[y];x=fa[top[x]])
if(deep[top[x]]<deep[top[y]])
swap(x,y);
if(deep[x]>deep[y]) swap(x,y);
return x;
}
int main()
{
n=read(),m=read();
;i<=m;i++)
{
x=read(),y=read(),z=read();
add(x,y,z),add(y,x,z);
}
dfs(),dfs1();
t=read();
;i<=t;i++)
{
x=read(),y=read();
ans=dis[x]+dis[y]-*dis[lca(x,y)];
printf("%d\n",ans);
}
;
}
poj——1986 Distance Queries的更多相关文章
- 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 ...
- POJ 1986 Distance Queries(Tarjan离线法求LCA)
Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 12846 Accepted: 4552 ...
- poj 1986 Distance Queries LCA
题目链接:http://poj.org/problem?id=1986 Farmer John's cows refused to run in his marathon since he chose ...
- POJ 1986 - Distance Queries - [LCA模板题][Tarjan-LCA算法]
题目链接:http://poj.org/problem?id=1986 Description Farmer John's cows refused to run in his marathon si ...
- poj 1986 Distance Queries(LCA)
Description Farmer John's cows refused to run in his marathon since he chose a path much too long fo ...
- POJ 1986 Distance Queries(LCA Tarjan法)
Distance Queries [题目链接]Distance Queries [题目类型]LCA Tarjan法 &题意: 输入n和m,表示n个点m条边,下面m行是边的信息,两端点和权,后面 ...
- poj 1986 Distance Queries 带权lca 模版题
Distance Queries Description Farmer John's cows refused to run in his marathon since he chose a pa ...
随机推荐
- selenium利用Excel进行参数化(简单示例)
上篇搭建好环境后,正真开始我的自动化之旅了.... 开始之前特别说明一下testNG的版本,不能直接使用Eclipse直接线上下载的版本,线上版本太高,不能兼容,运行程序会报以下错误,需要自行下载低一 ...
- HDU 1930 CRT
也是很模板的一道题,给出一些数,分割,模数固定是4个互质的. /** @Date : 2017-09-16 23:54:51 * @FileName: HDU 1930 CRT.cpp * @Plat ...
- CF821 C. Okabe and Boxes 栈模拟
Link 题意:给出操作,如果当前出栈操作使得出栈序列非顺序,可以在此之前自由排序栈中所有数,问最少排几次. 思路:已经出栈到第x个元素时,每次需要排序的操作后,能够保证前x元素出栈有序,否则说明该操 ...
- ④ 设计模式的艺术-04.抽象工厂(Abstract Factory)模式
抽象工厂模式 用来生产不同产品族的全部产品.(对于增加新的产品,无能为力:支持增加产品族) 抽象工厂模式是工厂方法模式的升级版本,在有多个业务品种.业务分类时,通过抽象工厂模式产生需要的对象是一种非常 ...
- MySQL性能优化之道
1.in和not in子查询优化 not in 是不能命中索引的,所以以下子查询性能很低. 如果是确定且有限的集合时,可以使用.如 IN (0,1,2). 用 exists或 notexists代替 ...
- 【BZOJ】3991: [SDOI2015]寻宝游戏 虚树+DFS序+set
[题意]给定n个点的带边权树,对于树上存在的若干特殊点,要求任选一个点开始将所有特殊点走遍后返回.现在初始没有特殊点,m次操作每次增加或减少一个特殊点,求每次操作后的总代价.n,m<=10^5. ...
- c++ virtual总结
virtual-关键字用于修饰成员函数时,有以下特性 1.用于修饰的基类的成员函数,被修饰的基类成员函数-其派生类的同名成员函数也默认带有virtual 关键字2.当virtual 用于修饰析构函数( ...
- 大聊Python----装饰器
什么是装饰器? 装饰器其实和函数没啥区别,都是用def去定义的,其本质就是函数,而功能就是装饰其他的函数,说白了就是为其他函数提供附加功能 装饰器有什么作用? 比如你是一个公司的员工,你所写的程序里有 ...
- 在wamp下增加多版本的PHP(PHP5.3,PHP5.4,PHP5.5)支持
1.安装WAMPServer 根据自己的操作系统选择相应的WAMP版本,我这里选择WAMPSERVER-32 BITS & PHP 5.5-2.5, 双击安装,选择安装目录即可,超级简单.根据 ...
- utsrelease.h 包含svn信息
utsrelease.h是一个自动生成的文件,没有办法修改,但这个数据是根据Makefile和.config的内容进行生成的,通过修改这两个文件的内容,可以改变!/usr/src/linux/Make ...