题目链接:点击打开链接
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <string.h>
using namespace std;
int a[100005];
int d[100005];
int getFather(int x)
{
int root;
if(x == a[x]){
return a[x];
}
root = getFather(a[x]);
d[x] += d[a[x]];
return a[x] = root;
}
int abs(int I){
return I < 0 ? -I:I;
}
int main(){
int T, N;
char ch[10];
int I, J;
scanf("%d", &T);
while(T--){
scanf("%d", &N);
for(int i = 0; i <= N; i++){
a[i] = i;
d[i] = 0;
}
while(~scanf("%s", ch)){
if(ch[0] == 'O'){
break;
}
else if(ch[0] == 'E'){
scanf("%d", &I);
getFather(I);
printf("%d\n", d[I]);
}
else if(ch[0] == 'I'){
scanf("%d%d", &I, &J);
a[I] = J;
d[I] = abs(I - J) % 1000;
}
}
}
return 0;
}