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

直接相加即可。

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. SVN中服务器地址变更

    SVN中服务器地址变更后不需要重新导项目,只要修改下SVN的服务器地址,更新一下即可.有两种方法: 方法一:通过MyEclipse中SVN插件 1.选择window→show view→other→S ...

  2. git clone代码时候出现的报错

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px "Andale Mono"; color: #28fe14; backgr ...

  3. [JLOI2011]基因补全

    1973: [JLOI2011]基因补全 Time Limit: 1 Sec  Memory Limit: 256 MB Description 在生物课中我们学过,碱基组成了DNA(脱氧核糖核酸), ...

  4. tomcat无法打开8080页面

    tomcat已启动 app已经正常执行 但不能打开8080管理页面 可能是在webapps目录下没有ROOT目录

  5. Python字符串详解

    字符串 作用: 名字,性别,国籍,地址等描述信息 定义: 在单引号.双引号.三引号内,由一串字符组成 优先掌握的操作: 按索引取值(正向取+反向取):只能取 切片(顾头不顾尾,步长) 长度len 成员 ...

  6. 通过traceroute追踪并打印成图片

    #!/usr/bin/evn python #-*-coding:utf-8 -*- import time import logging,warnings import subprocess imp ...

  7. AppScan扫描结果分析及工具栏使用

    Appscan的窗口大概分三个模块,Application Links(应用链接), Security Issues(安全问题), and Analysis(分析) Application Links ...

  8. iOS-Runtime之关于页面跳转的捷径【Runtime获取当前ViewController】

    写在前面 在我们操作页面跳转时,如果当前的类不是UIViewcontroller(下面用VC表示),你会不会写一个代理,或者block给VC传递信息,然后在VC里面进行 ///假如targetVc是将 ...

  9. Java:对象的强、软、弱和虚引用[转]

    原文链接:http://zhangjunhd.blog.51cto.com/113473/53092/ 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法 ...

  10. Java对正则表达式的支持(二)

    正则表达式的主要用途: a.在目标字符串中找出匹配正则表达式的部分 b.校验目标字符串是否符合正则表达式,例如校验邮箱地址 c.在目标字符串中替换符合正则表达式的部分为其他的字符串 Scanner类是 ...