• Some very cute questions and modifications, relax to use and check the set to solve.
• English questions, to the effect:
Input n (5<=n<=20000) indicates that the tree has n nodes, and EOF ends up reading in no more than 20,000 operations, a total of two:
①i v u: Indicates that the Father node of V is set to u (before this v has no father), the edge is set to ABS (V-u)%1000.
②e u: Indicates the distance from the root node of the tree to which you are currently in question.
Analysis
in order to record the current series of add-edges The position of all points after the operation (because you can answer the question at any time), according to the problem of the point of the characteristics of the relationship [only care about the point and its root node information], We select and check set to be maintained.
Then we only need to add the accumulation of edge weights in the backtracking process of the standard Findfather function, so that once the function can both complete the path compression, and maintain a bit of distance along the way to the root node (that is, edge right and).
And then this short puzzle reminds me of the word that people often use on the Internet: " Gee, the rest of the mess comes out ." But the problem is so simple. Ok.
1#include <stdio.h>2 #defineGo (i,a,b) for (int i=a;i<=b;i++)3 intt,n,fa[20004],d[20004];CharC;4 intAintA) {returnA>0? a:-A;}5 intFindintu)6 {7 if(U==fa[u])returnUintFa=find (Fa[u]);8D[u]+=d[fa[u]];returnfa[u]=Fa;9 }Ten intMain () One { Ascanf"%d", &t); while(T--&&SCANF ("%d",&N)) - { -Go (U,1, N) d[fa[u]=u]=0;intu,v; the while(SCANF ("%c", &c), c!='O') - { - if(c=='I') scanf ("%d%d", &v,&u), Fa[v]=u,d[v]=a (v-u)% +; - if(c=='E') scanf ("%d", &u), Fa[u]=find (U), printf ("%d\n", D[u]); + } - } + return 0; A}//Paul_guderian
Tomorrow, when loneliness strikes, I won't shed a tear,
I will erase the wounds of the Ashes with my song. ————— Wang Feng "Tomorrow"
"LA 3027 corporative Network"