toj-2469-朋友的朋友是朋友
来源:互联网 发布:广告法淘宝处罚案例 编辑:程序博客网 时间:2024/04/30 06:55
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.
Input
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 ≤ M ≤ N * (N-1) / 2. All the people are numbered from 1 to N.
Output
Output one line for each test case, indicating the minimum number of rooms they have to book.
Sample Input
35 31 22 34 55 41 22 33 44 510 0
Sample Output
2110
Hint
In the first sample test case, there are 5 people. We see that 1,2,3 can live in a room, while 4,5 can live in another room. So the answer should be 2. Please note even though 1 and 3 are not "direct" friends, but they are both 2's friends, so 1 and 3 are friends too.
In the second sample test case, there are 5 people, any two of them will become friends. So they can live in one room.
In the third sample test case, there are 10 people and no friend relationship between them. No two people can live the same room. They have to book 10 rooms.
using namespace std;
int find(int x)
{
int r=x;
while(father[r]!=r)
{
r = father[r];
}
int i=x, j;
while(i!=r)
{
j=father[i];
father[i]=r;
i=j;
}
return r;
}
void join(int x, int y)
{
int fx = find(x), fy = find(y);
if(fx != fy)
father[fx] = fy;
}
int main()
{
int Case, N, M, a, b;
cin >> Case;
while(Case--)
{
cin >> N >> M;
for(int i=1; i<=N; i++)
father[i] = i;
while(M--)
{
cin >> a >> b;
join(a,b);
}
int count = 0;
for(int i=1; i<=N; i++)
{
if(father[i] == i) count++;
}
cout << count << endl;
}
return 0;
}
- toj-2469-朋友的朋友是朋友
- 朋友
- 朋友
- 朋友
- 朋友
- 朋友?
- 朋友
- 朋友
- 朋友
- 朋友
- 朋友
- 朋友
- 朋友
- 朋友
- 朋友
- 朋友
- 朋友
- 朋友
- 必读的Python好文
- FPGA第一次作业——计数器
- 【python】数据库操作
- (HDU2063)过山车(匈牙利算法入门)
- 给那些想学习但只是不够系统的学生一丢丢信息
- toj-2469-朋友的朋友是朋友
- 欢迎使用CSDN-markdown编辑器
- RSA非对称加密算法及使用openssl创建秘钥过程
- JS解惑--原型与原型链
- 简单字符驱动设备
- 远程GPRS发送至上位机
- Android-WARNING: Uninstalling will remove the application data! Do you want to uninstall the existi
- R中logistics回归分析以及K-CV
- js中的引号嵌套问题