poj 1986
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 8638 | Accepted: 3032 | |
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
Hint
Source
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <stack>
- #include <vector>
- using namespace std;
- const int MAX_N = ;
- int N,M;
- int first[MAX_N],Next[ * MAX_N],v[ * MAX_N];
- int id[MAX_N],vs[ * MAX_N];
- int dep[MAX_N * ],d[MAX_N * ][],qid[MAX_N * ][];
- int Dis[MAX_N],w[MAX_N * ];
- int n;
- void RMQ() {
- for(int i = ; i <= n; ++i) {
- d[i][] = dep[i];
- qid[i][] = i;
- }
- for(int j = ; ( << j) <= n; ++j) {
- for(int i = ; i + ( << j) - <= n; ++i) {
- if(d[i][j - ] > d[i + ( << (j - ))][j - ]) {
- d[i][j] = d[i + ( << (j - ))][j - ];
- qid[i][j] = qid[i + ( << (j - ))][j - ];
- } else {
- d[i][j] = d[i][j - ];
- qid[i][j] = qid[i][j - ];
- }
- }
- }
- }
- void add_edge(int id,int u) {
- int e = first[u];
- Next[id] = e;
- first[u] = id;
- }
- int query(int L,int R) {
- int k = ;
- while(( << (k + )) < (R - L + )) ++k;
- return d[L][k] < d[R - ( << k) + ][k] ?
- qid[L][k] : qid[R - ( << k) + ][k];
- }
- void dfs(int u,int fa,int d,int dis,int &k) {
- id[u] = k;
- vs[k] = u;
- dep[k++] = d;
- Dis[u] = dis;
- for(int e = first[u]; e != -; e = Next[e]) {
- if(v[e] != fa) {
- dfs(v[e],u,d + ,dis + w[e],k);
- vs[k] = u;
- dep[k++] = d;
- }
- }
- }
- int main()
- {
- // freopen("sw.in","r",stdin);
- scanf("%d%d",&N,&M);
- n = * N - ;
- for(int i = ; i <= N; ++i) first[i] = -;
- for(int i = ; i <= * M; i += ) {
- int u;
- char ch;
- scanf("%d%d%d %c",&u,&v[i],&w[i],&ch);
- //printf("%d %d %d\n",u,v[i],w[i]);
- w[i + ] = w[i];
- v[i + ] = u;
- add_edge(i,u);
- add_edge(i + ,v[i]);
- }
- int k = ;
- dfs(,-,,,k);
- RMQ();
- int Q;
- scanf("%d",&Q);
- for(int i = ; i <= Q; ++i) {
- int a,b;
- scanf("%d%d",&a,&b);
- int p = vs[ query(min(id[a],id[b]),max(id[a],id[b])) ];
- printf("%d\n",Dis[a] + Dis[b] - * Dis[p]);
- }
- return ;
- }
poj 1986的更多相关文章
- POJ 1986 Distance Queries(Tarjan离线法求LCA)
Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 12846 Accepted: 4552 ...
- 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 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 【输入YY && LCA(Tarjan离线)】
任意门:http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total ...
- POJ 1986:Distance Queries(倍增求LCA)
http://poj.org/problem?id=1986 题意:给出一棵n个点m条边的树,还有q个询问,求树上两点的距离. 思路:这次学了一下倍增算法求LCA.模板. dp[i][j]代表第i个点 ...
- poj 1986 Distance Queries(LCA:倍增/离线)
计算树上的路径长度.input要去查poj 1984. 任意建一棵树,利用树形结构,将问题转化为u,v,lca(u,v)三个点到根的距离.输出d[u]+d[v]-2*d[lca(u,v)]. 倍增求解 ...
- POJ 1986:Distance Queries
Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 18139 Accepted: 6248 ...
随机推荐
- OC编写使用调试器
OC编写使用调试器 编写代码免不了,Bug.那么Debug就是程序员的必备技能了.本文和大家一起探讨,如何在应用开发编写代码过程中,使用日志项消息:以及使用动作.条件.迭代控制增强断点. 记录信息 在 ...
- LN : leetcode 292 Nim Game
lc 292 Nim Game 292 Nim Game You are playing the following Nim Game with your friend: There is a hea ...
- oracle-审计导数
1.因审计需求,需要将MySQL.Oracle数据库中需要的表数据导入到SqlSERVER进行审计. 2.之前的方法: A. oracle组将表dump下来,进行压缩,传送到oracle导数服务器 ...
- centos rsync安装配置
安装 1 yum -y install rsync ---------------------服务器安装------------------------------- 创建基础配置文件 1 2 3 4 ...
- discuz分类信息地区联动菜单字段
1 = 河南省 1.1 = 郑州市 1.1.1 = 中原区 1.1.2 = 二七区 1.1.3 = 管城区 1.1.4 = 金水区 1.1.5 = 上街区 1.1.6 = 惠济区 1.1.7 = 巩义 ...
- StatusBar & StatusBarItem
StatusBar & StatusBarItem StatusBar StatusBar class StatusBarItem StatusBarItem class Example &l ...
- QT 十六进制字符串转化为十六进制编码
/*************************************************Function: hexStringtoByteArray()Description: 十六进制字 ...
- 查看图片真正的格式,在不知道扩展名的情况下区分是jpeg还是bmp
用系统自带的画图软件打开图片,然后按文件-->另存为就会弹出保存窗口.保存窗口的保存类形就是"照片真正的格式".
- unity2d之2d帧动画创建
在2d游戏中帧动画的应用是非常广泛的,那么如何在unity中创建一个帧动画,主要是下面几个步骤. 原文地址 :http://blog.csdn.net/dingkun520wy/article/de ...
- ubuntu 13.04 163源(亲测可用)
# deb cdrom:[Ubuntu )]/ trusty main restricted # See http://help.ubuntu.com/community/UpgradeNotes f ...