#ifdef _WORK_
#include <algorithm>
#include <cstdio>
#include<cstring>
using namespace std;
#define CLR(x,y) memset((x),(y),sizeof((x)))
#define FOR(x,y,z) for(int (x)=(y);(x)<(z);++(x))
const int maxn = 20000 +10;
int fa[maxn],d[maxn];
inline void ini(int n){
CLR(d,0);
FOR(i,0,n+1) fa[i] = i;
}
int fnd(int x){
if(x == fa[x]) return x;
int tmp = fa[x];
fa[x] = fnd(fa[x]);
d[x] = d[x] + d[tmp];
return fa[x];
}
int main(){
int t,n,p,q;
char str[10];
scanf("%d",&t);
while(t--){
scanf("%d",&n);
ini(n);
while(~scanf("%s",str) && str[0] != ‘O‘){
if(str[0] == ‘I‘){
scanf("%d%d",&p,&q);
fa[p] = q;
d[p] = abs(p - q)%1000;
}else{
scanf("%d",&p);
fnd(p);
printf("%d\n",d[p]);
}
}
}
return 0;
}
[2016-03-19][UVALive][3027][Corporative Network]
原文:http://www.cnblogs.com/qhy285571052/p/61c1f535569220dfb7b920319b6b8935.html