zoukankan      html  css  js  c++  java
  • pta 朋友圈

    某学校有N个学生,形成M个俱乐部。每个俱乐部里的学生有着一定相似的兴趣爱好,形成一个朋友圈。一个学生可以同时属于若干个不同的俱乐部。根据“我的朋友的朋友也是我的朋友”这个推论可以得出,如果A和B是朋友,且B和C是朋友,则A和C也是朋友。请编写程序计算最大朋友圈中有多少人。

    输入格式:

    输入的第一行包含两个正整数N(≤30000)和M(≤1000),分别代表学校的学生总数和俱乐部的个数。后面的M行每行按以下格式给出1个俱乐部的信息,其中学生从1~N编号:

    第i个俱乐部的人数Mi(空格)学生1(空格)学生2 … 学生Mi

    输出格式:

    输出给出一个整数,表示在最大朋友圈中有多少人。

    输入样例:

    7 4
    3 1 2 3
    2 1 4
    3 5 6 7
    1 6

    输出样例:

    4
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int pre[30005];//记录祖先
    int Num[30005];//记录各点的子朋友有多少(包括自己)
    int N,M;
    
    int Find(int a){
        if(pre[a] == a)return a;
        return pre[a] = Find(pre[a]);
    }
    
    void Judge(int a,int b){
        int A = Find(a);
        int B = Find(b);
        if(A != B){
            pre[B] = A;
            Num[A] += Num[B];
        }
    }
    
    int main(){
    
        scanf("%d %d",&N,&M);
        for(int i=0 ; i<=N ; i++)pre[i] = i;
        for(int i=0 ; i<=N ; i++)Num[i] = 1;
        while(M--){
            int mid;
            scanf("%d",&mid);
            int a,b;
            if(mid == 1){
                scanf("%d",&a);
            }
            else {
                scanf("%d",&a);
                while(--mid){
                    scanf("%d",&b);
                    Judge(a,b);
                }
            }
        }
        int max = 0;
        for(int i=0 ; i<=N ; i++){
            if(Num[i] > max)max = Num[i];
        }
        printf("%d
    ",max);
    
        return 0;
    }
    
  • 相关阅读:
    Welcome-to-Swift-12附属脚本(Subscripts)
    Summarization of Tech Interviews
    How to Conduct High-Impact Research and Produce High-Quality Papers
    ZVAL——PHP源码分析
    个人使命
    习题-机器学习-西瓜书-周志华
    Machine Learning
    《踏踏实实学英语》读书笔记
    TF-IDF原理与实现
    线性代数学习感悟
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514198.html
Copyright © 2011-2022 走看看