您的位置:首页 > 其它

poj 1213 How Many Tables 并查集水题

2015-12-08 12:25 441 查看


How Many Tables

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 19987    Accepted Submission(s): 9942


Problem 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.

 

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

 

Author

Ignatius.L

 

Source

杭电ACM省赛集训队选拔赛之热身赛

 

Recommend

Eddy   |   We have carefully selected several similar problems for you:  1272 1325 1198 1102 1162 

 

并查集 容易失误的地方之一:该写find(x)的时候老写成pre[x];

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<cctype>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI (4.0*atan(1.0))
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n) for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n) for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n) for(int i=(n) ;i>=0 ;i--)
#define lson ind<<1,le,mid
#define rson ind<<1|1,mid+1,ri
#define MID int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk make_pair
#define _f first
#define _s second
using namespace std;
//const int INF= ;
typedef long long ll;
//const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
const int INF =0x3f3f3f3f;
const int maxn= 1000+20 ;
//const int maxm= ;
int pre[maxn];
set<int >se;
int n,m;
void init()
{
for(int i=1;i<=n;i++)
pre[i]=i;
}

int find(int x)
{
return x==pre[x]?x:pre[x]=find(pre[x]);
}
void merge(int s,int t)
{
int x=find(s);
int y=find(t);
if(x==y) return;
pre[x]=y;
}

int main()
{
int T;scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
init();
int x,y;
FOR1(i,m)
{
scanf("%d%d",&x,&y);
merge(x,y);
}
se.clear();
// FOR1(i,n) se.insert(pre[i]);
FOR1(i,n) se.insert(find(i));
printf("%d\n",se.size());

}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: