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 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.
Your program has to be ready to solve more than one test case.
Input
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

题意:没有更水,只有最水。。。

就是进行一系列操作,I把u的父节点设为v,权值是(v-u)%1000;

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
const int MAXN=20010;
int pre[MAXN];
int ans;
/*int find(int x){
return x==pre[x]?x:find(pre[x]);
}*/
void merge(int x,int y){
pre[x]=y;
}
int main(){
int T,N,a,b;
SI(T);
char s[2];
while(T--){
mem(pre,0);
SI(N);
while(scanf("%s",s),s[0]!='O'){
if(s[0]=='I'){
SI(a);SI(b);
merge(a,b);
}
else if(s[0]=='E'){
SI(a);ans=0;
while(a!=pre[a]&&pre[a]!=0){
ans+=(abs(a-pre[a])%1000);
a=pre[a];
}
printf("%d\n",ans);
}
}
}
return 0;
}

  

3027 - Corporative Network(并差集)的更多相关文章

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

    UVAlive 3027 Corporative Network 题目:   Corporative Network Time Limit: 3000MS   Memory Limit: 30000K ...

  2. 3027 - Corporative Network

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

  3. UVALive 3027 Corporative Network

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

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

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

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

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

  6. UVALive 3027 Corporative Network 带权并查集

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

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

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

  8. 并查集(路径更新) LA 3027 Corporative Network

    题目传送门 题意:训练指南P192 分析:主要就是一个在路径压缩的过程中,更新点i到根的距离 #include <bits/stdc++.h> using namespace std; c ...

  9. LA 3027 Corporative Network

    这题感觉和 POJ 1988 Cube Stacking 很像,在路径压缩的同时递归出来的时候跟新distant数组 我发现我一直WA的原因是,命令结束是以字母o结束的,而不是数字0!! //#def ...

随机推荐

  1. JSP page include taglib

    page include taglib 语法:<%@ 指令名称 属性=值 属性=值 -%> ------------------- page 1.language 默认值java 2.ex ...

  2. 拔一拔 ExtJS 3.4 里你遇到的没遇到的 BUG(1)

    本文从今天开始,我要做的就是不断的更新,不断的披露ExtJS 3.4的BUG并修复它.需要注意的是版本为3.4而不是4.0,因为4.0改动和变化比较大,所以不要对号入座. 嘿嘿,本人不怎么写东西,不过 ...

  3. LINQ to Entity Framework 操作符(转)

    在开始了解LINQ to Entities之前,需要先对.NET Framework 3.5版本后对C#语言的几个扩展特性做一些阐释,这有助于我们更容易.更深刻的理解LINQ to Entities技 ...

  4. [Java]使用队列求解josephus问题

    约瑟夫斯问题(有时也称为约瑟夫斯置换),是一个出现在计算机科学和数学中的问题.在计算机编程的算法中,类似问题又称为约瑟夫环. 有个囚犯站成一个圆圈,准备处决.首先从一个人开始,越过个人(因为第一个人已 ...

  5. 转:批处理for命令详解

    批处理for命令详解FOR这条命令基本上都被用来处理文本,但还有其他一些好用的功能!看看他的基本格式(这里我引用的是批处理中的格式,直接在命令行只需要一个%号)FOR 参数 %%变量名 IN (相关文 ...

  6. xmemcached的time out

    最近维护线上发现不停有java.util.concurrent.TimeoutException: Timed out(200) waiting for operation的问题,排查程序.配置文件的 ...

  7. react-native 入门资源合集

    # 了解react-native React Native enables you to build world-class application experiences on native pla ...

  8. MZL's xor

    MZL's xor Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) Total Sub ...

  9. Glog

    Glog的简单入门,glog虽然在配置参数方面比较麻烦,但是在小规模程序中,由于其简单灵活,也许会有优势. 0,  glog 是google的开源日志系统,相比较log4系列的日志系统,它更加轻巧灵活 ...

  10. iOS 监听键盘变化

    //将要显示键盘 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willShowKeyboard: ...