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. 1
  2. 4
  3. E 3
  4. I 3 1
  5. E 3
  6. I 1 2
  7. E 3
  8. I 2 4
  9. E 3
  10. O

Sample Output

  1. 0
  2. 2
  3. 3
  4. 5
  5.  
  6. -----------------------------------------------------------------------------------------------------------------------------------------------------------
  7.  
  8. 思路:
    操作只有询问到root距离以及改变父结点,因此可以用并查集实现。为每个结点添加信息d表示到父结点的距离,对于I操作,将d初始化为abs(u-v)%1000,对于每次询问E添加功能:压缩路径的同时改变d,即在p[u]=root的同时有d[u]=dist(u->root)
  9.  
  10. 代码:
  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= +;
  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,,n+){ p[i]=i; d[i]=; }
  25. char ch[];int x,y;
  26. while(scanf("%s",ch) && ch[]!='O'){
  27. if(ch[]=='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) % ; }
  29. }
  30. }
  31. return ;
  32. }
  1.  
  1.  

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

  1. UVALive 3027 Corporative Network

    ---恢复内容开始--- Corporative Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld ...

  2. UVALive 3027 Corporative Network 带权并查集

                         Corporative Network A very big corporation is developing its corporative networ ...

  3. 并查集 + 路径压缩(经典) UVALive 3027 Corporative Network

    Corporative Network Problem's Link Mean: 有n个结点,一开始所有结点都是相互独立的,有两种操作: I u v:把v设为u的父节点,edge(u,v)的距离为ab ...

  4. UVALive - 3027 Corporative Network (并查集)

    这题比较简单,注意路径压缩即可. AC代码 //#define LOCAL #include <stdio.h> #include <algorithm> using name ...

  5. UVALive 3027 Corporative Network (带权并查集)

    题意: 有 n 个节点,初始时每个节点的父节点都不存在,你的任务是执行一次 I 操作 和 E 操作,含义如下: I  u  v   :  把节点 u  的父节点设为 v  ,距离为| u - v | ...

  6. 3027 - Corporative Network(并差集)

    3027 - Corporative Network A very big corporation is developing its corporative network. In the begi ...

  7. 3027 - Corporative Network

    3027 - Corporative Network 思路:并查集: cost记录当前点到根节点的距离,每次合并时路径压缩将cost更新. 1 #include<stdio.h> 2 #i ...

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

    A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...

  9. LA 3027 Corporative Network 并查集记录点到根的距离

    Corporative Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [S ...

随机推荐

  1. DX 的.x 文件

    template Header { <3D82AB43-62DA-11cf-AB39-0020AF71E433> WORD major; WORD minor; DWORD flags;} ...

  2. 关于ref与out的区别

    写在最前面 这几天一直在公司接受培训,都是一些基础的知识,同时也乘着这个机会巩固一下自己的基础,基础太重要了.前些时一直看的是多线程方面的知识,接下来我会写一些其他方面的知识,毕竟作为一个实习新人得和 ...

  3. 论反馈信息如何推动 IT 运维团队进步?

    我们还记得<快乐大本营>中经典游戏----快乐传真吗?游戏规则是:很多人站一排,只有第一个人才看到最准确的信息,用东西隔着,戴耳机,一一将从前一个人获得的信息传递下去,最后一个人说出推测的 ...

  4. c/c++ define用法

    define,无参宏定义的一般形式为:#define 标识符 字符串 外文名 define 词条范围 计算机专业用语 无参一般形式 #define 标识符 字符串 带参一般形式 #define 宏名( ...

  5. 【前端学习】【CSS选择器】

    CSS选择器     CSS选择器 CSS(Cascading Style Sheets)是一项出色的技术,它使得网页的结构和表现样式完全分离.利用CSS选择器能轻松地对某个元素添加样式而不改动HTM ...

  6. 使用头文件climits中的符号常量获知整型数据的表数范围---gyy整理

    在头文件climits(limits.h)以宏定义的方式定义了各种符号常量来表示各种整型类型表示数的范围,如int的最大最小值,long的最大最小值等. 符号常量 表示 CHAR_BIT char 的 ...

  7. Qt: 访问容器(三种方法,加上for循环就四种了)good

    #include <iostream>#include <QString>#include <QList>#include <QListIterator> ...

  8. QT插件开发方式(作者有RemOjbects文档翻译(48)篇)

    创建一个QT的库项目,删除自动生成的.h和.cpp文件,添加一个接口定义.h文件和一个接口实现类(一个.h一个.cpp).代码如下: 1.接口文件源码 #ifndef PLUGININTERFACE_ ...

  9. Qt之QtSoap(访问WebService)

    http://blog.csdn.net/u011012932/article/details/51673800

  10. QListWidget代码刷新界面

    我有一个特殊效果要求实现(其实很弱智,也变成特殊效果,汗一下自己):两个QRadioButton切换的时候,让旁边的QListWidget自动变化不同的背景色.想了很多办法: 1. 控件自己刷新,不行 ...