POJ - 1984 Navigation Nightmare 种类并查集
思路:记录每个点与其根结点的横向距离和纵向距离,当知道其父节点与根结点的关系,很容易推出当前节点与根结点的关系:
直接相加即可。
int p = a[x].par; a[x].dx += a[p].dx; a[x].dy += a[p].dy;
合并什么的就不多说了,很容易得出。
值得一说的就是需要按照合并节点的顺序把询问排序,最后按照询问的顺序再排一次序。
AC代码
#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 40000 + 5;
struct node{
int par;
int dx, dy;
}a[maxn];
struct Edge{
int u, v, dis, ch;
}b[maxn];
struct Question{
int u, v, t, num;
}q[10000+5];
void init(int n) {
for(int i = 1; i <= n; ++i) {
a[i].par = i;
a[i].dx = a[i].dy = 0;
}
}
int find(int x) {
if(a[x].par == x) return x;
int r = find(a[x].par);
int p = a[x].par;
a[x].dx += a[p].dx;
a[x].dy += a[p].dy;
return a[x].par = r;
}
void unionset(int x, int y, int dx, int dy) {
int rx = find(x), ry = find(y);
if(rx != ry) {
a[rx].par = y;
a[rx].dx = dx - a[x].dx;
a[rx].dy = dy - a[x].dy;
}
}
int get_dis(int x, int y) {
int rx = find(x), ry = find(y);
if(rx != ry) return -1;
return abs(a[x].dx - a[y].dx) + abs(a[x].dy - a[y].dy);
}
bool cmp1(const Question &a, const Question &b) {
return a.t < b.t;
}
bool cmp2(const PI &a, const PI &b) {
return a.second < b.second;
}
int main() {
int n, m, Q;
while(scanf("%d%d", &n, &m) == 2) {
init(n);
for(int i = 1; i <= m; ++i) {
scanf("%d %d %d %c", &b[i].u, &b[i].v, &b[i].dis, &b[i].ch);
//printf("%d %d %d %c\n", b[i].u, b[i].v, b[i].dis, b[i].ch);
}
scanf("%d", &Q);
for(int i = 1; i <= Q; ++i) {
scanf("%d%d%d", &q[i].u, &q[i].v, &q[i].t);
q[i].num = i;
}
vector<PI>ans;
sort(q+1, q+Q+1, cmp1);
int ind = 1;
for(int i = 1; i <= m; ++i) {
int dx, dy;
switch(b[i].ch) {
case 'E': dx = b[i].dis, dy = 0; break;
case 'W': dx = -b[i].dis, dy = 0; break;
case 'N': dx = 0, dy = b[i].dis; break;
case 'S': dx = 0, dy = -b[i].dis; break;
}
unionset(b[i].u, b[i].v, dx, dy);
while(ind <= Q && q[ind].t == i) {
ans.push_back(make_pair(get_dis(q[ind].u, q[ind].v), q[ind].num));
++ind;
}
}
sort(ans.begin(), ans.end(), cmp2);
for(int i = 0; i < ans.size(); ++i) printf("%d\n", ans[i].first);
}
return 0;
}
如有不当之处欢迎指出!
POJ - 1984 Navigation Nightmare 种类并查集的更多相关文章
- POJ1984 Navigation Nightmare —— 种类并查集
题目链接:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K T ...
- POJ 1984 Navigation Nightmare 【经典带权并查集】
任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K To ...
- poj 1182:食物链(种类并查集,食物链问题)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44168 Accepted: 12878 Description ...
- poj 1182 食物链(种类并查集 ‘初心者’)
题目链接:http://poj.org/problem?id=1182 借着这题可以好好理解一下种类并查集,这题比较简单但挺经典的. 题意就不解释了,中问题. 关于种类并查集结局方法也是挺多的 1扩增 ...
- POJ 1984 Navigation Nightmare 带全并查集
Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= ...
- POJ 1984 - Navigation Nightmare - [带权并查集]
题目链接:http://poj.org/problem?id=1984 Time Limit: 2000MS Memory Limit: 30000K Case Time Limit: 1000MS ...
- POJ 1984 Navigation Nightmare(二维带权并查集)
题目链接:http://poj.org/problem?id=1984 题目大意:有n个点,在平面上位于坐标点上,给出m关系F1 F2 L D ,表示点F1往D方向走L距离到点F2,然后给出一系 ...
- poj 1984 Navigation Nightmare(带权并查集+小小的技巧)
题目链接:http://poj.org/problem?id=1984 题意:题目是说给你n个线,并告知其方向,然后对于后面有一些询问,每个询问有一个时间点,要求你输出在该时间点a,b的笛卡尔距离,如 ...
- POJ 1182 食物链(种类并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63592 Accepted: 18670 Description ...
随机推荐
- Django 模版中如何对主菜单进行选中?
问题描叙: 在实际模版中,返回的页面我们想要主菜单保持一种状态,这个主菜单进去的子页面,都需要保持主菜单选择状态 例如 主菜单是 课程, 下面展现的章节和视频页面时候, 课程这个主菜单在展现这个菜单下 ...
- 六、Html头部和元信息
前面整理的都是html常用到的标签,这里整理一下html的的头部和元信息标签. 定义html都的头部要写在<head>标签里面,一般他还包含如下一些标签: 1,<script> ...
- 【转】Linux上vi(vim)编辑器使用教程
Linux上vi(vim)编辑器使用教程 ------------------------------------------------------------ ikong ------------ ...
- 权限问题导致zabbix无法监控mysql
说说一个困扰自已两天的问题. 首先是用常规的方法安装上了mysql数据库.做了主从. 在监控从库的时候,发现所有的监控数据库的监控项都获取不到key值 . zabbix server端也不报错.获取到 ...
- SQL语句-create语句
SQL语法基础 Create database语句 Create database语句是在MySQL实例上创建一个指定名称的数据 库,create schema语句的语义和create databas ...
- 【视频编解码·学习笔记】5. NAL Unit 结构分析
在上篇笔记中通过一个小程序,可以提取NAL Unit所包含的的字节数据.H.264码流中的每一个NAL Unit的作用并不是相同的,而是根据不同的类型起不同的作用.下面将对NAL Unit中的数据进行 ...
- HARBOR 仓库 API功能接口
1 项目管理 1.1 查看仓库中项目详细信息 curl -u "admin:Harbor12345" -X GET -H "Content-Type: ...
- 【OCR技术系列之三】大批量生成文字训练集
放假了,终于可以继续可以静下心写一写OCR方面的东西.上次谈到文字的切割,今天打算总结一下我们怎么得到用于训练的文字数据集.如果是想训练一个手写体识别的模型,用一些前人收集好的手写文字集就好了,比如中 ...
- Effective Java 第三版——31.使用限定通配符来增加API的灵活性
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- Zookeeper启动时报8080端口被占用
zookeeper启动时报8080 端口被占用,导致启动失败.特别是服务器上部署了tomcat服务时需要注意. 通过查看zookeeper的官方文档,发现有3种解决途径: (1)删除jetty. (2 ...