Corporative Network_并查集

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=20005;

int t,n,x,y;
char str[5];
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)%1000;
}
void init()
{
    for(int i=0;i<=n;i++)
    {
        dis[i]=0;
        fa[i]=i;
    }
}
int main()
{

    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        init();

        while(~scanf("%s",str))
        {
            if(str[0]==‘O‘) break;
            else if(str[0]==‘E‘)
            {
                scanf("%d",&x);
                findx(x);
                printf("%d\n",dis[x]);
            }
            else
            {
                scanf("%d%d",&x,&y);
                merge(x,y);
            }
        }
    }
    return 0;
}
时间: 2024-11-18 14:41:48

Corporative Network_并查集的相关文章

[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

POJ1962:Corporative Network(并查集)

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 se

LA 3027 Corporative Network(并查集,求某个节点到根节点的距离)

A very big corporation is developing its corporative network. In the beginning each of the N enterprisesof the corporation, numerated from 1 to N, organized its own computing and telecommunication center.Soon, for amelioration of the services, the co

【并查集】UVALive3027 Corporative Network

[并查集]UVALive3027 Corporative Network 并查集--维护到根节点距离的d数组 题目大意 对n个节点操作,加边 or 询问某节点到根节点的距离 说一下思路 之前做过一道求连通分支最大元素个数的题目,维护的是一个cnt[ ]数组(在加边的过程中):比较这道题,可以考虑维护到根节点的距离d[ ]数组. 思路:记下每个节点到父亲节点的距离为d[i],然后在路径压缩时维护这个d数组: 在加边时只有这两个节点中的父亲节点的d需要维护,并不需要查操作,这一点的区别是相当大的!而

Corporative Network(带权并查集)

这个题的题意是  当输入'E'是查找操作,查找从后面这个数到他的父亲这边的值,'I'代表把后面的数作为前面数的父亲 然后他们两个的差值代表这两个边的权值 水水的题 #include <stdio.h> #include <string.h> int par[20005]; int rank1[20005]; int abs(int hh) { return (hh>0)?hh:-hh; } void init() { for(int i=0;i<20005;i++) {

UVALive 3027 Corporative Network 带权并查集

                     Corporative Network A very big corporation is developing its corporative network. In the beginning each of the N enterprisesof the corporation, numerated from 1 to N, organized its own computing and telecommunication center.Soon,

UVALive - 3027 - Corporative Network (并查集!!)

UVALive - 3027 Corporative Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A very big corporation is developing its corporative network. In the beginning each of the N enterprises of the cor

UVA 3027 Corporative Network 带权并查集、

题意:一个企业要去收购一些公司把,使的每个企业之间互联,刚开始每个公司互相独立 给出n个公司,两种操作 E I:询问I到I它连接点最后一个公司的距离 I I J:将I公司指向J公司,也就是J公司是I公司的上级,距离为abs(I-J)%1000(貌似G++不支持abs,PE了两发) 思路:转化一下题意就行了,首先刚开始的时候每个公司都是独立的,I操作就是并查集中合并操作,将I这课树并到J这个树上, E操作要求的东西就是 I到I的根节点的距离,先看一个没有路径压缩直接暴力的方法把.(本以为不会过的,

UVALive 3027 Corporative Network(并查集之四)

问题描述: 求出点到根的距离,带权并查集. 代码思路: 在 I 命令的执行过程中,并不路径压缩. 当执行 E 查询命令时在路径压缩的同时递归求解存储在dist数组里面. 代码: #include<stdio.h> #include<math.h> #include<stdlib.h> #include<string.h> #define MOD 1000 #define MAX 20000 int pre[MAX+7],dist[MAX+7]; int fi