# Kruskal 算法

假设 G=(V,E) 是连通图,将 G 中的边按权值从小到大的顺序排列

  • 将 n 个顶点看成 n 个集合
  • 按权值从大到小的顺序选择边,所选边应满足两个顶点不在同一个顶点集合内,即加入此边后不会在生成树中产生回路,将该边放到生成树边的集合中。同时将该边的两个顶点所在的顶点集合合并。
  • 重复 2,直到所有的顶点都在同一个顶点集合内。

# 并查集

  1. Make_Set (x) 把每一个元素初始化为一个集合
    初始化后每一个元素的父亲节点是它本身,每一个元素的祖先节点也是它本身(也可以根据情况而变)。
  2. Find_Set (x) 查找一个元素所在的集合
    查找一个元素所在的集合,其精髓是找到这个元素所在集合的祖先!这个才是并查集判断和合并的最终依据。
    判断两个元素是否属于同一集合,只要看他们所在集合的祖先是否相同即可。
    合并两个集合,也是使一个集合的祖先成为另一个集合的祖先,具体见示意图
  3. Union (x,y) 合并 x,y 所在的两个集合
    合并两个不相交集合操作很简单:
    利用 Find_Set 找到其中两个集合的祖先,将一个集合的祖先指向另一个集合的祖先。

# 优化

  1. Find_Set (x) 时 路径压缩
    寻找祖先时我们一般采用递归查找,但是当元素很多亦或是整棵树变为一条链时,每次 Find_Set (x) 都是 O (n) 的复杂度,有没有办法减小这个复杂度呢?
    答案是肯定的,这就是路径压缩,即当我们经过 "递推" 找到祖先节点后,"回溯" 的时候顺便将它的子孙节点都直接指向祖先,这样以后再次 Find_Set (x) 时复杂度就变成 O (1) 了,如下图所示;可见,路径压缩方便了以后的查找。
  2. Union (x,y) 时 按高度合并
    即合并的时候将元素少的集合合并到元素多的集合中,这样合并之后树的高度会相对较小。
int father[MAX]; /* father [x] 表示 x 的父节点 */
int rank[MAX];   /* rank [x] 表示 x 的高度 */
/* 初始化集合 */
void Make_Set(int x)
{
    father[x] = x; // 根据实际情况指定的父节点可变化
    rank[x] = 0;   // 根据实际情况初始化高度也有所变化
}
/* 非递归 Make_Set*/
// void Make_Set(int x)
// {
//     father[x] = -1;
//     rank[x] = 0;
// }
/* 查找 x 元素所在的集合,回溯时压缩路径 */
int Find_Set(int x)
{
    if (x != father[x])
    {
        father[x] = Find_Set(father[x]); // 这个回溯时的压缩路径是精华
    }
    return father[x];
}
/* 非递归 Find_Set*/
// int Find_Set(int x)
// {
//     while (father[x] != -1)
//     {
//         x = father[x];
//     }
//     return x;
// }
/*
   按高度合并 x,y 所在的集合
   下面的那个 if else 结构不是绝对的,具体根据情况变化
   但是,宗旨是不变的即,按高度合并,实时更新高度。
*/
void Union(int x, int y)
{
    x = Find_Set(x);
    y = Find_Set(y);
    if (x == y)
        return;
    if (rank[x] > rank[y])
    {
        father[y] = x;
    }
    else
    {
        if (rank[x] == rank[y])
        {
            rank[y]++;
        }
        father[x] = y;
    }
}

# 题目

It is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.

Input

The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.

Output

For each case, output the number of suspects in one line.

Sample Input & Output

100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
4
200 2
1 5
5 1 2 3 4 5
1
1 0
1
0 0

# 递归版本

#include <iostream>
using namespace std;
int n, m, i, j;
int father[30005], num[30005];
void makeSet(int n)
{
    for (i = 0; i < n; i++)
    {
        father[i] = i; // 使用本身做根
        num[i] = 1;
    }
}
int findSet(int x)
{
    if (father[x] != x) // 合并后的树的根是不变的
    {
        father[x] = findSet(father[x]);
    }
    return father[x];
}
void Union(int a, int b)
{
    int x = findSet(a);
    int y = findSet(b);
    if (x == y)
    {
        return;
    }
    if (num[x] <= num[y])
    {
        father[x] = y;
        num[y] += num[x];
    }
    else
    {
        father[y] = x;
        num[x] += num[y];
    }
}
int main()
{
    while (scanf("%d %d", &n, &m) != EOF && n != 0)
    {
        makeSet(n);
        for (i = 0; i < m; i++)
        {
            int count, first, b;
            scanf("%d %d", &count, &first);
            for (j = 1; j < count; j++)
            {
                scanf("%d", &b);
                Union(first, b);
            }
        }
        printf("%d\n", num[findSet(0)]);
    }
    return 0;
}

并查集 -- 学习详解

更新於 閱讀次數

請我喝[茶]~( ̄▽ ̄)~*

fygod 微信支付

微信支付

fygod 支付寶

支付寶

fygod PayPal

PayPal