HDU 1856 and check set (path compression)

Source: Internet
Author: User

Hdu 1856 More is better

Simple and check set, using the path of compression

Test instructions: Find the connected branch with the highest number of nodes and output the number of nodes.

Train of thought: count the number of nodes per connected branch (use and check the set to build the map when the path compression, with a root node subscript corresponding to the sum array Record node number), compared to obtain the maximum value.

1#include <cstdio>2#include <cstring>3 4 int Set[10000000+ -];5 intsum[10000000+ -];6 BOOLvisit[10000000+ -];7 8 intFindintx)9 {Ten     intR =x; One      while(Set[r]! =R) AR =Set[R]; -     inti =x; -     intJ; the      while(I! =R) -     { -j =Set[i]; -         Set[I] =R; +i =J; -     } +     returnR; A } at  - voidMergeintAintb) - { -      inti =find (a); -      intj =find (b); -      if(I! = j)Set[I] =J; inVisit[a] = visit[b] =true; -      return ; to } +  - intMain () the { *     intA,b,n; $      while(~SCANF ("%d",&N))Panax Notoginseng     { -         if(n = =0) the         { +printf"1\n"); A             Continue; the         } +          for(inti =1; I <=10000000; i++) -             Set[I] =i; $          for(inti =0; I < n; i++) $         { -scanf"%d%d",&a,&b); - merge (A, b); the         } -          for(inti =1; I <=10000000; i++)Wuyi         { the              if(Visit[i]) -              { WuSum[find (i)]++; -              } About         } $         intMax =0; -          for(inti =1; I <=10000000; i++) -         { -             if(Visit[i] &&Set[I] = =i) A             { +                 if(Sum[i] >max) theMax =Sum[i]; -             } $         } theprintf"%d\n", max); thememset (Visit,0,sizeof(visit)); thememset (SUM,0,sizeof(sum)); the     } -     return 0; in}

HDU 1856 and check set (path compression)

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.