鱼C论坛

 找回密码
 立即注册
查看: 1373|回复: 1

[技术交流] C++刷leetcode(323. 无向图中连通分量的数目)【并查集】

[复制链接]
发表于 2020-4-10 23:12:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 糖逗 于 2020-5-8 17:35 编辑

题目描述:
  1. 给定编号从 0 到 n-1 的 n 个节点和一个无向边列表(每条边都是一对节点),请编写一个函数来计算无向图中连通分量的数目。

  2. 示例 1:

  3. 输入: n = 5 和 edges = [[0, 1], [1, 2], [3, 4]]

  4.      0          3
  5.      |          |
  6.      1 --- 2    4

  7. 输出: 2
  8. 示例 2:

  9. 输入: n = 5 和 edges = [[0, 1], [1, 2], [2, 3], [3, 4]]

  10.      0           4
  11.      |           |
  12.      1 --- 2 --- 3

  13. 输出:  1
  14. 注意:
  15. 你可以假设在 edges 中不会出现重复的边。而且由于所以的边都是无向边,[0, 1] 与 [1, 0]  相同,所以它们不会同时在 edges 中出现。

  16. 来源:力扣(LeetCode)
  17. 链接:https://leetcode-cn.com/problems/number-of-connected-components-in-an-undirected-graph
  18. 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
复制代码




  1. #include <iostream>
  2. #include <vector>

  3. using namespace std;

  4. vector<int> parent;
  5. vector<int> len;
  6. int count;

  7. //寻找根节点
  8. int FindRoot(int x){
  9.         while(parent[x] != x){
  10.                 parent[x] = parent[parent[x]];
  11.                 x = parent[x];
  12.         }
  13.         return x;
  14. }


  15. void Union(int x, int y){
  16.         int rootX = FindRoot(x);
  17.         int rootY = FindRoot(y);
  18.         if(rootX == rootY) return;
  19.         if(len[rootX] >= len[rootY]){
  20.                 parent[rootY] = rootX;
  21.                 len[rootX] += len[rootY];
  22.         }
  23.         else{
  24.                 parent[rootX] = rootY;
  25.                 len[rootY] += len[rootX];
  26.         }
  27.         count--;
  28. }


  29. int solution(int n, vector<vector<int> >& edge){
  30.         count = n;
  31.         //初始化parent
  32.         for(int i = 0; i < n; i++){
  33.                 parent.push_back(i);
  34.                 len.push_back(1);
  35.         }
  36.         for(auto e : edge){
  37.                 Union(e[0], e[1]);
  38.         }
  39.         return count;
  40. }



  41. int main(void){
  42.         int n;
  43.         cout << "send the total number of nodes" << endl;
  44.         cin >> n;
  45.         cin.clear();
  46.         cout << "send the row for the input vector" << endl;
  47.         int row;
  48.         cin >> row;
  49.         cin.clear();
  50.         vector<vector<int> > input;
  51.         input.resize(row);
  52.         int number;
  53.         for(int i = 0; i < row; i++){
  54.                 for(int j = 0; j < 2; j++){
  55.                         cin >> number;
  56.                         input[i].push_back(number);
  57.                 }
  58.         }
  59.        
  60.         int res = solution(n, input);
  61.         cout << res << endl;
  62.        
  63.         return 0;
  64. }
复制代码




注意事项:
1.并查集,参考视频:https://www.bilibili.com/video/B ... 4500170862695587041
2.参考代码:https://leetcode-cn.com/problems ... jie-fa-by-kingpray/
3.并查集是常用的判断图中是否有环的数据结构

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-11 09:06:23 | 显示全部楼层
都跟你说了弄成悬赏贴(跟zltzlt的Python每日一题那样的,不过鱼币不用太多)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-1 19:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表