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. fatal error LNK1123: failure during conversion to COFF: file invalid or corr

    新装VS2010出现标题的错误,使用了下面的方法,不行 这个是由于日志文件引起的,可以将 项目\属性\配置属性\清单工具\输入和输出\嵌入清单:原来是"是",改成"否&q ...

  2. HDU1159-Common Subsequence

    描述: A subsequence of a given sequence is the given sequence with some elements (possible none) left ...

  3. Android ActionBar详解(二)--->使用ActionBar显示选项菜单

    MainActivity如下: package cc.testsimpleactionbar1; import android.os.Bundle; import android.app.Activi ...

  4. python 学习之Windows 下的编码处理!

    问题1: Non-ASCII character '\xe9' in file 问题原因:程序编码上出现问题 解决方法:在程序头部加上代码 #-*- coding: UTF-8 -*- 设置代码编码为 ...

  5. 查看、修改oracle字符集,查看oracle版本

    1.查看数据库字符集 数据库服务器字符集select * from nls_database_parameters,其来源于props$,是表示数据库的字符集. 客户端字符集环境select * fr ...

  6. Android studio之更改快捷键及自动导包

    更改AS中的代码提示快捷键,AS做的也挺智能的,在Keymap中可以选择使用eclipse的快捷键设置,但是虽然设置了,对有些快捷键还是不能使用,那么就需要我们手动去修改了. 在代码提示AS默认的快捷 ...

  7. Spring Boot MyBatis 连接数据库

    最近比较忙,没来得及抽时间把MyBatis的集成发出来,其实mybatis官网在2015年11月底就已经发布了对SpringBoot集成的Release版本,Github上有代码:https://gi ...

  8. [置顶] SpecDD(混合的敏捷方法模型)主要过程概述

    敏捷已成为当今使用最广泛的开发方法.有趣的是,敏捷方法的流行性并不是因为它取代了其他开发方法,相反它与这些方法进行了更好地融合.现实世界众多敏捷项目的成功,也证明了敏捷将走向杂化的未来. SpecDD ...

  9. Java图形化界面设计——布局管理器之BorderLayout(边界布局)

  10. Android开发(26)--补间动画(Tween)的实现

    补间动画(Tween Animation) 补间动画与逐帧动画在本质上是不同的,逐帧动画通过连续播放图片来模拟动画的效果,而补间动画则是通过在两个关键帧之间补充渐变的动画效果来实现的.补间动画的优点是 ...