当前位置:网站首页>Force buckle 1319 Number of connected network operations
Force buckle 1319 Number of connected network operations
2022-06-23 10:55:00 【A ruthless young Fisherman】
subject
Connect... With Ethernet cable n Computers are connected into a network , The number of the computer is from 0 To n-1. For cable connections Express , among connections[i] = [a, b] Connected to the computer a and b.
Any computer in the network can directly or indirectly access any other computer in the same network through the network .
Give you the initial wiring of this computer network connections, You can disconnect the cable between any two directly connected computers , And use it to connect a pair of computers that are not directly connected . Please calculate and return the minimum number of operations required to connect all computers . If not , Then return to -1 .
Example

Input :n = 4, connections = [[0,1],[0,2],[1,2]]
Output :1
explain : Unplug the computer 1 and 2 The cable between , And plug it into the computer 1 and 3 On .

Input :n = 6, connections = [[0,1],[0,2],[0,3],[1,2],[1,3]]
Output :2
Input :n = 6, connections = [[0,1],[0,2],[0,3],[1,2]]
Output :-1
explain : Insufficient number of cables .
Input :n = 5, connections = [[0,1],[0,2],[3,4],[2,3]]
Output :0
source : Power button (LeetCode)
link :https://leetcode.cn/problems/number-of-operations-to-make-network-connected
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Method 1: Union checking set 
Java Realization
Balanced optimization :size Array . Connect the small tree to the big one
Path compression :find()
class Solution {
public int makeConnected(int n, int[][] connections) {
if (connections.length < n - 1) return -1;
UF uf = new UF(n);
for (int[] con : connections) {
uf.unite(con[0], con[1]);
}
return uf.count - 1;
}
class UF {
private int count; // The number of connected components
int n; // Number of nodes in the graph
private int[] parent; // Stores the parent node of each node
private int[] size; // Number of each node , Make sure the little tree is under the big tree
public UF(int n) {
this.count = n;
this.parent = new int[n];
this.size = new int[n];
Arrays.fill(size, 1);
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
// Find the root node of each node
public int find(int x) {
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
// The nodes x And nodes y connected
public boolean unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return false;
if (size[x] < size[y]) {
int tmp = x;
x = y;
y = tmp;
}
parent[y] = x;
size[x] += size[y];
count--;
return true;
}
public boolean connected(int x, int y) {
x = find(x);
y = find(y);
return x == y;
}
}
}

边栏推荐
- Flush is the stock market? Is online account opening safe?
- Too helpless! Microsoft stopped selling AI emotion recognition and other technologies, saying frankly: "the law can not keep up with the development of AI"
- NOI OJ 1.3 05:计算分数的浮点数值 C语言
- Torch weight to mindspore
- NOI OJ 1.3 04:带余除法 C语言
- NOI OJ 1.3 17:计算三角形面积 C语言
- Large homework collection
- Google Earth Engine(GEE)——GEDI L2A Vector Canopy Top Height (Ver
- TTY驱动框架
- Unity technical manual - lifecycle lifetimebyemitterspeed - color in the cycle coloroverlifetime- speed color colorbyspeed
猜你喜欢

最简单DIY串口蓝牙硬件实现方案

【黄金分割点】与【斐波那契数列】

UWA new | real person real machine test new overseas model zone

Step by step introduction to sqlsugar based development framework (9) -- Realizing field permission control with WinForm control

Solve the problem of invalid audio autoplay

web技术分享| 【高德地图】实现自定义的轨迹回放

Win10 微软输入法(微软拼音) 不显示 选字栏(无法选字) 解决方法

Interview Manual of social recruitment Tencent high P (Senior Product Manager)

Economic common sense

Solve the problem that Preview PDF cannot be downloaded
随机推荐
Installation and use of binabsinspector, an open source binary file static vulnerability analysis tool
最简单DIY基于蓝牙、51单片机和舵机的钢铁爱国者机关枪控制器
ESP32-CAM高性价比温湿度监控系统
Experience of using thread pool in project
经济小常识
MySQL-02. Understanding of indexes at work
NOI OJ 1.2 整数数据类型存储空间大小
最简单DIY基于51单片机、PCA9685、IIC、云台的舵机集群控制程序
Deep dive kotlin synergy (XIV): problems of shared state
Description of directory files of TLBB series of Tianlong Babu - netbill server [ultra detailed]
Economic common sense
Solve the problem of invalid audio autoplay
JVM简单入门-01
程序中创建一个子进程,然后父子进程各自独自运行,父进程在标准输入设备上读入小写字母,写入管道。子进程从管道读取字符并转化为大写字母。读到x结束
How to write a literature review? What should I do if I don't have a clue?
Stockage d'images - référence
分享一个手游脚本源码
NOI OJ 1.3 09:与圆相关的计算 C语言
1154. 一年中的第几天
MySQL基础-笔记