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. Golang环境配置Centos

      1.下载go程序包( go1.7rc1.linux-amd64.tar.gz)(http://www.golangtc.com/static/go/1.7rc1/go1.7rc1.linux-am ...

  2. 【转载】java编程中'为了性能'一些尽量做到的地方

    1.尽量在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载的时间,提高加载的效率,但并不是所有地方都适用于单例,简单来说,单例主要适用于以下三个方面 第一,控制资源的使用,通过线程同步来控制资 ...

  3. css3实现轮播图

    css3动画属性简写: animation: name  duration  timing-function  delay  iteration-count  direction  fill-mode ...

  4. [SCOI2016]美味(可持久化线段树)

    可持久化trie树?好像和可持久化权值线段树差不多.. 如果这题没有那个\(x[i]\)这题就是一个裸的可持久化trie树. 仔细想想,多了这个\(x[i]\)之后有什么影响? 就是我们查询区间的时候 ...

  5. centeros 7开机自动挂载磁盘

    场景: 使用mount命令将新发现的磁盘/dev/sdb挂载给/liu后,但是重启后又看不到磁盘? 问题导致原因: 虽然我们可以使用mount命令去挂载磁盘,但是此操作只对服务器运行期间有效,也就是临 ...

  6. iOS基础UI控件介绍-Swift版

    iOS基础UI控件总结 iOS基础控件包括以下几类: 1.继承自NSObject:(暂列为控件) UIColor //颜色 UIImage //图像 2.继承自UIView: 只能相应手势UIGest ...

  7. 数组名作为函数参数以及sizeof用法

    来源:https://blog.csdn.net/jay_zhouxl/article/details/51745518 int f(int *p,char *a) { printf("p[ ...

  8. Mysql学习总结(23)——MySQL统计函数和分组查询

    1.使用count统计条数:select count(字段名...) from tablename; 2.使用avg计算字段的平均值:select avg(字段名) from tablename: 这 ...

  9. cogs 181. [USACO Jan07] 最高的牛

    181. [USACO Jan07] 最高的牛 ★★   输入文件:tallest.in   输出文件:tallest.out   简单对比时间限制:1 s   内存限制:32 MB FJ's N ( ...

  10. JAVA学习第四十六课 — 其它对象API(二)Date类 &amp; Calendar类(重点掌握)

    Date类(重点) 开发时,会时常遇见时间显示的情况,所以必须熟练Date的应用 <span style="font-family:KaiTi_GB2312;font-size:18p ...