zoukankan      html  css  js  c++  java
  • 【暑假】[实用数据结构]UVAlive 3027 Corporative Network

    UVAlive 3027 Corporative Network

    题目:

     

    Corporative Network
    Time Limit: 3000MS   Memory Limit: 30000K
    Total Submissions: 3450   Accepted: 1259

    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


    -----------------------------------------------------------------------------------------------------------------------------------------------------------

    思路:
    操作只有询问到root距离以及改变父结点,因此可以用并查集实现。为每个结点添加信息d表示到父结点的距离,对于I操作,将d初始化为abs(u-v)%1000,对于每次询问E添加功能:压缩路径的同时改变d,即在p[u]=root的同时有d[u]=dist(u->root)

    代码:
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #define FOR(a,b,c) for(int a=(b);a<(c);a++)
     5 using namespace std;
     6 const int maxn= 20000 +10;
     7 
     8 int p[maxn],d[maxn];
     9 int find_set(int u){
    10 //寻找root结点->路径压缩+维护d 
    11     if(u==p[u]) return u;
    12     else
    13      {
    14          int root=find_set(p[u]);
    15          d[u] += d[p[u]];   //更新d为d->root的距离 
    16          return p[u]=root; //路径压缩 
    17      }
    18 }
    19 
    20 int main(){
    21 int T,n;  scanf("%d",&T);
    22   while (T--){
    23       scanf("%d",&n);
    24       FOR(i,1,n+1){ p[i]=i; d[i]=0; } 
    25       char ch[5];int x,y;
    26       while(scanf("%s",ch) && ch[0]!='O'){
    27         if(ch[0]=='E'){ scanf("%d",&x);find_set(x);printf("%d
    ",d[x]); }
    28       else { scanf("%d%d",&x,&y); p[x]=y; d[x]=abs(x-y) % 1000; }
    29       }
    30   }
    31   return 0;
    32 }
    
    
    
    
    
  • 相关阅读:
    Python中的单例模式
    硬件资产管理系统总结
    浏览器同源策略之JSONP、CORS
    设置全局的csrf跨站请求伪造
    keystone命令与client接口学习
    实验室双显示屏安装使用记录
    redhat 5.4 下rabbitMQ单机安装.md
    Android Studio Connection refused: connect 错误解决
    Add Google Maven repository and sync project
    Java向上转型和向下转型
  • 原文地址:https://www.cnblogs.com/lidaxin/p/4710412.html
Copyright © 2011-2022 走看看