How Many Tables

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27455    Accepted Submission(s): 13656

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
 
板子题
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<string>
#define eps 0.000000001
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int N=+;
int parent[N];
int find(int x){
int r=x;
while(r!=parent[r])r=parent[r];
int i=x;
int j;
while(parent[i]!=r){
j=parent[i];
parent[i]=r;
i=j;
}
return r;
}
void Union(int x,int y){
x=find(x);
y=find(y);
if(x<y)parent[x]=y;
else if(x>y)parent[y]=x;
if(x==y)return;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m;
int x,y;
cin>>n>>m;
for(int i=;i<=n;i++)parent[i]=i;
for(int i=;i<=m;i++){
cin>>x>>y;
Union(x,y);
}
int ans=;
for(int i=;i<=n;i++){
if(parent[i]==i)ans++;
}
cout<<ans<<endl;
}
}

最新文章

  1. 对于System.exit(0)和System.exit(1)的一般理解
  2. DBN 入门学习资料整理
  3. Android菜鸟成长记11 -- sqlite数据库的设计和升降级
  4. JDBC操作MySQL数据库案例
  5. Dynamic CRM 2013学习笔记(六)备份和恢复
  6. CELL_PHOTO_IDENTIFIER
  7. 从头开始编写项目Makefile(八):型号规则
  8. JAVA基础第十组(5道题)
  9. onConfigurationChanged方法的使用
  10. MySQL--MHA原理
  11. day8--socket文件传输
  12. Regmap 框架:简化慢速IO接口优化性能【转】
  13. django版本切换以及更改url(pycharm)
  14. 架构师养成记--35.redis集群搭建
  15. 彻底明确Android中AIDL及其使用
  16. 理解 XCode 中的 Git 版本控制
  17. Java中的&lt;&lt; 和 &gt;&gt; 和 &gt;&gt;&gt; 详细分析
  18. 【BZOJ4104】解密运算 [暴力]
  19. [hadoop][基本原理]zookeeper基本原理
  20. 23种java设计模式之装饰者模式及动态代理

热门文章

  1. 【译】x86程序员手册08 -2.6中断和异常
  2. Appium 使用android_uiautomator定位元素时报错: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource
  3. Scala 技术笔记之 Option Some None
  4. [luogu2148 SDOI2009] E&amp;D (博弈论)
  5. MySQL详细操作
  6. saving snaps iteratively with for loop in Paraview
  7. net core 配置Redis Cache
  8. 解决WP程序 重复打开出现 “正在加载...” 字样 解决方案
  9. vue 使用echarts
  10. 【codeforces 755E】PolandBall and White-Red graph