【暑假】[实用数据结构]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 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

-----------------------------------------------------------------------------------------------------------------------------------------------------------

思路:  操作只有询问到root距离以及改变父结点,因此可以用并查集实现。为每个结点添加信息d表示到父结点的距离,对于I操作,将d初始化为abs(u-v)%1000,对于每次询问E添加功能:压缩路径的同时改变d,即在p[u]=root的同时有d[u]=dist(u->root)

代码:
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define FOR(a,b,c) for(int a=(b);a<(c);a++)
 5 using namespace std;
 6 const int maxn= 20000 +10;
 7
 8 int p[maxn],d[maxn];
 9 int find_set(int u){
10 //寻找root结点->路径压缩+维护d
11     if(u==p[u]) return u;
12     else
13      {
14          int root=find_set(p[u]);
15          d[u] += d[p[u]];   //更新d为d->root的距离
16          return p[u]=root; //路径压缩
17      }
18 }
19
20 int main(){
21 int T,n;  scanf("%d",&T);
22   while (T--){
23       scanf("%d",&n);
24       FOR(i,1,n+1){ p[i]=i; d[i]=0; }
25       char ch[5];int x,y;
26       while(scanf("%s",ch) && ch[0]!=‘O‘){
27         if(ch[0]==‘E‘){ scanf("%d",&x);find_set(x);printf("%d\n",d[x]); }
28       else { scanf("%d%d",&x,&y); p[x]=y; d[x]=abs(x-y) % 1000; }
29       }
30   }
31   return 0;
32 }

				
时间: 2024-10-07 11:45:13

【暑假】[实用数据结构]UVAlive 3027 Corporative Network的相关文章

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

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

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

[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

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

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

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

【暑假】[实用数据结构]UVAlive 3026 Period

UVAlive 3026 Period 题目: Period Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive),