題目描述
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;
}