POJ——T 1986 Distance Queries
http://poj.org/problem?id=1986
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 14383 | Accepted: 5063 | |
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 <algorithm>
#include <cstdio> using namespace std; const int N();
char s[];
int n,m,dis[N],head[N],sumedge;
struct Edge
{
int v,w,next;
Edge(int v=,int next=,int w=):
v(v),next(next),w(w){}
}edge[N<<];
inline void ins(int u,int v,int w)
{
edge[++sumedge]=Edge(v,head[u],w);
head[u]=sumedge;
} int size[N],top[N],dad[N],son[N],deep[N];
void DFS(int u,int fa,int deepth)
{
dad[u]=fa;
size[u]=;
deep[u]=deepth;
for(register int v,i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
if(dad[u]==v) continue;
dis[v]=dis[u]+edge[i].w;
DFS(v,u,deepth+);
size[u]+=size[v];
if(size[son[u]]<size[v]) son[u]=v;
}
}
void DFS_(int u,int Top)
{
top[u]=Top;
if(son[u]) DFS_(son[u],Top);
for(int v,i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
if(dad[u]!=v&&son[u]!=v) DFS_(v,v);
}
}
int LCA(int x,int y)
{
for(;top[x]!=top[y];x=dad[top[x]])
if(deep[top[x]]<deep[top[y]]) swap(x,y);
return deep[x]<deep[y]?x:y;
} int main()
{
scanf("%d%d",&n,&m);
for(int u,v,w;m--;)
{
scanf("%d%d%d%s",&u,&v,&w,s);
ins(u,v,w); ins(v,u,w);
}
DFS(,,); DFS_(,);
int k;scanf("%d",&k);
for(int u,v;k--;)
{
scanf("%d%d",&u,&v);
printf("%d\n",dis[v]+dis[u]-dis[LCA(u,v)]*);
}
return ;
}
POJ——T 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 【输入YY && LCA(Tarjan离线)】
任意门:http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total ...
- POJ 1986 Distance Queries LCA两点距离树
标题来源:POJ 1986 Distance Queries 意甲冠军:给你一棵树 q第二次查询 每次你问两个点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + d ...
- 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行是边的信息,两端点和权,后面 ...
随机推荐
- python下py2exe打包笔记
1.下载与python版本一致的py2exe插件包 2.安装py2exe,安装后在python目录下存在:\Lib\site-packages\py2exe\... 3.新建一个python脚本文件, ...
- 一个php处理图片裁剪,压缩,水印的小代码
插件地址:https://github.com/cigua/imagefilter
- swift语言点评二十-扩展
结论:扩展无法修改原来的数据结构. Extensions can add new functionality to a type, but they cannot override existing ...
- ARC下dealloc过程及.cxx_destruct的探究
我是前言 这次探索源自于自己一直以来对ARC的一个疑问,在MRC时代,经常写下面的代码: - (void)dealloc { self.array = nil; self.string = nil; ...
- SpringBoot学习笔记(8)-----SpringBoot文件上传
直接上代码,上传文件的前端页面: <body> <form action="/index/upload" enctype="multipart/form ...
- About CNN(convolutional neural network)
NO.1卷积神经网络基本概念 CNN是第一个被成功训练的多层深度神经网络结构,具有较强的容错.自学习及并行处理能力.最初是为识别二维图像而设计的多层感知器,局部连接和权值共享网络结构 类似于生物神经网 ...
- web移动端-弹性盒模型
(父元素加) : /*新版弹性盒模型*/ /* display: flex; */ /*设置主轴方向为水平方向*/ /* flex-direction: row; */ /*设置主轴方向为垂直方向*/ ...
- 虚拟集群LVS及DR模式搭建笔记
LVS(虚拟集群Linux Virtual Server) LVS-NAT:地址转换,数据包来回都要经过NAT转换,所以Director Server(即LVS服务器)将成为系统瓶颈.使用NAT模式将 ...
- 题解 P3413 【SAC#1 - 萌数】
这道题刚开始正向思维,然后处理重复的时候咕咕了. 参考了@巨型方块 大佬的题解后AC了,在这里就说几个我觉得比较重要或是容易被忽略的点,然后补充一些跳过的证明. 这道题的状态可以设为$dp[i][j] ...
- 【翻译自mos文章】开启dblink的 oracle net trace/tracing --对dblink进行跟踪的方法
开启dblink的 oracle net trace/tracing --对dblink进行跟踪的方法. 參考原文: DBLINK: How to Enable Oracle Net Tracing ...