POJ1962: Negative ative Network (and query set)

Source: Internet
Author: User

POJ1962: Negative ative Network (and query set)

Description

A very big corporation is developing its own ative network. in the beginning each of the N parameter ISES 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 sort ISES 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 specified ises j in some other cluster B (not necessarily the center) and link them with telecommunication line. the length of the line between the specified ises 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 cocould be changed and the end users wocould 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 Each ISES (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 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 shoshould contain as your 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

14E 3I 3 1E 3I 1 2E 3I 2 4E 3O

Sample Output

0235

Source

Southeastern Europe 2004
There are n points. At the beginning, each point uses itself as the signal end point. When I X Y is input, X and Y are connected, and the signal end point of X points to Y, e x is the distance from X to the end of the signal.
Train of Thought: Classic and query sets, but each node has permissions and needs to be updated
#include 
 
  #include 
  
   #include 
   
    #include 
    
     #include 
     
      #include
      #include 
       
        #include 
        
         #include 
         
          #include using namespace std;#define ls 2*i#define rs 2*i+1#define up(i,x,y) for(i=x;i<=y;i++)#define down(i,x,y) for(i=x;i>=y;i--)#define mem(a,x) memset(a,x,sizeof(a))#define w(a) while(a)#define LL long longconst double pi = acos(-1.0);#define Len 20005#define mod 19999997const int INF = 0x3f3f3f3f;int t,n;char str[5];int father[Len],dis[Len];int find(int x){ if(x == father[x]) return x; int fx = find(father[x]); dis[x] = dis[x]+dis[father[x]]; return father[x]=fx;}void solve(int x,int y){ int fx = find(x),fy = find(y); if(fx == fy) return; father[x] = x; dis[fx] = dis[x]; father[x] = y; dis[x] = abs(x-y)%1000;}int main(){ int i,j,k,x,y; scanf("%d",&t); w(t--) { scanf("%d",&n); up(i,0,n) { father[i] = i; dis[i] = 0; } w(~scanf("%s",str)) { if(str[0]=='O') break; if(str[0]=='E') { scanf("%d",&x); find(x); printf("%d\n",dis[x]); } else { scanf("%d%d",&x,&y); solve(x,y); } } } return 0;}
         
        
       
     
    
   
  
 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.