http://poj.org/problem?id=1986

Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 14383   Accepted: 5063
Case Time Limit: 1000MS

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 1..1+M: Same format as "Navigation Nightmare"

* 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

* Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance. 

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

Farms 2 and 6 are 20+3+13=36 apart. 

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的更多相关文章

  1. POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)

    POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...

  2. POJ.1986 Distance Queries ( LCA 倍增 )

    POJ.1986 Distance Queries ( LCA 倍增 ) 题意分析 给出一个N个点,M条边的信息(u,v,w),表示树上u-v有一条边,边权为w,接下来有k个询问,每个询问为(a,b) ...

  3. POJ 1986 Distance Queries 【输入YY && LCA(Tarjan离线)】

    任意门:http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total ...

  4. POJ 1986 Distance Queries LCA两点距离树

    标题来源:POJ 1986 Distance Queries 意甲冠军:给你一棵树 q第二次查询 每次你问两个点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + d ...

  5. POJ 1986 Distance Queries(Tarjan离线法求LCA)

    Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 12846   Accepted: 4552 ...

  6. poj 1986 Distance Queries LCA

    题目链接:http://poj.org/problem?id=1986 Farmer John's cows refused to run in his marathon since he chose ...

  7. 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 ...

  8. 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 ...

  9. POJ 1986 Distance Queries(LCA Tarjan法)

    Distance Queries [题目链接]Distance Queries [题目类型]LCA Tarjan法 &题意: 输入n和m,表示n个点m条边,下面m行是边的信息,两端点和权,后面 ...

随机推荐

  1. HDU 3015 Disharmony Trees 【 树状数组 】

    题意:给出n棵树,给出横坐标x,还有它们的高度h,先按照横坐标排序,则它们的横坐标记为xx, 再按照它们的高度排序,记为hh 两颗树的差异度为 abs(xx[i] - xx[j]) * min(hh[ ...

  2. linux系统利用GPU跑数据(tensorflow)

    https://blog.csdn.net/qq_26591517/article/details/82469680 查看机器上GPU情况 命令: nvidia-smi 功能:显示机器上gpu的情况 ...

  3. 搭健MyBatis开发环境

    相关文献资料地址:http://www.mybatis.org/mybatis-3/zh/getting-started.html 关于如何创建一个项目,添加Tomcat运行环境和生成`web.xml ...

  4. BZOJ 2865 字符串识别(后缀数组+线段树)

    很容易想到只考虑后缀长度必须为\(max(height[rk[i]],height[rk[i]+1])+1\)(即\([i,i+x-1]\)代表的串只出现过一次)然后我正着做一遍反着做一遍,再取一个\ ...

  5. shell的通俗理解

    (引自:https://zhidao.baidu.com/question/557066905.html) [一] shell的含义: 首先shell的英文含义是“壳”: 它是相对于内核来说的,因为它 ...

  6. 题解 P2657 【[SCOI2009]windy数】

    感觉数位DP有点弱,强化一下... 这道题是一道比较裸的数位DP. 我们用\(dp[i][j]\)表示长度为\(i\)最高位为\(j\)的windy数有多少个,状态转移方程为\(dp[i][j]=\s ...

  7. 【BZOJ 1191】[HNOI2006]超级英雄Hero

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 二分图的最大匹配. 因为要答下一题,这一题必须先答完. 所以如果某道题没有匹配了. 那么就直接break掉. [代码] #inclu ...

  8. HDU 3415 Max Sum of Max-K-sub-sequence 单调队列题解

    本题又是一题单调队列题解. 技巧就是须要计算好前n项和Sn = a1 + a2 + ... an 这样方便处理. 记录一条单调队列,其意义是: q(head), q(head+1), ...q(tai ...

  9. hadoop-02-关闭防火墙

    hadoop-02-关闭防火墙 su root service iptables status #查看状态 即时关闭: service iptables stop #关闭 重启之后关闭: chkcon ...

  10. ORA-06553:PLS-306:wrong number or types of arguments in call to &#39;&#39;

    1.错误描写叙述 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/ ...