题目传送
题意:
给你N个人,这n个人,有些人是朋友,有些人不是朋友(如果1和2是朋友,2和3是朋友,那么1和3也是朋友),现在问,你最少分多少组,使得每一组中的每一个人都不是朋友
思路:
其实就是求最长的环,或者一条链的最长长度,那么这几个人是肯定不能在一起的,所以这就是最少的分组,用并查集求出即可
AC代码
#include <bits/stdc++.h>
inline int read(){char c = getchar();int x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x)&(-x)
const int N = 2e5 + 5;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const int mod = 1e9+7;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
int n,m,arr[N],Max,ans[N];
int Find(int x)
{
if(x == arr[x]) return x;
return arr[x] = Find(arr[x]);
}
signed main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
cin >> n >> m;
for(int i = 1;i <= n;i++) arr[i] = i;
while(m--)
{
int a,b;
cin >> a >> b;
if(a > b) swap(a,b);
if(Find(a) != Find(b))
arr[Find(a)] = arr[Find(b)];
}
for(int i = 1;i <= n;i++)
{
int x = Find(arr[i]);
ans[x]++;
Max = max(Max,ans[x]);
}
cout << Max << endl;
}