UVALive 3027 Corporative Network 并查集水题

题意:有n个数进行,操作,'I'操作把a和b连接起来,而且a和b的距离即|a-b|%1000;'E'操作进行查询那个数到根节点的距离和;'O'操作结束。

没有压缩路径的并查集,所以我没有写递归函数直接循环。

由于E操作要的是a到根节点的距离和,计算时不需要取余,被坑了好几次。。。

代码:

/*
*  Author:      illuz <iilluzen[at]gmail.com>
*  Blog:        http://blog.csdn.net/hcbbt
*  File:        uvalive3027.cpp
*  Create Date: 2013-12-05 23:15:59
*  Descripton:  union set
*/

#include <cstdio>
#include <cstdlib>

const int MAXN = 2e4 + 10;

int f[MAXN], n, a, b;
char op[3];

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);

		for (int i = 0; i <= n; i++)
			f[i] = i;

		while (scanf("%s", op) && op[0] != 'O')
			if (op[0] == 'E') {
				scanf("%d", &a);
				b = 0;
				while (f[a] != a) {
					b += (abs(a - f[a])) % 1000;
					a = f[a];
				}
				printf("%d\n", b);
			} else if (op[0] == 'I') {
				scanf("%d%d", &a, &b);
				f[a] = b;
			}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值