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

题解:感觉题意好难啊,我靠尼玛。原来是i->j的距离是abs(i - j) % 1000,i的长度=i->j的长度 + j ->j的中心的长度(题意有说吗??你妹的,反正我是没看出来)。还有i肯定是i所在集合的根,这里很重要啊。就是在找父节点的时候把长度加上去就行了。题意太你妹难了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath> 

using namespace std;

int pre[20004];
int d[20004];

int find(int x)
{
	if(x == pre[x])
	{
		return x;
	}

	int fa = pre[x];
	pre[x] = find(pre[x]);
	d[x] += d[fa];
	return pre[x];
}

int main()
{
	int ncase;
	cin>>ncase;
	while(ncase--)
	{
		int n;
		scanf("%d",&n);
		char s[10];
		int u,v;
		for(int i = 1;i <= n;i++)
		{
			pre[i] = i;
			d[i] = 0;
		}
		while(scanf("%s",s) && s[0] != 'O')
		{
			if(s[0] == 'E')
			{
				scanf("%d",&u);
				find(u);
				printf("%d\n",d[u]);
			}
			else
			{
				scanf("%d%d",&u,&v);
				d[u] = abs(u - v) % 1000;
				int x = find(v);
				d[u] += d[v];
				pre[u] = x;
			}
		}
	}

	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-07 11:36:29

Corporative Network的相关文章

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

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

Corporative Network(带权并查集)

这个题的题意是  当输入'E'是查找操作,查找从后面这个数到他的父亲这边的值,'I'代表把后面的数作为前面数的父亲 然后他们两个的差值代表这两个边的权值 水水的题 #include <stdio.h> #include <string.h> int par[20005]; int rank1[20005]; int abs(int hh) { return (hh>0)?hh:-hh; } void init() { for(int i=0;i<20005;i++) {

UVa 1329 - Corporative Network Union Find题解

UVa的题目好多,本题是数据结构的运用,就是Union Find并查集的运用.主要使用路径压缩.甚至不需要合并树了,因为没有重复的连线和修改单亲节点的操作. 郁闷的就是不太熟悉这个Oj系统,居然使用库中的abs就会WA,自己写了个abs小函数就过了. 题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4075 #include <s

[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

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,

(DS 《算法竞赛入门经典》)LA 3027 Corporative Network(查询某一个节点到根节点之间的距离)

题目大意: 查询某一个节点到根节点之间的距离 解题思路: 加权并查集问题.之前做的题目是"查看两个或多个节点是否在同一个集合下",现在的题目是"查询某个节点到 根节点之间的距离".之前只需要使用到father[x]这个数组,用来表示x的父亲节点是谁.现在引入dist[x]数组,用来记录 x节点到根节点的距离 1)在并查集中,根节点不懂,其他节点都可以动. A very big corporation is developing its corporative net

[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

【并查集】UVALive3027 Corporative Network

[并查集]UVALive3027 Corporative Network 并查集--维护到根节点距离的d数组 题目大意 对n个节点操作,加边 or 询问某节点到根节点的距离 说一下思路 之前做过一道求连通分支最大元素个数的题目,维护的是一个cnt[ ]数组(在加边的过程中):比较这道题,可以考虑维护到根节点的距离d[ ]数组. 思路:记下每个节点到父亲节点的距离为d[i],然后在路径压缩时维护这个d数组: 在加边时只有这两个节点中的父亲节点的d需要维护,并不需要查操作,这一点的区别是相当大的!而