The problem with UVA is that the application of data structure is the application of union find and check set. Use path compression primarily. You don't even need to merge the tree because there are no duplicate lines and modifications to the single-parent node operation.
Depressed is not too familiar with this OJ system, incredibly use the ABS in the library will WA, wrote an ABS small function on the past.
Title: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= 4075
#include <stdio.h>const int max_n = 20005;const int MOD = 1000;//in UVA using the library ABS incredibly WA, wasted a lot of time inline int abs (int a) {retur N A < 0? -A:A; }struct Subset{int P, W;}; Subset Subs[max_n];int findparent (int x) {if (SUBS[X].P! = x) {int p = SUBS[X].P;SUBS[X].P = Findparent (SUBS[X].P); Subs[x]. W = (SUBS[X].W + SUBS[P].W);} return SUBS[X].P;} void initsubs (int N) {for (int i = 1; I <= N; i++) {subs[i].p = I;SUBS[I].W = 0;}} int main () {int T, N, I, J;char cmd;scanf ("%d", &t), while (t--) {scanf ("%d", &n), GetChar (); Initsubs (n); while (cmd = GetChar ()) && cmd! = ' O ') {if (cmd = = ' E ') {scanf ("%d", &i), SUBS[I].P = Findparent (i);p rintf ("%d\n", SUBS[I].W );} ELSE{SCANF ("%d%d", &i, &j), SUBS[I].W = (ABS (j-i))%MOD;SUBS[I].P = j;//There is no duplicate connection and changes to the parent, therefore directly connected to Ok}getchar ();}} return 0;}