POJ-1962 Corporative Network

Corporative Network
Time Limit: 3000MS Memory Limit: 30000K
   

Description

A very big corporation is developing its corporative network. In the beginning each of the N enterprises of the corporation, numerated from 1 to N, organized its own computing and telecommunication center. Soon, for amelioration of the services, the corporation started to collect some enterprises in clusters, each of them served by a single computing and telecommunication center as follow. The corporation chose one of the existing centers I (serving the cluster A) and one of the enterprises J in some other cluster B (not necessarily the center) and link them with telecommunication line. The length of the line between the enterprises I and J is |I – J|(mod 1000).In such a way the two old clusters are joined in a new cluster, served by the center of the old cluster B. Unfortunately after each join the sum of the lengths of the lines linking an enterprise to its serving center could be changed and the end users would like to know what is the new length. Write a program to keep trace of the changes in the organization of the network that is able in each moment to answer the questions of the users.

Input

Your program has to be ready to solve more than one test case. The first line of the input will contains only the number T of the test cases. Each test will start with the number N of enterprises (5<=N<=20000). Then some number of lines (no more than 200000) will follow with one of the commands: 
E I – asking the length of the path from the enterprise I to its serving center in the moment; 
I I J – informing that the serving center I is linked to the enterprise J. 
The test case finishes with a line containing the word O. The I commands are less than N.

Output

The output should contain as many lines as the number of E commands in all test cases with a single number each – the asked sum of length of lines connecting the corresponding enterprise with its serving center.

Sample Input

1
4
E 3
I 3 1
E 3
I 1 2
E 3
I 2 4
E 3
O

Sample Output

0
2
3
5

Source

Southeastern Europe 2004

————————————————————上课的分割线————————————————————

思路:典型的并查集。一种操作“E”表示查询,要求输出u到父结点的距离。另一种操作“I”表示连接,将v作为u的父结点。一个并,一个查。只是需要多保存一个信息——结点与其父结点之间的距离。在进行路径压缩的时候,先暂存与父亲的距离,之后更新为该结点与父亲的距离加上父亲与爷爷的距离即可。
另外,本题的合并操作简化了,指明将v作为u的父亲并且u之前保证没有父亲,因此并不需要单写一个函数进行合并,只需要这样即可:
fa[u] = v;
d[u] = abs(u - v) % 1000;

代码如下:
/****************************************/
 #include <cstdio>
 #include <cstdlib>
 #include <cstring>
 #include <algorithm>
 #include <cmath>
 #include <stack>
 #include <queue>
 #include <vector>
 #include <map>
 #include <string>
 #include <iostream>
 using namespace std;
/****************************************/
const int N = 20010;
int fa[N], d[N];

int Find(int x)
{
	int tmp;
	if(x != fa[x]) {
		tmp = fa[x];//暂存于父结点的距离
		fa[x] = Find(tmp);//路径压缩,找到爷爷
		d[x] = d[x] + d[tmp];//每压缩掉一个父亲,将其与父亲的距离更新为【与其父亲距离+其父亲与其爷爷距离】,作为与新父亲的距离
		//这里不需要取模,因为题意只需在连接两个点的时候取模
	}
	return fa[x];
}

int main()
{
	int cas;
	scanf("%d", &cas);
	while(cas--) {
		int n, u, v;
		scanf("%d", &n);
		for(int i = 1; i <= n; i++) {
			fa[i] = i;
			d[i] = 0;
		}
		char op[9];
		while(scanf("%s", op) && op[0] != 'O') {
			if(op[0] == 'E') {
				scanf("%d", &u);
				Find(u);
				printf("%d\n", d[u]);
			}
			if(op[0] == 'I') {
				scanf("%d%d", &u, &v);
				fa[u] = v;
				d[u] = abs(u - v) % 1000;
			}
		}
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值