zoukankan      html  css  js  c++  java
  • PTA数据结构与算法题目集(中文) 7-25

    PTA数据结构与算法题目集(中文)  7-25

    7-25 朋友圈 (25 分)
     

    某学校有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
    题目分析:这道题是并查集的利用 没什么需要注意的
     1 #define _CRT_SECURE_NO_WARNINGS
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<malloc.h>
     5 
     6 int S[30005];
     7 
     8 int Find(int x)
     9 {
    10     for (; S[x] > 0; x = S[x])
    11         ;
    12     return x;
    13 }
    14 
    15 void Union(int x, int y)
    16 {
    17     int X = Find(x);
    18     int Y = Find(y);
    19     if (X == Y)return;
    20     S[X] += S[Y];
    21     S[Y] = X;
    22 }
    23 
    24 int main()
    25 {
    26     int N, M;
    27     int x, y;
    28     int have = 0;
    29     scanf("%d%d", &N, &M);
    30     for (int i = 0; i <= N; i++)S[i] = -1;
    31     while (M--)
    32     {
    33         int num;
    34         scanf("%d", &num);
    35         for (int i = 0; i < num; i++)
    36         {
    37             if (i == 0)
    38                 scanf("%d", &x);
    39             else
    40             {
    41                 scanf("%d", &y);
    42                 Union(x, y);
    43             }
    44         }
    45     }
    46     for (int i = 1; i <=N; i++)
    47     {
    48         if (S[i] < have)
    49             have = S[i];
    50     }
    51     have = 0 - have;
    52     printf("%d", have);
    53 }
    View Code
  • 相关阅读:
    URAL 1998 The old Padawan 二分
    URAL 1997 Those are not the droids you're looking for 二分图最大匹配
    URAL 1995 Illegal spices 贪心构造
    URAL 1993 This cheeseburger you don't need 模拟题
    URAL 1992 CVS
    URAL 1991 The battle near the swamp 水题
    Codeforces Beta Round #92 (Div. 1 Only) A. Prime Permutation 暴力
    Codeforces Beta Round #7 D. Palindrome Degree hash
    Codeforces Beta Round #7 C. Line Exgcd
    Codeforces Beta Round #7 B. Memory Manager 模拟题
  • 原文地址:https://www.cnblogs.com/57one/p/11631395.html
Copyright © 2011-2022 走看看