文章目录

  • 1. 题目
  • 2. 解题
    • 2.1 BFS
    • 2.2 DFS
    • 2.3 并查集

1. 题目

用以太网线缆将 n 台计算机连接成一个网络,计算机的编号从 0 到 n-1。
线缆用 connections 表示,其中 connections[i] = [a, b] 连接了计算机 a 和 b。

网络中的任何一台计算机都可以通过网络直接或者间接访问同一个网络中其他任意一台计算机。

给你这个计算机网络的初始布线 connections,你可以拔开任意两台直连计算机之间的线缆,并用它连接一对未直连的计算机。
请你计算并返回使所有计算机都连通所需的最少操作次数。如果不可能,则返回 -1 。

示例 1:
输入:n = 4, connections = [[0,1],[0,2],[1,2]]
输出:1
解释:拔下计算机 1 和 2 之间的线缆,并将它插到计算机 1 和 3 上。

示例 2:
输入:n = 6, connections = [[0,1],[0,2],[0,3],[1,2],[1,3]]
输出:2示例 3:
输入:n = 6, connections = [[0,1],[0,2],[0,3],[1,2]]
输出:-1
解释:线缆数量不足。示例 4:
输入:n = 5, connections = [[0,1],[0,2],[3,4],[2,3]]
输出:0提示:
1 <= n <= 10^5
1 <= connections.length <= min(n*(n-1)/2, 10^5)
connections[i].length == 2
0 <= connections[i][0], connections[i][1] < n
connections[i][0] != connections[i][1]
没有重复的连接。
两台计算机不会通过多条线缆连接。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-operations-to-make-network-connected
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

  • 首先至少需要有n-1条连接,才可能满足题意
  • 其次,求有多少个集团,集团的个数-1就是需要改的线缆数量
  • 可以用BFS、DFS、并查集来求有多少个集团

2.1 BFS

class Solution {unordered_map<int,unordered_set<int>> m;
public:int makeConnected(int n, vector<vector<int>>& connections) {if(connections.size() < n-1)return -1;vector<int> visited(n,false);for(auto& c : connections){m[c[0]].insert(c[1]);m[c[1]].insert(c[0]);}int count = 0, tp;queue<int> q;for(int i = 0; i < n; ++i){if(!visited[i]){count++;visited[i] = true;q.push(i);while(!q.empty()){tp = q.front();q.pop();for(auto c : m[tp]){if(!visited[c]){q.push(c);visited[c] = true;}}}}}return count-1;}
};

432 ms 58.9 MB

2.2 DFS

class Solution {unordered_map<int,unordered_set<int>> m;
public:int makeConnected(int n, vector<vector<int>>& connections) {if(connections.size() < n-1)return -1;vector<int> visited(n,false);for(auto& c : connections){m[c[0]].insert(c[1]);m[c[1]].insert(c[0]);}int count = 0;for(int i = 0; i < n; ++i){if(!visited[i]){count++;visited[i] = true;dfs(i,visited);}}return count-1;}void dfs(int i, vector<int>& visited){for(auto c : m[i]){if(!visited[c]){visited[c] = true;dfs(c, visited);}}}
};

392 ms 59.4 MB

2.3 并查集

并查集参考:数据结构–并查集(Disjoint-Set)

class dsu
{public:vector<int> f;dsu(int n){f.resize(n);for(int i = 0; i < n; ++i)f[i] = i;}int find(int x){if(x == f[x])return x;return f[x] = find(f[x]);}void merge(int x, int y){int fx = find(x);int fy = find(y);f[fx] = fy;}int countuni(){int count = 0;for(int i = 0; i < f.size(); ++i){if(i == find(i))count++;}return count;}
};class Solution {public:int makeConnected(int n, vector<vector<int>>& connections) {if(connections.size() < n-1)return -1;dsu uni(n);for(auto& c : connections)uni.merge(c[0],c[1]);return uni.countuni()-1;}
};

176 ms 29 MB

可以看出并查集占用内存较少,运行时间较快。

LeetCode 1319. 连通网络的操作次数(BFS/DFS/并查集)相关推荐

  1. leetcode —— 1319. 连通网络的操作次数

    用以太网线缆将 n 台计算机连接成一个网络,计算机的编号从 0 到 n-1.线缆用 connections 表示,其中 connections[i] = [a, b] 连接了计算机 a 和 b. 网络 ...

  2. leetcode 1319. 连通网络的操作次数(并查集)

    用以太网线缆将 n 台计算机连接成一个网络,计算机的编号从 0 到 n-1.线缆用 connections 表示,其中 connections[i] = [a, b] 连接了计算机 a 和 b. 网络 ...

  3. 1319. 连通网络的操作次数

    链接:1319. 连通网络的操作次数 题解: 1.图的bfs遍历 class Solution {public:void bfs(std::unordered_map<int, std::vec ...

  4. 【每日一题】 1319. 连通网络的操作次数

    [每日一题] 1319. 连通网络的操作次数 避免每日太过咸鱼,一天搞定一道LeetCode算法题 一.题目描述 用以太网线缆将 n 台计算机连接成一个网络,计算机的编号从 0 到 n-1.线缆用 c ...

  5. 【力扣】1319. 连通网络的操作次数

    以下为力扣官方题解 1319. 连通网络的操作次数 题目 示例1 示例2 示例3 示例4 提示 官方题解 思路一 深度优先搜索 代码 复杂度分析 思路二 并查集 代码 复杂度分析 题目 用以太网线缆将 ...

  6. leetcode1319. 连通网络的操作次数

    leetcode1319. 连通网络的操作次数 题目描述 链接: leetcode1319. 用以太网线缆将n台计算机连接成一个网络,计算机的编号从0到n-1.线缆用connections表示,其中c ...

  7. leetcode-1319:连通网络的操作次数

    leetcode-1319:连通网络的操作次数 题目 解题 方法一:并查集 题目 题目连接 用以太网线缆将 n 台计算机连接成一个网络,计算机的编号从 0 到 n-1.线缆用 connections ...

  8. 【数据结构与算法】之连通网络的操作次数的算法

    一.题目要求 用以太网线缆将 n 台计算机连接成一个网络,计算机的编号从 0 到 n-1.线缆用 connections 表示,其中 connections[i] = [a, b] 连接了计算机 a ...

  9. leetcode1319. 连通网络的操作次数(并查集)

    用以太网线缆将 n 台计算机连接成一个网络,计算机的编号从 0 到 n-1.线缆用 connections 表示,其中 connections[i] = [a, b] 连接了计算机 a 和 b. 网络 ...

最新文章

  1. 进程(process)和线程(thread)
  2. TF卡里删掉文件后内存没变大_内存卡损坏怎么修复?数据恢复方法教程
  3. 基本类型--枚举类型和位标志
  4. 网页与 alert() 一样用于弹框却比之多了一个取消选项的方法 confirm()
  5. 金融系列5《AUTH过程》
  6. 关于《PHP任务学习》的说明
  7. java 登陆微信获取好友列表_微信api接口,触发推送微信好友列表及返回
  8. 创建阻止windows自动锁屏的脚本
  9. 虚拟小镇意识保存~认识脑电波
  10. 同花顺面经(二面挂)
  11. 【Unity】Unity 基本介绍
  12. 用turtle画奥运五环
  13. 农村经济与科技杂志农村经济与科技杂志社农村经济与科技编辑部2022年第9期目录
  14. python的输出语法_Python入门语法综述
  15. Golang日志框架lumberjack包源码分析
  16. 关于springboot整合mybatisplus中找不到数据库中的表的问题
  17. 春天的马车曲(横光利一)
  18. 【程序源代码】小程序最佳开发实践-租房小程序
  19. 从客户端到服务器端,适配微信iOS OpenSDK中的Universal Links
  20. html期末作业代码网页设计——云南民族文化(8页) HTML+CSS+JavaScript html网页制作期末大作业成品_网页设计期末作业

热门文章

  1. mac 编译android系统,mac 编译 Android 系统杂记
  2. aix oracle 10.2.0.1 升级 10.2.0.4,AIX Oracle RAC 升级到10.2.0.4.0要特别注意的问题 - 爱肯的专栏 ......
  3. mysql 单标递归_MySql8 WITH RECURSIVE递归查询父子集的方法
  4. CNN的发展历史(LeNet,Alexnet,VGGNet,GoogleNet,ReSNet)
  5. [转]VisualStudio如何组织解决方案的目录结构
  6. 第二季3:海思MPP模块与视频缓冲池
  7. Find The Multiple POJ - 1426 (BFS)
  8. 看雪KSSD-windows驱动
  9. xhprof windows下安装和使用(转载)
  10. 使用eclipse以及Juint进行测试