poj1986 LCA
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 11759 | Accepted: 4157 | |
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
题意:
n个点,m条边的树,不一定是树有可能有独立的点,但是这题查询的时候应该都是合法的。q次查询,求x,y的距离。
思路:
求出x,y的lca,然后减一减~~(具体看上篇)。
/*
* Author: sweat123
* Created Time: 2016/7/13 10:27:16
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define key_value ch[ch[root][1]][0]
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
struct node{
int to;
int val;
int next;
}edge[MAXN*];
int dfn[MAXN*],dis[MAXN],ver[MAXN*],vis[MAXN],dp[MAXN*][],first[MAXN],n,pre[MAXN],ind,m,tot;
void add(int x,int y,int z){
edge[ind].to = y;
edge[ind].val = z;
edge[ind].next = pre[x];
pre[x] = ind ++;
}
void dfs(int rt,int dep){
vis[rt] = ;
ver[++tot] = rt;
dfn[tot] = dep;
first[rt] = tot;
for(int i = pre[rt]; i != -; i = edge[i].next){
int t = edge[i].to;
if(!vis[t]){
dis[t] = dis[rt] + edge[i].val;
dfs(t,dep+);
ver[++tot] = rt;
dfn[tot] = dep;
}
}
}
void rmq(){
for(int i = ; i <= tot; i++){
dp[i][] = i;
}
for(int i = ; i < ; i++){
for(int j = ; j + ( << i) - <= tot; j++){
int x = dp[j][i-];
int y = dp[j + (<<(i-))][i-];
if(dfn[x] > dfn[y]){
dp[j][i] = y;
} else {
dp[j][i] = x;
}
}
}
}
int askrmq(int x,int y){
x = first[x];
y = first[y];
if(x > y)swap(x,y);
int k = (int)(log(y - x + ) * 1.0 / log(2.0));
int l = dp[x][k];
int r = dp[y-(<<k)+][k];
if(dfn[l] > dfn[r])return r;
else return l;
}
int main(){
while(~scanf("%d%d",&n,&m)){
ind = tot = ;
memset(vis,,sizeof(vis));
memset(pre,-,sizeof(pre));
for(int i = ; i <= m; i++){
int x,y,z;
char s[];
scanf("%d%d%d%s",&x,&y,&z,s);
add(x,y,z);
add(y,x,z);
}
memset(dis,,sizeof(dis));
dfs(,);
rmq();
int q;
scanf("%d",&q);
while(q--){
int x,y;
scanf("%d%d",&x,&y);
int tp = ver[askrmq(x,y)];
int ans = dis[x] - dis[tp] + dis[y] - dis[tp];
printf("%d\n",ans);
}
}
return ;
}
poj1986 LCA的更多相关文章
- POJ1986(LCA应用:求两结点之间距离)
Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 11304 Accepted: 3985 ...
- 最近公共祖先:LCA及其用倍增实现 +POJ1986
Q:为什么我在有些地方看到的是最小公共祖先? A:最小公共祖先是LCA(Least Common Ancestor)的英文直译,最小公共祖先与最近公共祖先只是叫法不同. Q:什么是最近公共祖先(LCA ...
- POJ1986 Distance Queries (LCA)(倍增)
Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 12950 Accepted: 4577 ...
- POJ1986 DistanceQueries 最近公共祖先LCA 离线算法Tarjan
这道题与之前那两道模板题不同的是,路径有了权值,而且边是双向的,root已经给出来了,就是1,(这个地方如果还按之前那样来计算入度是会出错的.数据里会出现多个root...数据地址可以在poj的dis ...
- poj1986带权lca
lca求距离,带权值 的树上求lca,我是用倍增法求的,求两点之间的距离转化为到根节点之间的距离 (de了一个小时 的bug,重打居然就过了....) #include<map> #inc ...
- poj-1986 Distance Queries(lca+ST+dfs)
题目链接: Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 11531 Accepted ...
- [poj1986]Distance Queries(LCA)
解题关键:LCA模板题 复杂度:$O(n\log n)$ #pragma comment(linker, "/STACK:1024000000,1024000000") #incl ...
- poj1986 Distance Queries(lca又是一道模版题)
题目链接:http://poj.org/problem?id=1986 题意:就是老问题求val[u]+val[v]-2*val[root]就行.还有这题没有给出不联通怎么输出那么题目给出的数据一定 ...
- 带权值的LCA
例题:http://poj.org/problem?id=1986 POJ1986 Distance Queries Language: Default Distance Queries Time L ...
随机推荐
- CoreData数据库浅析
Core Data是iOS5之后才出现的一个框架,它提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite数据库文件中,也能够将保存在数据库中的数据还原成OC对象.在此数 ...
- Android5.0 TimePicker,DatePicker恢复成低版本滚动模式
新版中的TimePicker DatePicker是不支持使用遥控器的, 恢复成低版本滚动模式只需要是xml文件加上一句即可: android:datePickerMode="spinner ...
- .NET 4.5 中新提供的压缩类
Windows8 的开发已经如火如荼开始了,在 Windows8 中提供的 .NET Framework 已经更新到了 4.5 版,其中又增加了一些新的特性,对压缩文件的支持就是其中之一. 在 4.5 ...
- sql 截取日期
截取日期: select to_char( NEW_TIME( sysdate, 'GMT','EST'), 'yyyy-mm')from dual; 或得年或月或日 Year/ month/Da ...
- SSIS 2010 BUG 一例
Sample data :abc|"edfg|xyz Test 1: Text Qualified is set to nothing. the result is in good for ...
- $是对string.Format的简化
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- iOS多线程到底不安全在哪里?
iOS多线程安全的概念在很多地方都会遇到,为什么不安全,不安全又该怎么去定义,其实是个值得深究的话题. 共享状态,多线程共同访问某个对象的property,在iOS编程里是很普遍的使用场景,我们就从P ...
- Zabbix监控VMare Vcenter
1.参照Zabbix文档配置 依照官方文档配置,没什么说的. zabbix官方文档:https://www.zabbix.com/documentation/3.2/manual/vm_monitor ...
- 微信公众平台开发——微信授权登录(OAuth2.0)
1.OAuth2.0简介 OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用. 允许用户 ...
- CentOS使用yum源中自带的rpm包安装LAMP环境
CentOS使用yum源中自带的rpm包安装LAMP环境.这是Linux下安装LAMP的环境一种最基本最简便的方式.新手可以从容安装使用. 1. 安装基础包(可选安装)yum install -y w ...