关闭

hdu1213 How Many Tables (裸并查集)

标签: hdu1213How Many Tables并查集
315人阅读 评论(0) 收藏 举报
分类:

How Many Tables

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


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
 

Recommend
Eddy   |   We have carefully selected several similar problems for you:  1272 1232 1856 1325 1233 
 


代码:

#include<cstdio>
using namespace std;

const int maxn=1e3;
int f[maxn+10];

int find(int x)
{
  if(f[x]==x)return x;
  return f[x]=find(f[x]);
}

int main()
{
 // freopen("1.in","r",stdin);
  
  int t,n,m,i,x,y,rx,ry;
  scanf("%d",&t);
  while(t--)
    {
      scanf("%d%d",&n,&m);
      for(i=1;i<=n;i++)f[i]=i;
      for(i=1;i<=m;i++)
        {
          scanf("%d%d",&x,&y);
          rx=find(x),ry=find(y);
          if(rx!=ry)f[rx]=ry,n--;
		}
	  printf("%d\n",n);
	}
  return 0;
}


0
0
查看评论

HDOJ 1213 How Many Tables(并查集)

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17935&#...
  • zwj1452267376
  • zwj1452267376
  • 2015-07-24 20:47
  • 541

HDU:1213 How Many Tables(简单并查集)

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24376&#...
  • zugofn
  • zugofn
  • 2016-08-01 20:20
  • 542

HDU - 1213 How Many Tables (简单并查集)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意: T组数据, n
  • u014561729
  • u014561729
  • 2014-07-16 20:50
  • 215

hdu1213 How many tables 并查集

hdu1213 并查集
  • Remilitarize
  • Remilitarize
  • 2017-03-21 14:17
  • 90

HDU1213 How Many Tables 【并查集】

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 133...
  • u012846486
  • u012846486
  • 2014-08-06 18:12
  • 661

HDU1213 How Many Tables (并查集)

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 ...
  • ling_wang
  • ling_wang
  • 2017-12-02 21:38
  • 20

How Many Tables hdu1213 并查集

http://acm.hdu.edu.cn/showproblem.php?pid=1213 。。。好久没写代码了。= =。 非常简单的并查集。 #include using namespace std; #define N 1003 int people[N]; int findfathe...
  • operator456
  • operator456
  • 2013-04-19 16:20
  • 474

hdu1213 How Many Tables 并查集

#include #include #define Maxn 1024 int father[Maxn]; void init(int n) { for(int i=1;i<=n;i++) father[i]=i; } int getf(int x) { if(x!=fathe...
  • Griffin_0
  • Griffin_0
  • 2015-10-08 17:32
  • 188

HDU1213 How Many Tables - 并查集

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1213/* HDU1213 How Many Tables http://acm.hdu.edu.cn/showproblem.php?pid=1213 */ #include <iostream&...
  • qq_26437925
  • qq_26437925
  • 2015-08-26 15:27
  • 192

HDU1213 How Many Tables 并查集

并查集水题,建立n并查集,然后把认识的人都连起来,最后算一下父亲的数量即可(输入里的空一行是什么鬼。。。) #include #include #include #include #include #include #include #include #include #include #incl...
  • PrimeG
  • PrimeG
  • 2017-04-25 20:12
  • 105
    个人资料
    • 访问:286347次
    • 积分:6435
    • 等级:
    • 排名:第4475名
    • 原创:353篇
    • 转载:59篇
    • 译文:0篇
    • 评论:31条
    最新评论