[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 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 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 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 题解:并查集。注意:由于本题的合并操作指定了父节点和子节点,所以不能使用启发式合并。不使用路径压缩能AC。若使用路径压缩,需要在压缩时维护每个节点到父节点的距离d[i],合并时更新。 代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#include<stdlib.h>
#include<stdbool.h> #define rep(i,a,b) for(i=(a);i<=(b);i++)
#define clr(x,y) memset(x,y,sizeof(x))
#define sqr(x) (x*x) int i,j,n,
father[],d[]; void pre()
{
clr(father,);
clr(d,);
} int find(int x)
{
int r,k,p,sum=; k=x;
while(k!=father[k]) {
sum+=abs(k-father[k])%;
k=father[k];
} return sum;
} int main()
{
int i,T,n,x,y,f1;
char ch;
scanf("%d",&T);
while(T--) {
pre();
scanf("%d",&n);
rep(i,,n) father[i]=i; while(true) {
scanf("%c",&ch);
if(ch=='O') break;
if(ch=='E') {
scanf("%d",&x);
f1=find(x);
printf("%d\n",f1);
}
if(ch=='I') {
scanf("%d%d",&x,&y);
father[x]=y;
} } } return ;
}
[LA] 3027 - Corporative Network [并查集]的更多相关文章
- LA 3027 Corporative Network 并查集记录点到根的距离
Corporative Network Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [S ...
- UVALive - 3027 Corporative Network (并查集)
这题比较简单,注意路径压缩即可. AC代码 //#define LOCAL #include <stdio.h> #include <algorithm> using name ...
- 【LA 3027 Corporative Network】
·一些很可爱的询问和修改,放松地去用并查集解决. ·英文题,述大意: 输入n(5<=n<=20000)表示树有n个节点,并且会EOF结束地读入不超过 20000个操作,一共有两种: ...
- 并查集(路径更新) 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 ...
- 【暑假】[实用数据结构]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 ...
- 3027 - Corporative Network(并差集)
3027 - Corporative Network A very big corporation is developing its corporative network. In the begi ...
- UVALive 3027 Corporative Network 带权并查集
Corporative Network A very big corporation is developing its corporative networ ...
随机推荐
- pfsense下的流量管理(转)
http://www.pppei.net/blog/post/331 在作流量管理时,这些概念很重要,不要迷失.. 这里再对Limiter 的源地址和目的地址做个说明,因为limiter是被应用在La ...
- Linux开发工具的使用
1. Linux开发工具的使用 Vim编译的使用 Gdb调试工具的使用 Makefile的编写 linux跟踪调试 SSH的使用 subversion的使用 1. Linux开发工具的使用 V ...
- Linux CentOS 7 YouCompleteMe相关配置。
CentOS 6.5上面弄了2天,以失败告终!!!当作练手了.在网上看到一篇CentOS7.0上安装YouCompleteMe插件的文章,就重新在虚拟机上安装了一个CentOS7,按那个文章执行了一下 ...
- HDU--3487 Play with Chain (Splay伸展树)
Play with Chain Problem Description YaoYao is fond of playing his chains. He has a chain containing ...
- samsungGalaxyS4USB驱动
http://www.samsung.com/cn/support/usefulsoftware/KIES/JSP
- 页面onclick()中传值问题
html中onclick()里面传变量到javascript中的问题,小小的记录下: 传变量的话一定要加 '' <span onclick="sellGoods('${session ...
- phpcms:一、安装及新建模板
1.复制D:\WWW\phpcms\phpcms\templates\目录下的default文件粘贴在当前目录下,并重命名为新模板名字(youpinzhiyuan2012) 2.打开D:\WWW\ph ...
- Thinkphp显示系统常量信息的方法(php的用法)
输入 :public function Main() { dump(get_defined_constants(true)); }显示系统信息, 其中: 'APP_PATH' ...
- Quartz集成springMVC 的方案二(持久化任务、集群和分布式)
Quartz是一个开放源码项目,专注于任务调度器,提供了极为广泛的特性如持久化任务,集群和分布式任务等. Quartz核心是调度器,还采用多线程管理. 1.持久化任务:当应用程序停止运行时,所有调度信 ...
- mybatis 调用mysql存储过程 带输出输入参数
http://lohasle.iteye.com/blog/1669879 存储过程都是一样的,只是根据自己的喜好,可以用MAP或者JAVABEAN传递参数. -- ----------------- ...