Algorithm template--and check set 2 (Support fast Instant query this connected block content, pure original!) )

Source: Internet
Author: User

Implementation function: Enter N, now has n number, then enter any line, if it is "1 x y" means to merge x and y blocks, if "2 x" means the entire contents of the block where the output x resides

Principle: In fact, the main is that they create a chain line,he chain Head, TA represents the chain end, and then the merger between the different blocks is directly connected to the two chain, that is, a tail to another head, forming a new chain (because it is the direct superposition of the chain, so you can do strict O (1), And the output of the output of how much complexity is how much, there is no additional complexity at all. Then use the original normal array and check the set for maintenance and tracking (the theoretical value is O (logn) but in fact due to C[x]:=getfat (c[x]) optimization results in the actual test result is much smaller than this complexity)

Complexity: "Merge operation O (1), Query O (block size) (meaning the complexity is almost entirely used to output)" Xn, compared to the previous algorithm for instant processing performance has improved, but only need to finally static global processing, the difference is not too much, this will be slightly faster, the traditional program code slightly less

(PS: It is worth noting that this new data structure in the tens of thousands of two numbers in the same block of the case, you must avoid the same chain end-to-end lead to produce a ring!!! This program 41 line continue there is reflected. Merge block statements such as C[x]:=y and merge (x, Y) operations are ordered, and the order must be consistent and do not want to be in the same order as the original and check set.

1 type2Point=^node;3Node=Record4 G:longint;5 Next:point;6     End;7Line=Record8 He,ta:point;9     End;Ten var OneA:Array[0..100000] ofLine ; AC:Array[0..100000] ofLongint; - I,j,k,l,m,n:longint; - P:point; the procedureMergevarp1,p2:line); inline; -           begin -p1.ta^.next:=p2.he; -p1.ta:=P2.ta; +p2.he:=Nil;p 2.ta:=Nil; -           End; + functionGetfat (x:longint): Longint;inline; A          begin at               ifX&LT;&GT;C[X] Thenc[x]:=Getfat (c[x]); - exit (C[x]); -          End; - begin - READLN (n); -       fori:=1  toN Do in          begin -c[i]:=i; toNew (p);p ^.g:=i;p^.next:=Nil; +a[i].he:=p;a[i].ta:=p; -          End; the       whileTrue Do *            begin $ read (l);Panax Notoginseng                  CaseL of -                      1:begin the readln (j,k); +J:=getfat (j); k:=Getfat (k); A                             ifJ=k Thencontinue; thec[k]:=J; + merge (A[j],a[k]); -                      End; $                      2:begin $ Readln (j); -j:=Getfat (j); -p:=a[j].he; the                              whileP<>Nil  Do -                                   beginWuyiWrite (P^.G,' '); thep:=P^.next; -                                   End; Wu Writeln; -                      End; About                 End; $  -            End; - End.

Algorithm template--and check set 2 (Support fast Instant query this connected block content, pure original!) )

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.