-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveMaxNumberOfEdges.java
More file actions
158 lines (146 loc) · 5.67 KB
/
RemoveMaxNumberOfEdges.java
File metadata and controls
158 lines (146 loc) · 5.67 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package leetcode;
import java.util.LinkedList;
import java.util.List;
/**
* RemoveMaxNumberOfEdges
* https://leetcode-cn.com/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/
* 1579. 保证图可完全遍历
* https://leetcode-cn.com/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/solution/bing-cha-ji-xue-xi-by-oshdyr-20c0/
*
* @author tobin
* @since 2021-01-27
*/
public class RemoveMaxNumberOfEdges {
public static void main(String[] args) {
RemoveMaxNumberOfEdges sol = new RemoveMaxNumberOfEdges();
System.out.println(sol.maxNumEdgesToRemove(4,
new int[][]{{3, 1, 2}, {3, 2, 3}, {1, 1, 3}, {1, 2, 4}, {1, 1, 2}, {2, 3, 4}}));
System.out.println(sol.maxNumEdgesToRemove(4,
new int[][]{{3, 1, 2}, {3, 2, 3}, {1, 1, 4}, {2, 1, 4}}));
System.out.println(sol.maxNumEdgesToRemove(4,
new int[][]{{3, 2, 3}, {1, 1, 2}, {2, 3, 4}}));
System.out.println(sol.maxNumEdgesToRemove(4,
new int[][]{{3, 1, 2}, {3, 3, 4}, {1, 1, 3}, {2, 2, 4}}));
}
public int maxNumEdgesToRemove(int n, int[][] edges) {
List<int[]> alice = new LinkedList<>();
List<int[]> bob = new LinkedList<>();
List<int[]> both = new LinkedList<>();
for (int[] edge : edges) {
switch (edge[0]) {
case 1:
alice.add(edge);
break;
case 2:
bob.add(edge);
break;
default:
both.add(edge);
}
}
int edgeUsed = 0;
// 由共同路径可以构建的树
int[] unionSet = new int[n];
for (int i = 0; i < n; i++) {
unionSet[i] = -1;
}
for (int[] edge : both) {
int i = edge[1] - 1;
int j = edge[2] - 1;
if (unionSet[i] < 0 && unionSet[j] < 0) {
unionSet[i] = i;
unionSet[j] = i;
} else if (unionSet[i] >= 0 && unionSet[j] >= 0) {
if (parent(unionSet, i) == parent(unionSet, j)) {
// skip add edge
continue;
}
// 合并两个子树
// random select one, actually should use small-subTree attach to big-subTree
// 需要找到两个的父亲进行合并
unionSet[parent(unionSet, j)] = parent(unionSet, i);
} else if (unionSet[i] >= 0) {
unionSet[j] = parent(unionSet, i);
} else { // unionSet[j] >= 0
unionSet[i] = parent(unionSet, j);
}
edgeUsed++;
}
// alice路径可以构建的树
int[] aliceUnionSet = new int[n];
for (int i = 0; i < n; i++) {
aliceUnionSet[i] = unionSet[i];
}
for (int[] edge : alice) {
int i = edge[1] - 1;
int j = edge[2] - 1;
if (aliceUnionSet[i] < 0 && aliceUnionSet[j] < 0) {
aliceUnionSet[i] = i;
aliceUnionSet[j] = i;
} else if (aliceUnionSet[i] >= 0 && aliceUnionSet[j] >= 0) {
if (parent(aliceUnionSet, i) == parent(aliceUnionSet, j)) {
// skip add edge
continue;
}
// 合并两个子树
// random select one, actually should use small-subTree attach to big-subTree
// 需要找到两个的父亲进行合并
aliceUnionSet[parent(aliceUnionSet, j)] = parent(aliceUnionSet, i);
} else if (aliceUnionSet[i] >= 0) {
aliceUnionSet[j] = parent(aliceUnionSet, i);
} else { // unionSet[j] >= 0
aliceUnionSet[i] = parent(aliceUnionSet, j);
}
edgeUsed++;
}
// check alice
int aliceParent = parent(aliceUnionSet, 0);
for (int i = 0; i < n; i++) {
if (aliceParent != parent(aliceUnionSet, i)) {
return -1;
}
}
// bob路径可以构建的树
int[] bobUnionSet = new int[n];
for (int i = 0; i < n; i++) {
bobUnionSet[i] = unionSet[i];
}
for (int[] edge : bob) {
int i = edge[1] - 1;
int j = edge[2] - 1;
if (bobUnionSet[i] < 0 && bobUnionSet[j] < 0) {
bobUnionSet[i] = i;
bobUnionSet[j] = i;
} else if (bobUnionSet[i] >= 0 && bobUnionSet[j] >= 0) {
if (parent(bobUnionSet, i) == parent(bobUnionSet, j)) {
// skip add edge
continue;
}
// 合并两个子树
// random select one, actually should use small-subTree attach to big-subTree
// 需要找到两个的父亲进行合并
bobUnionSet[parent(bobUnionSet, j)] = parent(bobUnionSet, i);
} else if (bobUnionSet[i] >= 0) {
bobUnionSet[j] = parent(bobUnionSet, i);
} else { // unionSet[j] >= 0
bobUnionSet[i] = parent(bobUnionSet, j);
}
edgeUsed++;
}
// check bob
int bobParent = parent(bobUnionSet, 0);
for (int i = 0; i < n; i++) {
if (bobParent != parent(bobUnionSet, i)) {
return -1;
}
}
return edges.length - edgeUsed;
}
private int parent(int[] unionSet, int idx) {
int oldParent = unionSet[idx];
if (oldParent >= 0 && oldParent != idx) {
unionSet[idx] = parent(unionSet, oldParent);
}
return unionSet[idx];
}
}