1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// @Title: 保证图可完全遍历 (Remove Max Number of Edges to Keep Graph Fully Traversable)
// @Author: 15816537946@163.com
// @Date: 2021-01-27 23:52:48
// @Runtime: 380 ms
// @Memory: 19 MB

type unionFind struct {
	parent, size []int
	setCount     int // 当前连通分量数目
}

func newUnionFind(n int) *unionFind {
	parent := make([]int, n)
	size := make([]int, n)
	for i := range parent {
		parent[i] = i
		size[i] = 1
	}
	return &unionFind{parent, size, n}
}

func (uf *unionFind) find(x int) int {
	if uf.parent[x] != x {
		uf.parent[x] = uf.find(uf.parent[x])
	}
	return uf.parent[x]
}

func (uf *unionFind) union(x, y int) bool {
	fx, fy := uf.find(x), uf.find(y)
	if fx == fy {
		return false
	}
	if uf.size[fx] < uf.size[fy] {
		fx, fy = fy, fx
	}
	uf.size[fx] += uf.size[fy]
	uf.parent[fy] = fx
	uf.setCount--
	return true
}

func (uf *unionFind) inSameSet(x, y int) bool {
	return uf.find(x) == uf.find(y)
}

func maxNumEdgesToRemove(n int, edges [][]int) int {
	ans := len(edges)
	alice, bob := newUnionFind(n), newUnionFind(n)
	for _, e := range edges {
		x, y := e[1]-1, e[2]-1
		if e[0] == 3 && (!alice.inSameSet(x, y) || !bob.inSameSet(x, y)) {
			// 保留这条公共边
			alice.union(x, y)
			bob.union(x, y)
			ans--
		}
	}
	uf := [2]*unionFind{alice, bob}
	for _, e := range edges {
		if tp := e[0]; tp < 3 && uf[tp-1].union(e[1]-1, e[2]-1) {
			// 保留这条独占边
			ans--
		}
	}
	if alice.setCount > 1 || bob.setCount > 1 {
		return -1
	}
	return ans
}