Title Link: http://poj.org/problem?id=2524
This problem belongs to and look up the set
Main topic:
Investigate the student's beliefs in a school, tell you that there are N people in the school, give you a total of M group X, y to show the same religious situation, ask a school student the maximum number of religions religion
This is also a template problem, nothing to say, I said in another article about the practice of template, we can refer to the next. http://blog.csdn.net/xdz78/article/details/47296585
#include <stdio.h> #include <stdlib.h> #include <string.h>int pre[ 50010];int find (int x) {int r=x; while (pre[r]!=r) {r=pre[r]; }//path compression, optimizing int i=x,j; while (i!=r) {j=pre[i]; Pre[i]=r; I=j; Return r;//returns the root node}void join (int x,int y) {//if x, Y is changed to the same root node of x, y on different trees. Don't move in a tree. int R_x=find (x), R_y=find (y); if (r_x!=r_y) {pre[r_x]=r_y; }}int Main () {int hash[50010]; int sum,x,y,i,j=1; int n,m; while (scanf ("%d%d", &n,&m) &&n!=0&&m!=0) {sum=0; memset (hash,0,sizeof (hash)); for (i=1;i<=n;i++) {pre[i]=i; }//Front is initialize while (m--) {scanf ("%d%d", &x,&y); Join (x, y); }//Add all the relationships to the tree for (i=1;i<=n;i++) {hash[find (i)]=1;//simple hash} for (i=1;i<=n;i++) { Sum+=hash[i]; } printf ("Case%d:%d\n", j++,sum); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2524 Detailed