思路:记录每个点与其根结点的横向距离和纵向距离,当知道其父节点与根结点的关系,很容易推出当前节点与根结点的关系:

直接相加即可。

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 种类并查集的更多相关文章

  1. POJ1984 Navigation Nightmare —— 种类并查集

    题目链接:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K T ...

  2. POJ 1984 Navigation Nightmare 【经典带权并查集】

    任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K To ...

  3. poj 1182:食物链(种类并查集,食物链问题)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44168   Accepted: 12878 Description ...

  4. poj 1182 食物链(种类并查集 ‘初心者’)

    题目链接:http://poj.org/problem?id=1182 借着这题可以好好理解一下种类并查集,这题比较简单但挺经典的. 题意就不解释了,中问题. 关于种类并查集结局方法也是挺多的 1扩增 ...

  5. POJ 1984 Navigation Nightmare 带全并查集

    Navigation Nightmare   Description Farmer John's pastoral neighborhood has N farms (2 <= N <= ...

  6. POJ 1984 - Navigation Nightmare - [带权并查集]

    题目链接:http://poj.org/problem?id=1984 Time Limit: 2000MS Memory Limit: 30000K Case Time Limit: 1000MS ...

  7. POJ 1984 Navigation Nightmare(二维带权并查集)

    题目链接:http://poj.org/problem?id=1984 题目大意:有n个点,在平面上位于坐标点上,给出m关系F1  F2  L  D ,表示点F1往D方向走L距离到点F2,然后给出一系 ...

  8. poj 1984 Navigation Nightmare(带权并查集+小小的技巧)

    题目链接:http://poj.org/problem?id=1984 题意:题目是说给你n个线,并告知其方向,然后对于后面有一些询问,每个询问有一个时间点,要求你输出在该时间点a,b的笛卡尔距离,如 ...

  9. POJ 1182 食物链(种类并查集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63592   Accepted: 18670 Description ...

随机推荐

  1. junit3对比junit4

    本文内容摘自junit实战,感谢作者的无私奉献. 个人觉得每个开源包的版本对比意义不大,闲来无事,这里就来整理一下好了.本文名为junit3对比junit4,但是我通过这篇博客主要也是想统一的来整理下 ...

  2. 流API--流的迭代

    虽然流不是数据存储对象,但是仍然可以使用迭代器来遍历其元素,就如同使用迭代器遍历集合中的元素一样. 流API支持2类迭代器,一类是传统的Iterator,另一类是JDK8新增的Spliterator. ...

  3. 用powershell实现:“倩女幽魂姥姥”版《语音报警系统》

    ------[第一章 前言]------ win7,及以上版本中,是自带语音库的,系统自带一套女声中文库,一套女声英文库.用powershell调用,从而发音,制作报警系统.是一件太简单的事情,只需要 ...

  4. 【转】How to append current date and timestamp to filename in shell script

    $ date +"FORMAT" now=$(date +"%Y-%m-%d-%S") filename="my_program.$now.log&q ...

  5. python简单分布式demo

    A服务器是master,B服务器为worker, A服务器上执行taskManger.py # coding:utf-8 import random,time,Queue from multiproc ...

  6. 自定义Func模块

    自定义Func模块 (1)自定义模块步骤 (2)生成模块 [root@controller modules]# cd /usr/lib/python2.7/site-packages/func/min ...

  7. Java - 二叉树递归与非递归

    树的定义具有递归特性,因此用递归来遍历比较符合特性,但是用非递归方式就比较麻烦,主要是递归和栈的转换. import java.util.Stack; /** * @author 李文浩 * @ver ...

  8. 关于word图片显示不全

    问题:在编辑word时,在其中一行插入一张图片,但是显示不全. 原因:给文字行距设置成 [ 固定值 ]的原因. 解决方案:先删除图片,在插入图片的一行右键--> 段落,弹出对话框,找到设置行距的 ...

  9. c#代码启动任务管理器的几种方法

    1.直接启动 ProcessStartInfo info = new ProcessStartInfo(); info.FileName = Path.Combine(Environment.GetE ...

  10. Eclipse EE遇到问题记录

    Eclipse EE可以进行Java web的开发,下面记录了使用Eclipse EE调试时遇到的一些问题. 1.tomcat启动timeout的设置,双击server,主窗口就变为了server的配 ...