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)