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;
}
相关文章推荐
- DOM树知识点梳理
- 帝国列表页分开调取年份和月份单独调用的方法?
- mysql 定时器 event
- 黑马程序员————Java运算符
- php 统计fasta 序列长度和GC含量
- BIEE用户跟踪-来自官网
- 当下移动互联网的6个泡沫,快要破了!
- POJ 2251 Dungeon Master
- AndroidStudio学习笔记一:了解Activity
- JAVA 修饰符
- Material Design之CoordinatorLayout+AppBarLayout实现上滑隐藏ToolBar-----Material Design使用---
- 函数模板
- mysql 存储过程
- 主席树学习
- js实现兼容IE、Firefox的图片缩放代码
- Javascript 获取浏览器窗口中文档(视口)可用尺寸的方法
- 猫猫学iOS之tableview隐藏最下边多余的cell
- 杂思
- Django笔记教程:四、会话、注册、以及用户
- Linux中的completion使用