UVALive - 3027 - Corporative Network (并查集!!)

UVALive - 3027

Corporative Network

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

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 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 file 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

Regionals 2004 >> Europe
- Southeastern

昨天不知道怎么搞的,,网站崩溃了一天。。

并查集!!

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>		//用于使用abs
using namespace std;

const int maxn = 20000 + 10;
int pa[maxn], d[maxn];

int find(int x)
{			//路径压缩,同时维护d[i]:结点i到树根的距离
	if(pa[x] != x)
	{
		int root = find(pa[x]);
		d[x] += d[pa[x]];
		return pa[x] = root;
	}
	else return x;
}

int main()
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		int n, u, v;
		char cmd[9];
		scanf("%d", &n);
		for(int i=0; i <= n; i++) { pa[i] = i; d[i] = 0; }		//初始化,每个结点单独是一棵树 

		while(scanf("%s", cmd) && cmd[0] != 'O')
		{
			if(cmd[0] == 'E')
			{
				scanf("%d", &u);
				find(u);				//调用find函数,其实是维护d[i]的值
				printf("%d\n", d[u]);
			}
			if(cmd[0] == 'I')
			{
				scanf("%d %d", &u, &v);
				pa[u] = v; d[u] = abs(u - v) % 1000;
			}
		}
	}
	return 0;
} 
时间: 2024-08-28 05:36:36

UVALive - 3027 - Corporative Network (并查集!!)的相关文章

[LA] 3027 - Corporative Network [并查集]

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

LA 3027 Corporative Network(并查集,求某个节点到根节点的距离)

A very big corporation is developing its corporative network. In the beginning each of the N enterprisesof the corporation, numerated from 1 to N, organized its own computing and telecommunication center.Soon, for amelioration of the services, the co

[2016-03-19][UVALive][3027][Corporative Network]

时间:2016-03-19 13:24:23 星期六 题目编号:[2016-03-19][UVALive][3027][Corporative Network] 题目大意:给定n个节点,I u v表示把u节点的父节点设置为v,距离为|u-v|%1000,E u表示询问u到根节点的距离,给定若干个I E操作,输出相应答案 分析:带权并查集 方法:d[maxn] 维护到父节点的距离,每次询问的时候,把当前节点压缩到根节点输出对应距离即可 #ifdef _WORK_ #include <algorit

【暑假】[实用数据结构]UVAlive 3027 Corporative Network

UVAlive 3027 Corporative Network 题目:   Corporative Network Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 3450   Accepted: 1259 Description A very big corporation is developing its corporative network. In the beginning each of the N ent

UVALive 3027 Corporative Network 带权并查集

                     Corporative Network A very big corporation is developing its corporative network. In the beginning each of the N enterprisesof the corporation, numerated from 1 to N, organized its own computing and telecommunication center.Soon,

UVALive 3027 Corporative Network(并查集之四)

问题描述: 求出点到根的距离,带权并查集. 代码思路: 在 I 命令的执行过程中,并不路径压缩. 当执行 E 查询命令时在路径压缩的同时递归求解存储在dist数组里面. 代码: #include<stdio.h> #include<math.h> #include<stdlib.h> #include<string.h> #define MOD 1000 #define MAX 20000 int pre[MAX+7],dist[MAX+7]; int fi

POJ1962:Corporative Network(并查集)

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 se

3027 - Corporative Network

3027 - Corporative Network 思路:并查集: cost记录当前点到根节点的距离,每次合并时路径压缩将cost更新. 1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<string.h> 5 #include<math.h> 6 #include<stack> 7 #include<set> 8 #inc

UVALive 4487 Exclusive-OR 加权并查集神题

已知有 x[0-(n-1)],但是不知道具体的值,题目给定的信息 只有 I P V,说明 Xp=V,或者 I P Q V,说明 Xp ^ Xq=v,然后要求回答每个询问,询问的是 某任意的序列值 Xp1^Xp2,,,,X^pk 这个题目用加权并查集是这么处理的: 1. f[]照样是代表父节点,照样进行路径压缩,把每个 V[i]=V[i]^V[f[i]],即节点存储的值实际是它与它父亲的异或的值.为什么要这样呢,因为异或首先满足交换律,而且异或同一个数偶数次,即相当于本身,那么这个题目的其中一个要