先使用并查集,后计数
#include <queue>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
#define MAX 31111
const int INF = 0x3f3f3f3f;
const int maxn = 2*1e6+9;
int n,m;
int t;
int root[MAX];
int find_root(int pos)
{
int i = pos;
int temp = pos;
while(root[pos] != pos)
pos = root[pos];
while(root[i] != pos)
{
temp = root[i];
root[i] = pos;
i = temp;
}
return pos;
}
int degree[MAX];
int main(void)
{
int i,j,k;
cin >> n >> m;
for(i=0; i<=n; ++i)
root[i] = i;
for(i=0; i<m; ++i)
{
cin >> k;
int a = 0;
for(j=0; j<k; ++j)
{
int b = 0;
cin >> b;
if(j == 0)
a = b;
else
{
int temp_a = find_root(a);
int temp_b = find_root(b);
if(temp_a != temp_b)
root[temp_b]= temp_a;
}
}
}
for(i=1; i<=n; ++i)
{
int pos = 0;
pos = find_root(i);
++degree[pos];
}
set<int> ans;
for(i=1; i<=n; ++i)
ans.insert(degree[i]);
set<int>::iterator p = ans.end();
--p;
cout << *p << endl;
return 0;
}