poj 1986 Distance Queries
好像是模板题 当作练习题 不错; 要求任意两点之间的距离。可以假设一个根节点,然后所有点到根节点的距离,然后求出任意两点多公共祖先; 距离就变成了
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的更多相关文章
- 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 ...
随机推荐
- iOS NSString 和NSData 转换
NSString 转换成NSData 对象 NSData* xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncodin ...
- WCF传输Dataset大数据量 -压缩
由于WCF不能传输DataTable(不能序列化),所以更多项目中都会使用DataSet作为查询集合的首选返回类型,但是由于DataSet会生成很多的状态信息等,所以DataSet体积也会变大,有几种 ...
- hdu2021(很闲~~)
http://acm.hdu.edu.cn/showproblem.php?pid=2021 water~~~ #include<iostream> #include<stdio.h ...
- Shell练习 统计单词个数,降序排列
原文:https://leetcode.com/problems/word-frequency/ Write a bash script to calculate the frequency of e ...
- web.xml文件中加载顺序的优先级
在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是 ...
- ios开发--animation flash动画
/** * showAnimationFlash */ + (void)showAnimationFlashWithView:(UIView *)animationView durati ...
- springMVC找不到JS等文件
应用springMVC时 JS等文件找不到错误 应用springMVC时如果配置URL映射时如下配置 <servlet> <servlet-name>appSe ...
- SGU 106 The equation 扩展欧几里得好题
扩展欧几里得的应用……见算法竞赛入门经典p.179 注意两点:1.解不等式的时候除负数变号 2.各种特殊情况的判断( a=0 && b=0 && c=0 ) ( a=0 ...
- c# 网络是否连接
c# 网络是否连接 方案一: using System; using System.Collections.Generic; using System.Linq; using System.Text ...
- [置顶] ArcGIS10.1完美破解步骤详细图文教程
ArcGIS软件安装其实都比较简单的,只要大家清楚每个步骤,顺序安装即可.但是安装过程要注意一些问题,license先安装,安装完成先停止服务,然后再安装desktop.完成后就是破解步骤了,很多同学 ...