3027 - Corporative Network(并差集)
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(并差集)的更多相关文章
- 【暑假】[实用数据结构]UVAlive 3027 Corporative Network
UVAlive 3027 Corporative Network 题目: Corporative Network Time Limit: 3000MS Memory Limit: 30000K ...
- 3027 - Corporative Network
3027 - Corporative Network 思路:并查集: cost记录当前点到根节点的距离,每次合并时路径压缩将cost更新. 1 #include<stdio.h> 2 #i ...
- UVALive 3027 Corporative Network
---恢复内容开始--- Corporative Network Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld ...
- [LA] 3027 - Corporative Network [并查集]
A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...
- LA 3027 Corporative Network 并查集记录点到根的距离
Corporative Network Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [S ...
- UVALive 3027 Corporative Network 带权并查集
Corporative Network A very big corporation is developing its corporative networ ...
- 并查集 + 路径压缩(经典) UVALive 3027 Corporative Network
Corporative Network Problem's Link Mean: 有n个结点,一开始所有结点都是相互独立的,有两种操作: I u v:把v设为u的父节点,edge(u,v)的距离为ab ...
- 并查集(路径更新) LA 3027 Corporative Network
题目传送门 题意:训练指南P192 分析:主要就是一个在路径压缩的过程中,更新点i到根的距离 #include <bits/stdc++.h> using namespace std; c ...
- LA 3027 Corporative Network
这题感觉和 POJ 1988 Cube Stacking 很像,在路径压缩的同时递归出来的时候跟新distant数组 我发现我一直WA的原因是,命令结束是以字母o结束的,而不是数字0!! //#def ...
随机推荐
- ROS验证publisher和subscriber
在前面的两篇博客中我们用C++在ROS中创建了一个发布者和接收者,并使用catkin_make构建了新的节点,下面就需要验证一下,我们写的是否正确. 首先运行roscore roscore 在使用ca ...
- 亲测的一款在线作图神器:ProcessOn
本人近日发现一款作图神器:ProcessOn 它是一款在线的作图工具,完全国产,前台是用HTML5 Canvas加javascript做绘图,后台用java实现数据处理和图片生成, 整站UI基本类似 ...
- Android Listview切换动画,扩展到任意view切换之间动画实现
添加布局如下: <?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2010 ...
- Linux 安装g++
g++ 它的全名不叫g++而是叫gcc-c++; 所以要安装它就可以用 yum install gcc-c++;
- 阿里云ECS每天一件事D9:nginx1.7整合tomcat8.0
仅通过8080端口访问jsp显然不是一算是一个太好的方法,可以使用nginx的proxy_pass子模块,实现nginx转发jsp请求至tomcat. 典型的配置如下: server { listen ...
- U盘中的闪存白片与黑片
简单的说,黑片的概念主要用于芯片,白片的概念既用于芯片也用于闪存卡.黑片就是指芯片工厂选出的淘汰的次品,没有打上工厂标和芯片型号的芯片,这样的芯片都经过个种渠道流通到市场上来,现在很多U盘大厂大量的采 ...
- SSD常见问题的技术分析
AHCI对性能的影响 AHCI,全称Advanced Host Controller Interface,即高级主机控制器接口,是一种相比老旧的“IDE虚拟模式”更适合新一代SATA存储设备通信的协议 ...
- HttpUrlConnection get和post简单实现(疑惑解决)
近期研究微信的公众平台开发.须要和微信的server进行数据读取,简单研究了下jdk自带的HttpUrlConnection类(URLConnection的子类),简单实现了一下微信的access_t ...
- MD5的加密和解密(总结)
效果图例如以下: package com.test; import java.security.MessageDigest; public class MD5 { // MD5加码.32位 publi ...
- PDFium-PDF开源之旅(1)-初探代码下载编译
近日,Google和Foxit合作开源了Foxit的PDF源代码. 叫PDFium 相关新闻不少.哈,虽说已经不是程序猿了.只是还是有兴趣跑起来围观看看.废话少说.先说编译代码的步骤(事实上Googl ...