某学校有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 <iostream>
using namespace std;
typedef struct node
{
int data;
int rank;
int parent;
}UFSTree;
void MAKE_SET(UFSTree t[], int n)
{
int i;
for (i = 0; i <= n; i++)
{
t[i].data = i;
t[i].rank = 0;
t[i].parent = i;
}
}
int FIND_SET(UFSTree t[], int x)
{
if (x != t[x].parent)
return(FIND_SET(t, t[x].parent));
else
return x;
}
void UNION(UFSTree t[], int x, int y)
{
x = FIND_SET(t, x);
y = FIND_SET(t, y);
if (t[x].rank > t[y].rank)
t[y].parent = x;
else
{
t[x].parent = y;
if (t[x].rank == t[y].rank)
t[y].rank++;
}
}
int main()
{
int N, M;
cin >> N >> M;
UFSTree P[30001];
MAKE_SET(P, N);
for (int i = 0; i < M; i++)
{
int n,M[30001];
cin >> n >> M[0];
for (int j = 1; j < n; j++)
{
cin >> M[j];
UNION(P, M[j - 1], M[j]);
}
}
int Max[30001] = { 0 };
for (int k = 1; k <= N; k++)
Max[FIND_SET(P, k)]++;
int MA = 0;
for (int p = 1; p <= N; p++)
MA = MA > Max[p] ? MA : Max[p];
cout << MA;
return 0;
}