好像是模板题  当作练习题 不错;  要求任意两点之间的距离。可以假设一个根节点,然后所有点到根节点的距离,然后求出任意两点多公共祖先;  距离就变成了

dis[u]+dis[v] - 2*dis[ lca(u,v) ]  非常好的题目

 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std; struct date{
int v,val,next;
}edge[];
int N,M,total,head[],dis[];
void add_edge( int u,int v,int val ){
edge[total].v = v;
edge[total].val = val;
edge[total].next = head[u];
head[u] = total++;
}
bool vis[]; int num,sta[],tab[],dep[],dp[][];
void LCA( int son,int deep,int w ){
dep[num] = deep; tab[num] = son; sta[son] = num++; dis[son] = w; vis[son] = true;
for( int i = head[son]; i != -; i = edge[i].next ){
int v = edge[i].v; //cout<<edge[i].val<<endl;
if( !vis[v] )
{ LCA( v,deep+,w+edge[i].val ); dep[num] = deep; tab[num] = son; num++; }
}
//dep[num] = deep; tab[num] = son; num++;
}
int work( int n1,int n2 ){
if( dep[n1] < dep[n2] )return n1;
return n2;
}
void RMQ( ){
for( int i = ; i <= num; i++ )dp[i][] = i;
for( int i = ; (<<i) <= num; i++ )
for( int j = ; j - + (<<i) <= num; j++ )
dp[j][i] = work( dp[j][i-],dp[j+(<<(i-))][i-] );
}
int query( int L,int R )
{
int k = ;
while( (<<(k+)) <= (R-L+) )k++;
return tab[work( dp[L][k],dp[R-(<<k)+][k] )];
}
int main( )
{
int u,v,w; char str[];
while( scanf("%d%d",&N,&M) != EOF )
{
memset( head,-,sizeof(head) ); total = ;
for( int i = ; i <= M; i++ )
{
scanf("%d%d%d%s",&u,&v,&w,&str);
add_edge( u,v,w );
add_edge( v,u,w );
}
memset( vis,,sizeof(vis) );
num = ; LCA( ,, ); num--; RMQ( );
//for( int i = 1; i <= num; i++ )cout<<i<<" "<<dep[i]<<" "<<tab[i]<<endl;
int Q; scanf("%d",&Q);
while( Q-- ){
scanf("%d%d",&u,&v);
//cout<<u<<" "<<v<<" "<<dis[u]<<" "<<dis[v]<<endl;
if( sta[u] > sta[v] )swap( u,v );
int t = query(sta[u],sta[v]);//cout<<endl<<" "<<t<<endl;
//cout<<t<<" "<<dis[1]<<" "<<u<<" "<<v<<" "<<dis[u]<<" "<<dis[v]<<endl;
cout<<dis[u]+dis[v]-*dis[t]<<endl;
}
}
return ;
}

poj 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 LCA两点距离树

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

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

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

  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行是边的信息,两端点和权,后面 ...

  10. poj 1986 Distance Queries 带权lca 模版题

    Distance Queries   Description Farmer John's cows refused to run in his marathon since he chose a pa ...

随机推荐

  1. C#--判断当前是否是移动设备和设备的型号

    --如果是移动设备是true var ismobile = System.Web.HttpContext.Current.Request.Browser.IsMobileDevice; --设备型号( ...

  2. 如何开启Centos6.4系统的SSH服务

    无论是Centos6.4系统的虚拟电脑还是服务器,始终感觉直接在命令行中操作不方便:比如全选.复制.粘贴.翻页等等.比如服务器就需要在机房给服务器接上显示器.键盘才操作感觉更麻烦.所以就可借助SSH( ...

  3. MONO常用快捷键

      Action Mac OS X Win/Linux 注释代码(//) Cmd + / Ctrl + / 注释代码(/**/) Cmd + Option + / Ctrl + Shift + / 格 ...

  4. C#基本语句

    1.创建个控制台应用程序(显示当前时间)然后在默认生成的Main函数里下如下代码:Console.WriteLine("显示当前时间:{0}",DateTime.Now.ToStr ...

  5. ruby 学习 -- Array --2

    定义: [1, 2, 3] # An array that holds three Fixnum objects [-10...0, 0..10,] # An array of two ranges; ...

  6. 构建高性能web之路------mysql读写分离实战

    http://blog.csdn.net/cutesource/article/details/5710645 http://www.jb51.net/article/38953.htm http:/ ...

  7. ios开发理解nil,Nil, NULL

    nil是一个对象指针为空,Nil是一个类指针为空,NULL是基本数据类型为空.这些可以理解为nil,Nil, NULL的区别吧. iOS剪切板 UIPasteboard *pasteboard = [ ...

  8. Java:装饰设计模式

    装饰设计模式: 当想要对已有的对象进行功能增强时,可以定义类,将已有对象传入,基于已有的功能, 并提供加强功能,那么自定义的该类就称为装饰类. 装饰类通常通过构造方法接收被装饰的对象,并基于被装饰的对 ...

  9. 解决Ext.form.DateField在浏览器中显示可能有问题

    解决Ext.form.DateField在浏览器中显示可能有问题,界面将会拉的很长很长.如下图:  办法: 1:设置css的长度 .x-date-menu { padding-top:2px;padd ...

  10. 测试 float

    关于blog上对 float的许多讨论,这个函数不对,那个运算出错. 其实原因不在这里,根源是计算机没有向你保证你看到的就是真实的,都是一厢情愿而已. 废话不说.下面是测试,一看就明白.再不明白的看看 ...