Corporative Network LA3027

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>

int f[20500][2];

void inser(int x, int y)
{
    f[x][0] = y;
    f[x][1] = abs(x-y)%1000;
}
int find(int x)
{
    int tmp;
    if(f[x][0] != x)
    {
        tmp = f[x][0];
        f[x][0] = find(f[x][0]);
        f[x][1] += f[tmp][1];
    }
    return f[x][0];
}

int main()
{
    int i, t, n, a, b;
    char ch;
    scanf("%d", &t);
    while(t--){
        scanf("%d", &n);
        for( i=0; i<=n ;i++){
            f[i][0] = i;
            f[i][1] = 0;
        }
        while(scanf(" %c", &ch), ch!='O') {
            if(ch=='I'){
                scanf("%d%d", &a, &b);
                inser(a, b);
            }
            else{
                scanf("%d", &a);
                find(a);
                printf("%d\n",  f[a][1]);
            }
        }
    }

    return 0;
}

你可能感兴趣的:(Corporative Network LA3027)