poj 1962 Corporative Network (并查集)

来源:互联网 发布:美国对中国的误解知乎 编辑:程序博客网 时间:2024/06/03 07:53

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
题意:
一家大公司有多个子公司,原来每个公司都有自己的运算中心,为了提升服务质量,公司决定把一些公司的运算中心合并;
1.输入I ——I J 代表把集合A的运算中心连接到集合B的一家公司,J不一定为集合B的运算中心,I到J 的距离为(I-J)%1000。
2.输入E—–I 问公司I到其运算中心的距离。
N不超过20000,其中询问次数不超过N,输入的数字最大不超过200000,输入O表示结束。
思路:并查集,当搜寻根节点的同时求出到根节点的距离即可;
代码:

#include<cstring>#include<algorithm>#include<cstdio>using namespace std;int a[200005],sum[200005];int find(int t){    if(a[t]==t)       return a[t];    int tt=a[t];    a[t]=find(a[t]);    sum[t]+=sum[tt];    return a[t];}int main(){    int t;    scanf("%d",&t);    while(t--)    {        int n;        scanf("%d",&n);        memset(sum,0,sizeof(sum));        for(int i=1;i<=n;i++)        {            a[i]=i;        }        char c[2];        while(~scanf("%s",c))        {            if(c[0]=='O')               break;            else if(c[0]=='I')            {                int l,r;                scanf("%d%d",&l,&r);                int zx=find(l);                int zy=find(r);                if(zx==zy)                   continue;                sum[l]=abs(r-l)%1000;                a[l]=r;            }            else            {                int k;                scanf("%d",&k);                int temp=find(k);                printf("%d\n",sum[k]);            }        }    }}
0 0
原创粉丝点击