连接图 III-LintCode

本文介绍了一种使用并查集算法来维护图的连通性的方法。通过connect(a, b)函数添加边,并用query()返回当前图中连通区域的数量。文章详细解释了并查集的基本原理及其在图算法中的应用。

给一个图中的 n 个节点, 记为 1 到 n . 在开始的时候图中没有边.
你需要完成下面两个方法:
1. connect(a, b), 添加一条连接节点 a, b的边
2. query(), 返回图中联通区域个数

样例

5 // n = 5
query() 返回 5
connect(1, 2)
query() 返回 4
connect(2, 4)
query() 返回 3
connect(1, 4)
query() 返回 3

思路
利用并查集的思想,对于每个节点求其最高层的节点,并压缩路径。设置cnt为n表示初始的连通域的个数,num[i]=i 。连接点a,b先判断其最高层的节点,若相同则a,b已经在同一个连通域内,否则,合并连通域。

#ifndef C591_H
#define C591_H
#include<iostream>
#include<vector>
using namespace std;
class ConnectingGraph3 {
public:
    /*
    * @param n: An integer
    */ConnectingGraph3(int n) {
        // do intialization if necessary
        //初始化nums的值
        for (int i = 0; i <= n; ++i)
            nums.push_back(i);
        cnt = n;
    }

      /*
      * @param a: An integer
      * @param b: An integer
      * @return: nothing
      */
      void connect(int a, int b) {
          // write your code here
          int root1 = unionSearch(a, nums);
          int root2 = unionSearch(b, nums);
          //若其顶层节点不同,合并顶层节点
          if (root1 != root2)
          {
              nums[root1] = root2;
              cnt--;
          }
      }

      /*
      * @return: An integer
      */
      int query() {
          // write your code here
          return cnt;
      }
      //返回root最顶层的值,并进行路径压缩
      int unionSearch(int root, vector<int>& nums)
      {
          while (root != nums[root])
          {
              nums[root] = nums[nums[root]];
              root = nums[root];
          }
          return root;
      }
      vector<int> nums;
      int cnt;
};
#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值