POJ 2524 Detailed

Source: Internet
Author: User

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

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.