题目描述
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.
输入
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.
输出
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
样例输入 复制
2
6 4
1 2
2 3
3 4
1 48 10
1 2
2 3
5 6
7 5
4 6
3 6
6 7
2 5
2 4
4 3
样例输出 复制
3
2
题目大意:今天是 Ignatius 的生日,他邀请了很多朋友。Ignatius 想知道他至少需要多少张桌子。不是所有的朋友都互相认识,而并不是所有的朋友都想和陌生人呆在一起。如果 A 认识 B,B 认识 C,则 A、B、C 互相认识,他们可以呆在一张桌子上。如果 A 认识 B,B 认识 C,D 认识 E,则 A、B、C 可以呆在一张桌子上,而 D、E 必须呆在另一张桌子上。所以 Ignatius 至少需要 2 张桌子。
分析: 题目可以抽象为有n个点m条边的无向图,问有多少个连通分量。实际上和这一节的题目B一样。
#include<algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <queue>
#include <stack>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#define INF 0xffffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
using namespace std;int main(void)
{#ifdef testfreopen("in.txt","r",stdin);//freopen("in.txt","w",stdout);clock_t start=clock();#endif //testint n,m,T;scanf("%d",&T);while(T--){scanf("%d%d",&n,&m);int cnt=1;int citys[n+5]={0};for(int i=0;i<m;++i){int a,b;scanf("%d%d",&a,&b);if(citys[a]==0&&citys[b]==0)citys[a]=citys[b]=cnt,cnt++;else if(citys[a]==0&&citys[b]!=0)citys[a]=citys[b];else if(citys[a]!=0&&citys[b]==0)citys[b]=citys[a];else{int temp=citys[b];for(int i=1;i<=n;++i)if(citys[i]==temp)citys[i]=citys[a];}}sort(citys+1,citys+n+1);
// for(int i=1;i<=n;++i)
// printf("%d ",citys[i]);
// printf("\n");int sum=1,num=citys[1];for(int i=2;i<=n;++i){if(citys[i]!=num||citys[i]==0)sum++,num=citys[i];}printf("%d\n",sum);}#ifdef testclockid_t end=clock();double endtime=(double)(end-start)/CLOCKS_PER_SEC;printf("\n\n\n\n\n");cout<<"Total time:"<<endtime<<"s"<<endl; //s为单位cout<<"Total time:"<<endtime*1000<<"ms"<<endl; //ms为单位#endif //testreturn 0;
}