题意:两两给出相互认识的人,另外说明若A认识B,B认识C,则A也一定认识C,相互认识的人才能坐在一个桌子, 求需要多少个桌子。
解题思路:就是先给出n个人, 每个人都是一个独立的集合, 将互相认识的人的集合合并,求最后剩下的集合,用并查集进行 合并, 最后用一遍循环若根节点是自身则加一个桌子(不是自身的都合并到别的集合上了,不用单开桌子)。 简单题。
Description
Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.
One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.
For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.
One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.
For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.
Input
The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.
Output
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
Sample Input
2 5 3 1 2 2 3 4 5 5 1 2 5
Sample Output
2 4
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cctype>
#include<list>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
#define FOR(i, s, t) for(int i = (s) ; i <= (t) ; ++i)
#define REP(i, n) for(int i = 0 ; i < (n) ; ++i)
int buf[10];
inline long long read()
{
long long x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
inline void writenum(int i)
{
int p = 0;
if(i == 0) p++;
else while(i)
{
buf[p++] = i % 10;
i /= 10;
}
for(int j = p - 1 ; j >= 0 ; --j) putchar('0' + buf[j]);
}
/**************************************************************/
#define MAX_N 1005
const int INF = 0x3f3f3f3f;
int parent[MAX_N];
void init(int n)
{
for(int i = 0 ; i < n + 5; i++)
{
parent[i] = i;
}
}
int find(int a)
{
if(a != parent[a])
{
parent[a] = find(parent[a]);
}
return parent[a];
}
void unite(int a, int b)
{
int fa = find(a);
int fb = find(b);
if(fa == fb) return;
parent[fb] = fa;
}
int same(int a, int b)
{
return find(a) == find(b);
}
int main()
{
int t = read();
while(t--)
{
int n = read();
int m = read();
init(n);
for(int i = 0 ; i < m ; i++)
{
int a = read();
int b = read();
unite(a, b);
}
int cnt = 0;
for(int i = 1 ; i <= n ; i++)
{
if(parent[i] == i)
{
cnt++;
}
}
cout<<cnt<<endl;
}
return 0;
}