HDU 1213 How Many Tables(并查集统计求不同集合个数)
来源:互联网 发布:淘宝店铺发货无需物流 编辑:程序博客网 时间:2024/06/09 20:05
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 31224 Accepted Submission(s): 15516
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.
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
25 31 22 34 55 12 5
Sample Output
24
Author
Ignatius.L
Source
杭电ACM省赛集训队选拔赛之热身赛
Recommend
Eddy
题意:给n个点,m个关系,统计共有多少个不同集合
也是并查集的常用用法,直接合并有关系的集合,也是取最小的pre
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define maxn 1010
using namespace std;
int pre[maxn];
int off[maxn];
int fr(int x)
{
int r=x;
while(r!=pre[r])r=pre[r];
int j=x;
while(x!=r)
{
j=pre[x];
pre[x]=j;
x=j;
}
return r;
}
void join(int x,int y)
{
int r1=fr(x);
int r2=fr(y);
if(r1>r2)pre[r1]=r2;
else if(r1<r2)pre[r2]=r1;
}
int main()
{
int t;
scanf("%d",&t);
int a,b,m,n;
int mark[maxn];
while(t--)
{
scanf("%d%d",&n,&m);
memset(mark,0,sizeof(mark));
for(int i=1;i<=n;i++)pre[i]=i;
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
join(a,b);
}
int ans=0;
for(int i=1;i<=n;i++)
{
int r=fr(i);
if(!mark[r]){ans++;mark[r]=1;}
}
printf("%d\n",ans);
}
return 0;
}
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define maxn 1010
using namespace std;
int pre[maxn];
int off[maxn];
int fr(int x)
{
int r=x;
while(r!=pre[r])r=pre[r];
int j=x;
while(x!=r)
{
j=pre[x];
pre[x]=j;
x=j;
}
return r;
}
void join(int x,int y)
{
int r1=fr(x);
int r2=fr(y);
if(r1>r2)pre[r1]=r2;
else if(r1<r2)pre[r2]=r1;
}
int main()
{
int t;
scanf("%d",&t);
int a,b,m,n;
int mark[maxn];
while(t--)
{
scanf("%d%d",&n,&m);
memset(mark,0,sizeof(mark));
for(int i=1;i<=n;i++)pre[i]=i;
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
join(a,b);
}
int ans=0;
for(int i=1;i<=n;i++)
{
int r=fr(i);
if(!mark[r]){ans++;mark[r]=1;}
}
printf("%d\n",ans);
}
return 0;
}
阅读全文
0 0
- HDU 1213 How Many Tables(并查集统计求不同集合个数)
- HDU 1213 How Many Tables(求集合个数)
- 并查集 hdu How Many Tables
- HDU 1213 How Many Tables (dfs求联通块 || 并查集)
- hdu 1213 How Many Tables(并查集~~)
- Hdu 1213How Many Tables 并查集
- 杭电hdu 1213 How Many Tables并查集
- HDU 1213 How Many Tables(并查集)
- HDU 1213 How Many Tables,并查集
- HDU--1213--How Many Tables--并查集
- hdu 1213 How Many Tables(并查集练习)
- HDU 1213 How Many Tables 并查集入门
- HDU 1213 How Many Tables 并查集 水~
- HDU 1213 How Many Tables(并查集)
- [ACM] hdu 1213 How Many Tables(并查集)
- [ACM] hdu 1213 How Many Tables(并查集)
- 【并查集】 HDU 1213 How Many Tables
- hdu 1213 How Many Tables(并查集学习)
- js基础知识总结
- 计算机图形学 学习笔记(四):直线裁剪算法:Cohen-Suther land,中点分割法,Liang-Barsky
- 一言不合又给大家发数字货币量化和策略的干货啦
- 集合
- Xcode上的Other linker flags
- HDU 1213 How Many Tables(并查集统计求不同集合个数)
- iOS
- 并行计算--第一章
- html中input输入框如何限定输入数字或英文
- 两个表的关联查询
- learning之工厂方法模式
- 关于vs、fs、索引数组的理解总结--适合新手
- Linux下Jenkins 安装与配置
- 从萌芽到当今——浅谈互联网架构的演进