程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> LA 3027 Corporative Network 並查集記錄點到根的距離

LA 3027 Corporative Network 並查集記錄點到根的距離

編輯:C++入門知識

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

[Submit]   [Go Back]   [Status] 

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 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題目大意:有N個結點,初始時每個結點的父親都不存在,你的任務時執行一次I操作和E操作,格式如下:

I u  v  :把結點u的父節點設為v,距離為|u-v|除以1000的余數,輸入保證執行指令前u 沒有父節點

E u   :詢問u 到根節點的距離

 

思路:

帶距離的並查集,在每次合並父親節點的時候更新一個距離就可以了

可參考劉汝佳入門經典訓練指南

 
 

[cpp]
#include <iostream>  
using namespace std; 
const int maxn = 20000 + 10;         
int d[maxn], fa[maxn];        
int find(int x)       

    if(x == fa[x]) return x; 
    else 
    { 
        int root = find(fa[x]); 
        d[x] += d[fa[x]];        
        return fa[x] = root;        
    } 

int main() 

    int T, N, i, I, J; 
    cin>>T; 
    while(T--) 
    { 
        cin>>N; 
        for(i = 0; i <= N; i++)       
        { 
            fa[i] = i;        
            d[i] = 0;       //自己到自己(根)的距離為0  
        } 
        char c; 
        bool ok = 1;          
        while(ok && cin>>c) 
        { 
            switch(c) 
            { 
            case 'O': 
                { 
                    ok = 0; 
                    break; 
                } 
            case 'I': 
                { 
                    //int x,y;  
                    cin>>I>>J; 
                /*  x=find(I);
                    y=find(J);
                    if(x==y)  continue;
                    fa[x]=y;
                    */ 
                    fa[I] = J; 
                    int ans = I > J ? (I-J) : (J-I); 
                    d[I] =ans % 1000;       
                    break; 
                } 
            case 'E': 
                { 
                    cin>>I; 
                    find(I);          
                    cout<<d[I]<<endl; 
                    break; 
                } 
            } 
        } 
    } 
    return 0; 

#include <iostream>
using namespace std;
const int maxn = 20000 + 10;       
int d[maxn], fa[maxn];      
int find(int x)     
{
 if(x == fa[x]) return x;
 else
 {
  int root = find(fa[x]);
  d[x] += d[fa[x]];      
  return fa[x] = root;      
 }
}
int main()
{
 int T, N, i, I, J;
 cin>>T;
 while(T--)
 {
  cin>>N;
  for(i = 0; i <= N; i++)     
  {
   fa[i] = i;      
   d[i] = 0;       //自己到自己(根)的距離為0
  }
  char c;
  bool ok = 1;        
  while(ok && cin>>c)
  {
   switch(c)
   {
   case 'O':
    {
     ok = 0;
     break;
    }
   case 'I':
    {
     //int x,y;
     cin>>I>>J;
    /* x=find(I);
     y=find(J);
     if(x==y)  continue;
     fa[x]=y;
     */
     fa[I] = J;
     int ans = I > J ? (I-J) : (J-I);
     d[I] =ans % 1000;     
     break;
    }
   case 'E':
    {
     cin>>I;
     find(I);        
     cout<<d[I]<<endl;
     break;
    }
   }
  }
 }
 return 0;
}

 

 

不能按代碼中注釋的 那樣

x=find(I);

y=find(J);

if(x==y) continue;

fa[x]=y;

代替 fa[I] = J;

一換掉 就wa 

:  假設用dis表示點到根的距離

因為 為了求dis   所以誰的父親就是誰的父親 不能壓縮 , 但是上面的find代碼是進行壓縮的     不過它是在遞歸求dis後 才壓縮的    所以說 是可以壓縮的   

即 必須先遞歸 後壓縮     這樣就能保證求出的dis 確實是按照父親節點算出來的

上面的那個錯誤  是由於先調用find函數壓縮 ,這時候 點  I 的 dis還沒有算出來,後面再求它的dis的時候 其就不再是按照真正的父親節點找出的 而是按照祖先節點算的(因為壓縮過了)

對於之後輸入E   再次調用fand函數的時候  雖然已經壓縮過了   但是所有點的dis已經求出來了  對於再加入的邊 I  J   我們依舊能夠通過接到已經求出的正確dis 求出來I 的dis

注意dis表示 點到根的距離

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved