POJ 2524 Ubiquitous Religions "and check set"

Source: Internet
Author: User

Problem-Solving Ideas: Enter the total number of N, and M group data, that is, and Hangzhou electric Smooth engineering, similar to the M group of data to do the merger, the last number of areas to find a total.

#include <stdio.h>int pre[50001];int find (int root) {if (Root!=pre[root]) Pre[root]=find (Pre[root]); return pre[ Root];} void Unionroot (int x,int y) {int root1,root2;root1=find (x); Root2=find (y); if (x!=y) {Pre[root1]=root2;}} int main () {int n;int i;int x,y;long int m;int tag=1;while (scanf ("%d%d", &n,&m)!=eof&& (n| | m) {int sum=0;for (i=0;i<n;i++) pre[i]=i;while (m--) {scanf ("%d%d", &x,&y); Unionroot (x, y);} for (i=0;i<n;i++) {if (Find (i) ==i) sum++;} printf ("Case%d:%d\n", tag,sum); tag++;}}

POJ 2524 Ubiquitous Religions "and check set"

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.