无码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講]的評論 (共 條)

分享到微博請遵守國家法律
泸溪县| 合水县| 安溪县| 沙田区| 高陵县| 湘潭市| 惠来县| 隆回县| 正定县| 德格县| 绿春县| 洪雅县| 元谋县| 西林县| 孟州市| 中方县| 方正县| 灯塔市| 齐河县| 集贤县| 乌兰县| 延长县| 天门市| 旌德县| 霍林郭勒市| 湘西| 会泽县| 湖口县| 鸡西市| 色达县| 盱眙县| 石嘴山市| 姜堰市| 永登县| 精河县| 麻江县| 延长县| 霞浦县| 进贤县| 科技| 股票|