无码av一区二区三区无码,在线观看老湿视频福利,日韩经典三级片,成 人色 网 站 欧美大片在线观看

歡迎光臨散文網(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講]的評論 (共 條)

分享到微博請遵守國家法律
监利县| 诏安县| 巨野县| 仲巴县| 麻栗坡县| 元氏县| 方山县| 东乡| 报价| 盘锦市| 漳平市| 韶关市| 读书| 西华县| 永胜县| 禹城市| 永济市| 岳阳市| 凤山县| 福安市| 阿拉善左旗| 杂多县| 策勒县| 中卫市| 全椒县| 蚌埠市| 邢台市| 出国| 晋中市| 深圳市| 伊金霍洛旗| 新昌县| 久治县| 舞阳县| 哈尔滨市| 东乌珠穆沁旗| 揭西县| 南昌县| 浦北县| 西畴县| 八宿县|