题解 | #分寝室问题#
分寝室问题
https://ac.nowcoder.com/acm/problem/254344
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[101] = {0};
int n,k;
cin >> n >> k;
if (k == 0)
{
cout << n << " " << 1;
return 0;
}
bool od[101] = {0};
int flag = 1;
for (int i = 0; i < k; i++)
{
int a, b;
cin >> a >> b;
if (arr[a] == 0 && arr[b] == 0)
{
arr[a] = flag;
arr[b] = flag;
flag++;
}else if (arr[a] != 0)
{
arr[b] = arr[a];
}else if (arr[b] != 0)
{
arr[a] = arr[b];
}
}
int count = 0;
int mx = 0;
for (int i = 1; i <= n; i++)
{
if (arr[i] == 0)
{
count++;
}
else if (od[i] == 0)
{
int temp = 1;
count++;
od[i] = 1;
for (int j = i + 1; j <= n; j++)
{
if (arr[j] == arr[i])
{
od[j] = 1;
temp++;
}
}
mx = max(temp, mx);
}
}
cout << count << " " << mx;
return 0;
}