"LA 3027 corporative Network"

Source: Internet
Author: User

• 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--&AMP;&AMP;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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.