The test instructions of this question is when the input ' E ' is the lookup operation, looking for the value from the back of this number to his father side, ' I ' represents the father who put the number of the back as the preceding number
Then the difference between them represents the weights of the two sides.
Water and water problems
#include <stdio.h> #include <string.h>int par[20005];int rank1[20005];int abs (int hh) {return (hh>0)? HH: -HH;} void Init () {for (int i=0;i<20005;i++) {par[i]=i; rank1[i]=0; }}int findset (int x) {int root;if (x!=par[x]) {root=findset (par[x]); Rank1[x]+=rank1[par[x]];p ar[x]=root;} return par[x];} int main () {int t;int k;int A,u,v;char s[10];while (scanf ("%d", &t) ==1) {while (t--) {scanf ("%d", &k); Init (); while (scanf ("%s", s)!=eof) {if (s[0]== ' O ') break; if (s[0]== ' E ') {scanf ("%d", &a); Findset (a); printf ("%d\n", Rank1[a]); } else {scanf ("%d%d", &u,&v); Par[u]=v; Rank1[u]=abs (u-v)%1000; printf ("%d%d\n", U,rank1[u]); }//printf ("%d%d%d%d\n", rank1[1],rank1[2],rank1[3],rank1[4]); }}}}