并查集主要解决集合的有关运算,主要操作是查找操作和并操作。

1、集合的储存方式。

为便于查找,集合通常以树结构储存,每个元素分 数据域和指针域,可以用链式储存,也可以用结构数组储存,用根节点来表示一个集合。这个性质也决定了集合中是子节点指向父节点。

typedef struct 
{
    ElementType Data;
    int Parent;
}SetType;

下标 Data Parent
0 1 -1
1 2 0
2 3 -1
3 4 0
4 5 2
5 6 -1
6 7 0
7 8 2
8 9 5
9 10 5
     

负数表示根节点,非负数表示父节点的下标

2、集合的查找。

集合既然用根节点表示,查找某元素所在的集合也就可以用集合表示了。

int Find ( SetType *S, ElementType X )
{
int i = 0;
while( i<MaxSize && S[i].Data != X )
i++;
if( i >= MaxSize ) return ERROR;
while( S[i].Parent >= 0 )
i = S[i].Parent ;
return i;
}

3、集合的并。

将x1和x2所在的集合并在一起,就是将根节点并在一起,当然前提是他们本来就不属于同一集合。

void Union ( SetType *S, ElementType X1, ElementType X2 )
{
int Root1, Root2;
Root1 = Find ( S, X1 );
Root2 = Find ( S, X2 );
if( Root1 != Root2 )
S[Root2].Parent = Root1;
}

上题:

 
 
 

How Many Tables

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

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

Source

杭电ACM省赛集训队选拔赛之热身

#include <iostream>
using namespace std; typedef int ElementType;
typedef struct
{
ElementType Data;
int Parent;
}SetType; SetType S[1001];
int MaxSize; int Find ( SetType *S, ElementType X )
{
int i = 0;
while( i<MaxSize && S[i].Data != X )
i++;
while( S[i].Parent >= 0 )
i = S[i].Parent ;
return i;
} void Union ( SetType *S, ElementType X1, ElementType X2 )
{
int Root1, Root2;
Root1 = Find ( S, X1 );
Root2 = Find ( S, X2 );
if( Root1 != Root2 )
S[Root2].Parent = Root1;
} int main()
{
int t;
cin >> t;
while ( t-- )
{
int num = 0;
int n, m, i;
cin >> n >> m;
for( i=0; i<n; i++ )
{
S[i].Parent = -1;
S[i].Data = i + 1;
}
MaxSize = n;
for( i=0; i<m; i++ )
{
int a, b;
cin >> a >> b;
Union( S, a, b );
}
for( i=0; i<n; i++ )
{
if(S[i].Parent == -1)
num++;
}
cout << num << endl;
}
return 0;
}

并查集简述 (HDU-1213-How Many Tables)的更多相关文章

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

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...

  2. HDU 1213 - How Many Tables - [并查集模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...

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

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意: 这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以 ...

  4. HDU - 1213 How Many Tables 【并查集】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意 给出N个人 M对关系 找出共有几对连通块 思路 并查集 AC代码 #include < ...

  5. HDU 1213 How Many Tables 并查集 水~

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...

  6. 并查集---体会以及模板&&How Many Tables - HDU 1213

    定义&&概念: 啥是并查集,就是将所有有相关性的元素放在一个集合里面,整体形成一个树型结构,它支持合并操作,但却不支持删除操作 实现步骤:(1)初始化,将所有节点的父亲节点都设置为自己 ...

  7. HDU 1213 How Many Tables(并查集,简单)

    题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌.简单的并查集应用,但注意题意是从1到n的,所以要减1. 代码: #include ...

  8. HDU 1213 How Many Tables (并查集,常规)

    并查集基本知识看:http://blog.csdn.net/dellaserss/article/details/7724401 题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋 ...

  9. HDU 1213 How Many Tables (并查集)

    How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...

  10. hdu 1213 How Many Tables(并查集练习)

    题目链接:hdu1213 赤裸裸的并查集.....水题一个.... #include<stdio.h> #include<string.h> #include<algor ...

随机推荐

  1. 无法安装 Microsoft Visual Studio 2010 Service Pack 1

    解决办法: 32 位系统删除:HKEY_LOCAL_MACHINE\Software\Microsoft\VSTO Designtime Setup\ 64 位系统删除:HKEY_LOCAL_MACH ...

  2. 关于项目报错Dynamic Web Module 3.0 requires Java 1.6 or newer 的解决方法

    起因:今天使用maven创建web项目的时候发现项目上老是有个红X,错误如下图所示,虽然项目能正常运行起来,但是LZ的强迫症发作,不弄掉就觉得心里不舒服,于是上网查了攻略,几经周折最终大功告成,现在就 ...

  3. solr facet查询及solrj 读取facet数据(相当有用)

    原文出自:http://www.coin163.com/java/docs/201310/d_3010029802.html 一.   Facet 简介 Facet 是 solr 的高级搜索功能之一  ...

  4. 【HDU 6031]】 Innumerable Ancestors

    题意 有一棵有n个结点的树,这里有m个询问,每个询问给出两个非空的结点集合A和B,有些结点可能同时在这两个集合当中.你需要从A和B中分别选择一个节点x和y(可以是同一个结点)你的目标是使LCA(x,y ...

  5. 使用python创建生成动态链接库dll

    如今,随着深度学习的发展,python已经成为了深度学习研究中第一语言.绝大部分的深度学习工具包都有python的版本,很多重要算法都有python版本的实现.为了将这些算法应用到具体工程中,这些工具 ...

  6. Django--form基础

    一.Django--form功能 用户提交数据验证 生成html标签 二.基础实例 需求 利用Djang​o的form功能,接收用户注册信息. urls.py 1 2 3 4 5 from app01 ...

  7. 41、Aspera下载安装运用

    参考:http://www.so.com/linkm=aLVHG%2FgJT4HyuVeK4%2BxX2LMFvF6oTiTCaruHE20pwjRia7DmVI2hIVfmw%2BFNPczCtvX ...

  8. Laravel 的 make:auth Artisan 命令到底生成了哪些文件?

    众所周知,在 Laravel 中执行 $ php artisan make:auth $ php artisan migrate 命令后,我们就能拥有一个完整的登录.注册认证系统,这为开发带来极大的便 ...

  9. 2.8.3 并发下诡异的HashMap

    package 第二章.并发下诡异的HashMap; import org.junit.Test; import java.util.HashMap;import java.util.Map;impo ...

  10. 现代C++学习笔记之二入门篇1

    现代 C++ 强调: 基于堆栈的范围,而非堆或静态全局范围. 自动类型推理,而非显式类型名称. 智能指针而不是原始指针. std::string 和 std::wstring 类型(请参见 <s ...