【算法】Friends

给定一些旅行者,其中一些是朋友。朋友关系具有传递性。他们计划预订酒店房间,每个人都希望和朋友住在一起,避免与陌生人同住。题目要求计算至少需要预订多少间房间。输入包含测试用例数、人员数、关系数,以及具体的友谊关系。通过并查集算法可以解决此问题。示例输入和输出展示了不同情况下的房间需求。
摘要由CSDN通过智能技术生成

描述

There are some people traveling together. Some of them are friends. The friend relation is transitive, that is, if A and B are friends, B and C are friends, then A and C will become friends too.

These people are planning to book some rooms in the hotel. But every one of them doesn’t want to live with strangers, that is, if A and D are not friends, they can’t live in the same room.

Given the information about these people, can you determine how many rooms they have to book at least? You can assume that the rooms are large enough.

输入

The first line of the input is the number of test cases, and then some test cases followed.

The first line of each test case contain two integers N and M, indicating the number of people and the number of the relationship between them. Each line of the following M lines contain two numbers A and B (1 ≤ A ≤ N , 1 ≤ B ≤ N , A ≠ B), indicating that A and B are friends.

You can assume 1 ≤ N ≤ 100, 0 ≤

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值