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
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

【题意】给出t个例子,有n个结点,刚开始结点以自己为终点,而后I后给出两个x,y,表示x指向y,E后给出一个x,问x的终点与他的距离mod1000;

【解法】并查集

#include<iostream>
#include<math.h>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=; int t,n,x,y;
char str[];
int fa[N],dis[N];
int findx(int x)
{
if(x==fa[x])
return x;
int fx=findx(fa[x]);
dis[x]+=dis[fa[x]];
return fa[x]=fx;
}
void merge(int x,int y)
{
int fx=findx(x),fy=findx(y);
if(fx==fy)
{
return ;
}
fa[x]=x;dis[fx]=dis[x];
fa[x]=y;
dis[x]=abs(x-y)%;
}
void init()
{
for(int i=;i<=n;i++)
{
dis[i]=;
fa[i]=i;
}
}
int main()
{ scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
init(); while(~scanf("%s",str))
{
if(str[]=='O') break;
else if(str[]=='E')
{
scanf("%d",&x);
findx(x);
printf("%d\n",dis[x]);
}
else
{
scanf("%d%d",&x,&y);
merge(x,y);
}
}
}
return ;
}

Corporative Network_并查集的更多相关文章

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

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

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

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

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

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

  4. UVALive 3027 Corporative Network 带权并查集

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

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

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

  6. Corporative Network (有n个节点,然后执行I u,v(把u的父节点设为v)和E u(询问u到根节点的距离))并查集

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

  7. POJ1962:Corporative Network【带权并查集】

    <题目链接> 题目大意: n个节点,若干次询问,I x y表示从x连一条边到y,权值为|x-y|%1000:E x表示询问x到x所指向的终点的距离.   解题分析: 与普通的带权并查集类似 ...

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

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

  9. UVA 3027 Corporative Network 带权并查集、

    题意:一个企业要去收购一些公司把,使的每个企业之间互联,刚开始每个公司互相独立 给出n个公司,两种操作 E I:询问I到I它连接点最后一个公司的距离 I I J:将I公司指向J公司,也就是J公司是I公 ...

随机推荐

  1. hdu 4405Aeroplane chess(概率DP)

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. Xcode 8 打包上线 iTunes Connect 找不到构建版本

    Xcode 8 打包上线 iTunes Connect 找不到构建版本 最近苹果推出新的mac操作系统(macOS Sierra 10.12),大家可能都已经升级了,作为一个开发者,小编肯定是第一时间 ...

  3. android.support.v4.app.Fragment和android.app.Fragment区别

    1.最低支持版本不同 android.app.Fragment 兼容的最低版本是android:minSdkVersion="11" 即3.0版 android.support.v ...

  4. 转载:javascript面向对象编程:非构造函数的继承

    一.什么是"非构造函数"的继承? 比如,现在有一个对象,叫做"中国人". var Chinese = { nation:'中国' }; 还有一个对象,叫做&qu ...

  5. group_concat函数与find_in_set()函数相结合

    一:group_concat函数详解 1.语法如下: group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'] ...

  6. web自定义控件UserControl

    今天做了两个自定义控件,之前用WPF也做过,但是感觉跟今天的不太一样.首先是在项目中建了一个UserContral的控件界面,把需要的控件拖到里面,再给按钮添加事件.我们公司的控件都是买的Dev Ex ...

  7. ANGULAR JS PROMISE使用

    Promise是一种模式,以同步操作的流程形式来操作异步事件,避免了层层嵌套,可以链式操作异步事件. 我们知道,在编写javascript异步代码时,callback是最最简单的机制,可是用这种机制的 ...

  8. Servlet页面跳转实现方法的区别

    一直对Servlet页面跳转的几种方式理解的糊里糊涂的,今天在网上搜了一把,找到一遍比较好的,记下来,以后看看. Servlet页面跳转分两部分,一是发生在Servlet,一是在JSP,其实JSP也就 ...

  9. [示例]NSDictionary编程题-字典的排序应用(iOS7班)

    代码: #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepo ...

  10. jQuery中的quickExpr

    jQuery 源码中的 quickExpr 是用来检测参数 selector 是否是复杂的 HTML 代码(如“abc<div>”)或 #id,匹配结果存放在数组 match 中 // A ...