and check set-merge by rank

Source: Internet
Author: User
Tags time 0

1239: Zhongshan College ACM Group Time limit: 1 Sec Memory Limit:
Submit: Solved: 21
[Submit] [Status] [Web Board] Description After several years of development, Zhongshan College ACM team is more and more large, the level is more and more high, the influence is more and more big. As the number of people grows, in order to create a good communication within the team to discuss the atmosphere, the coaching team decided to let the team in the form of discussion. And the coaching team wants all the people they know to discuss in the same group to improve their motivation. The understanding here can be either direct or indirect (if A and B are recognized, B and C are recognized, then A and C can thus make three persons a member of the same group by B. More than four people do similar treatment. If there is a person who does not know anyone, then no way, he can only stay in the corner in a daze. Of course, he is a discussion group himself. At the same time each player has a unique number, numbered from 0 to N-1 (n is the total number of players). Now there are n individuals in the team, with the M-group relationship, each group has two numbers, a, a and B, indicating mutual understanding. So now I ask you, how many discussion groups are there in our school ACM team? And for each player, do you know how many people he belongs to in the discussion group? Input for each set of inputs, the first behavior is two numbers n and M, (1 <=n <=100,1 <=m<=100), respectively, representing the total number of N and M group relationships within the ACM team. The next M-line, each line has two numbers a and b,a and B is the number of the players, representing A and B realizations (0 <= a,b<=n-1). The following line enters an integer k, which represents the K-group query, followed by the K-line each line entered an integer x, indicating the number of members of the team to ask for the number x. (N and M at the same time 0 o'clock input end) output the first line outputs the number of discussion groups (in the form "x groups in Zsc_acmer Camp", where x represents the number of discussion groups) followed by the K-line, and each line outputs a response to the query. Sample Input5 2501234Sample Output3 groups in zsc_acmer camp33311
#include <iostream> #include <cstdio> #include <cstring>using namespace Std;int d[500],s[500];int    Find (int x) {int k,j,r;    R=x;    while (R!=d[r]) r=d[r];    K=x;        while (k!=r) {j=d[k];        D[k]=r;    K=j; } return R;}    void Unio (int x,int y) {x=find (x);    Y=find (y);    if (x==y) return;        if (S[x]>=s[y]) {d[y]=x;    S[x]+=s[y];        } else {d[x]=y;    S[Y]+=S[X];    }}int Main () {int n,m,a,b,ki,tt;        while (~SCANF ("%d%d", &n,&m)) {if (n==0&&m==0) break;            for (int i=0;i<n;i++) {d[i]=i;        S[i]=1;            } while (m--) {scanf ("%d%d", &a,&b);        Unio (A, b);        } int cas=0;        for (int i=0;i<n;i++) {if (d[i]==i) cas++;        } printf ("%d groups in Zsc_acmer camp\n", CAs);        scanf ("%d", &ki);      while (ki--) {      scanf ("%d", &AMP;TT);            int Aa=find (TT);        printf ("%d\n", S[aa]); }} return 0;}

and check set-merge by rank

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.