杭电1213题解:一道最基础的并查集问题

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

根据题意我们可以知道,这是一道并查集的基础问题,不涉及路径也不涉及连通分量内部元素的关系,只求连通分量的个数,由于本题数据并不是很大,我们可以简单地使用Quick-Union算法即可(甚至Quick-Find算法也可)

根据Quick-Union算法,令每次输入的两个元素所在的树根相等,即id[tree(mi)] = id[tree(mj)]  (tree函数是索引目标元素树根的函数), 最后统计树根的个数(即id[x] = x)

下面看代码

 1 #include <iostream>
 2 #include <cstring>
 3
 4 using namespace std;
 5 int id[1005];
 6 int tree(int x)
 7 {
 8     while(id[x]!=x)
 9     {
10         x = id[x];
11     }
12     return id[x];
13 }
14
15 int main()
16 {
17     int T;
18     int i = 0, j = 0;
19     int n, m;
20
21     int mi, mj;
22     int counts;
23     cin >> T;
24     for(i=0; i<T; i++)
25     {
26         counts = 0;
27         memset(id, 0, sizeof(id));
28         cin >> n >> m;
29         for(j=0; j<=n; j++){
30             id[j] = j;
31         }
32         for(j=1; j<=m; j++)
33         {
34             cin >> mi >> mj;
35             id[tree(mi)] = id[tree(mj)];
36         }
37         for(j=1; j<=n; j++)
38         {
39             if(id[j]==j)
40                counts++;
41         }
42
43         cout << counts << endl;
44     }
45     return 0;
46 }
时间: 2024-10-10 01:48:46

杭电1213题解:一道最基础的并查集问题的相关文章

hdu1829 A Bug&#39;s Life 基础种类并查集

题目的大意可以理解为:A爱B,B爱C ……给出一系列爱恋的关系,推断有没有同性恋. 思路是把相同性别的归为一个集合,异性的异性为同性. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=2010; int f[N], r[N], flag; void init() { for(int i=1;i&

hdu 1829 A Bug&#39;s Life (基础种类并查集)

先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组,一个存并查集内的父子关系,一个存各个节点所属的种类关系. 以这道题为例(题意在后面,如果没有读题,可以先看完题在来看这部分)—— 这道题很明显,将bug分成两类,一公一母.但是实际上我们并不关心它是公的还是母的,只关心它们之间是同性还是异性.所以,我们可以设与并查集的根节点同性的为0,反之为1.所

1213 How Many Tables 简单的并查集问题

my code: #include <cstdio>#include <cstring>#include<iostream>using namespace std;int find(int num,int A []){while(num!=A[num])//{ num = A[num];return num;}// bool follow(int a,int b,int A[]){int i = find(a,A);int j = find(b,A);//cout<

零基础学并查集算法

并查集是我暑假从高手那里学到的一招,觉得真是太精妙的设计了.以前我无法解决的一类问题竟然可以用如此简单高效的方法搞定.不分享出来真是对不起party了.(party:我靠,关我嘛事啊?我跟你很熟么?) 来看一个实例,杭电1232畅通工程 首先在地图上给你若干个城镇,这些城镇都可以看作点,然后告诉你哪些对城镇之间是有道路直接相连的.最后要解决的是整幅图的连通性问题.比如随意给你两个点,让你判断它们是否连通,或者问你整幅图一共有几个连通分支,也就是被分成了几个互相独立的块.像畅通工程这题,问还需要修

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): 15083    Accepted Submission(s): 7361 Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinn

HDU 1213 How Many Tables(模板——并查集)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 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 fri

hdu杭电1213 How Many Tables【并查集】

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

杭电 1856 More is better (并查集求最大集合)

Description Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements. Mr Wang selected a room big enough to hold the boys. The boy w

hdu 1829 A Bug&#39;s Life (基础并查集)

题目: 链接:点击打开链接 题意: 给定虫子的交配关系,确定实验是否支持教授的假设即没有同性恋或者不符合假设. 思路: 是一道基础的并查集题目.存在两个集合异性和同性,给出多组关系,看这两个集合有木有联系,即是否有同性恋. 定义一个数组sex[],sex[i]表示与编号i的性别相反的虫子编号.然后将和i虫子有联系的合并为同一个集合(认为是同性的).如果findset(u) == findset(v),出现了反常行为. 代码: #include <iostream> #include <c