最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

【算法】并查集(Disjoint Set)[共3講]

2023-07-13 16:08 作者:wechannnnnn  | 我要投稿

Golang版本代碼

package main

import "fmt"

const VERTICES = 6

func initialise(parent []int) {

for i := 0; i < VERTICES; i++ {

parent[i] = -1

}

}

func find_root(x int, parent []int) int {

x_root := x

for parent[x_root] != -1 {

x_root = parent[x_root]

}

return x_root

}

func union_vertices(x, y int, parent, rank []int) int {

x_root := find_root(x, parent)

y_root := find_root(y, parent)

if x_root == y_root {

return 0

} else {

if rank[x_root] > rank[y_root] {

parent[y_root] = x_root

} else if rank[y_root] > rank[x_root] {

parent[x_root] = y_root

} else {

parent[x_root] = y_root

rank[y_root]++

}

}

return 1

}

func main() {

parent := make([]int, VERTICES, VERTICES)

rank := make([]int, VERTICES, VERTICES)

edges := [][]int{

{0, 1}, {1, 2}, {1, 3},

{3, 4}, {2, 5},

}

initialise(parent)

for _, edge := range edges {

x := edge[0]

y := edge[1]

if union_vertices(x, y, parent, rank) == 0 {

fmt.Println("Cycle detected!")

return

}

}

fmt.Println("No cycles found.")

}

【算法】并查集(Disjoint Set)[共3講]的評論 (共 條)

分享到微博請遵守國家法律
堆龙德庆县| 和田市| 扎兰屯市| 华池县| 盐山县| 两当县| 临潭县| 临安市| 汶川县| 东明县| 山阴县| 澄城县| 寿阳县| 潜江市| 新和县| 河西区| 保康县| 吴江市| 抚宁县| 府谷县| 海城市| 武夷山市| 中方县| 开阳县| 密云县| 临沂市| 新乡县| 赤水市| 保山市| 克东县| 尖扎县| 淳化县| 保亭| 镇平县| 雷州市| 兖州市| 梅州市| 广宗县| 乳源| 金川县| 巩义市|