题解 | #分寝室问题#

分寝室问题

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;
}
全部评论

相关推荐

狠赚笔第一人:学计算机自己不努力怪大环境?我大一就拿到了美团大厂的offer,好好看看自己有没有努力查看图片
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务