Hdu1962corporative Network Belt Right Loop

Source: Internet
Author: User

1 /*2 There are n enterprises, each enterprise want to achieve communication, to connect with the line, the length of the line is abs (A-B)%1000;3 If enterprise A is linked to Enterprise B then B is the center of the serving!4 Then there are two kinds of actions:5 E A: The distance from Enterprise A to the serving center line6 I A, B connects enterprise A to Enterprise B, then B becomes the serving center (before the enterprise that connects a, their serving center becomes b)7     8 ideas: and check set! (Backtracking solution when compressing paths)! 9 */ Ten#include <iostream> One#include <cstring> A#include <cmath> -#include <cstdio> - #defineM 20005 the using namespacestd; - intN; - intF[m]; - intANS[M];//the distance from node I to serving center!  +  - intGetfather (intx) { +     if(X==f[x])returnx; A     intHfSGetfather (f[x]); atANS[X]+=ANS[F[X]];//The distance from node X to serving center is added to the distance from the parent node to serving center!  -     returnf[x]=ff; - } -  - voidUnion (intAintb) {  -     if(a==b)return; inf[a]=b; -Ans[a]=abs (A-B)% +; to } +  - intMain () { the    intT; *    Charch[3]; $Cin>>T;Panax Notoginseng     while(t--){ -Cin>>N; the       intA, B; +memset (ans,0,sizeof(ans)); A        for(intI=1; i<=n; ++i) thef[i]=i; +        while(Cin>>ch && ch[0]!='O'){ -           if(ch[0]=='E'){ $Cin>>A; $ Getfather (a); -cout<<ans[a]<<Endl; -           } the           Else{ -Cin>>a>>b;Wuyi Union (A, b); the           } -       } Wu    } -    return 0; About}

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.