-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode2867.java
More file actions
260 lines (236 loc) · 8.48 KB
/
LeetCode2867.java
File metadata and controls
260 lines (236 loc) · 8.48 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import java.util.List;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Arrays;
public class LeetCode2867 {
public static void main(String[] args) {
// 输入:n = 5, edges = [[1,2],[1,3],[2,4],[2,5]]
// 输出:4
System.out.println(new Solution2867_2().countPaths(5, new int[][] { { 1, 2 },
{
1, 3 },
{ 2, 4 }, { 2, 5 } }));
// 输入:n = 6, edges = [[1,2],[1,3],[2,4],[3,5],[3,6]]
// 输出:6
System.out.println(
new Solution2867_2().countPaths(6, new int[][] { { 1, 2 }, { 1, 3 }, { 2, 4
},
{ 3, 5 }, { 3, 6 } }));
// 输入: n = 1, edges = []
// 输出: 0
System.out.println(
new Solution2867_2().countPaths(1, new int[][] {}));
// 输入: n = 4, edges = [[1,2],[4,1],[3,4]]
// 输出: 4
System.out.println(
new Solution2867_2().countPaths(4, new int[][] { { 1, 2 }, { 4, 1 }, { 3, 4 }
}));
// 输入: n = 5, edges = [[1,3],[4,3],[2,3],[5,2]]
// 输出: 3
System.out.println(
new Solution2867_2().countPaths(5, new int[][] { { 1, 3 }, { 4, 3 }, { 2, 3
},
{ 5, 2 } }));
// 输入: n = 5, edges = [[4,1],[5,4],[2,1],[3,4]]
// 输出: 6
System.out.println(
new Solution2867_2().countPaths(5, new int[][] { { 4, 1 }, { 5, 4 }, { 2, 1
},
{ 3, 4 } }));
}
}
/**
* 以合数为中心,搜素所有质数数量为0的路径
* 由于是树结构,因此不会存在环,所以求路径树就是求节点数
*/
class Solution2867_1 {
public long countPaths(int n, int[][] edges) {
boolean[] isPrimes = loadAllPrimes(n);
ArrayList<Integer>[] tree = buildTree(n, edges);
// System.out.println(Arrays.toString(isPrimes));
long result = 0;
Integer[] dp = new Integer[n + 1];
HashSet<Integer> visited;
for (int ind = 1; ind <= n; ind++) {
if (isPrimes[ind]) {
List<Integer> neighborCounts = new ArrayList<>();
for (int neighbor : tree[ind]) {
if (!isPrimes[neighbor]) {
if (dp[neighbor] == null) {
visited = new HashSet<Integer>();
dfs(neighbor, tree, isPrimes, visited);
dp[neighbor] = visited.size();
for (int k : tree[neighbor]) {
if (!isPrimes[k]) {
dp[k] = dp[neighbor];
}
}
}
neighborCounts.add(dp[neighbor]);
}
}
int sum = 0;
for (int i = 0; i < neighborCounts.size(); i++) {
result += neighborCounts.get(i);
sum += neighborCounts.get(i);
}
long temp = 0;
for (int i = 0; i < neighborCounts.size(); i++) {
temp += (long) neighborCounts.get(i) * (sum - neighborCounts.get(i));
}
result += temp / 2;
}
}
// System.out.println(Arrays.toString(dp));
return result;
}
public void dfs(int ind, ArrayList<Integer>[] tree, boolean[] isPrimes, HashSet<Integer> visited) {
visited.add(ind);
for (int neighbor : tree[ind]) {
if (isPrimes[neighbor] || visited.contains(neighbor)) {
continue;
}
dfs(neighbor, tree, isPrimes, visited);
}
}
private ArrayList<Integer>[] buildTree(int n, int[][] edges) {
@SuppressWarnings("unchecked")
ArrayList<Integer>[] tree = new ArrayList[n + 1];
for (int i = 1; i <= n; i++) {
tree[i] = new ArrayList<>();
}
for (int i = 0; i < edges.length; i++) {
tree[edges[i][0]].add(edges[i][1]);
tree[edges[i][1]].add(edges[i][0]);
}
return tree;
}
private boolean[] loadAllPrimes(int n) {
boolean[] isPrimes = new boolean[n + 1];
Arrays.fill(isPrimes, true);
isPrimes[1] = false;
for (int i = 2; i * i <= n; i++) {
if (isPrimes[i]) {
for (int j = 2; i * j <= n; j++) {
isPrimes[j * i] = false;
}
}
}
return isPrimes;
}
}
/**
* 解法1中存在一个用合数值设置周围合数值的步骤,可以想到用并查集来代替
*/
class Solution2867_2 {
class DisjointSet {
private int count;
private int[] parent;
private int[] rank;
private int[] family;
DisjointSet(int N, boolean[] isPrimes) {
this.count = N;
this.parent = new int[N + 1];
this.rank = new int[N + 1];
this.family = new int[N + 1];
for (int i = 1; i <= N; i++) {
this.parent[i] = i;
this.rank[i] = 1;
this.family[i] = isPrimes[i] ? 0 : 1;
}
}
int count() {
return this.count;
}
int find(int p) {
while (parent[p] != p) {
p = parent[p];
}
return p;
}
boolean connected(int p, int q) {
return find(p) == find(q);
}
int getFamily(int p) {
// NOTE: 这里还可以考虑提前同步root的值到所有成员,这就要考虑到分布情况了
int root = find(p);
return family[root];
}
void union(int p, int q) {
int pRoot = find(p);
int qRoot = find(q);
if (qRoot == pRoot) {
return;
}
if (rank[pRoot] < rank[qRoot]) {
parent[pRoot] = qRoot;
rank[qRoot] = rank[pRoot] + 1 <= rank[qRoot] ? rank[qRoot] : rank[pRoot] + 1;
family[qRoot] += family[pRoot];
} else {
parent[qRoot] = pRoot;
rank[pRoot] = rank[qRoot] + 1 <= rank[pRoot] ? rank[pRoot] : rank[qRoot] + 1;
family[pRoot] += family[qRoot];
}
this.count--;
}
}
public long countPaths(int n, int[][] edges) {
boolean[] isPrimes = loadAllPrimes(n);
ArrayList<Integer>[] tree = buildTree(n, edges);
DisjointSet uf = new DisjointSet(n, isPrimes);
for (int[] edge : edges) {
int u = edge[0];
int v = edge[1];
if (!isPrimes[u] && !isPrimes[v]) {
uf.union(u, v);
}
}
long result = 0;
for (int ind = 1; ind <= n; ind++) {
if (isPrimes[ind]) {
List<Integer> neighborCounts = new ArrayList<>();
for (int neighbor : tree[ind]) {
if (!isPrimes[neighbor]) {
neighborCounts.add(uf.getFamily(neighbor));
}
}
int sum = 0;
for (int i = 0; i < neighborCounts.size(); i++) {
result += neighborCounts.get(i);
sum += neighborCounts.get(i);
}
long temp = 0;
for (int i = 0; i < neighborCounts.size(); i++) {
temp += (long) neighborCounts.get(i) * (sum - neighborCounts.get(i));
}
result += temp / 2;
}
}
return result;
}
private ArrayList<Integer>[] buildTree(int n, int[][] edges) {
@SuppressWarnings("unchecked")
ArrayList<Integer>[] tree = new ArrayList[n + 1];
for (int i = 1; i <= n; i++) {
tree[i] = new ArrayList<>();
}
for (int i = 0; i < edges.length; i++) {
tree[edges[i][0]].add(edges[i][1]);
tree[edges[i][1]].add(edges[i][0]);
}
return tree;
}
private boolean[] loadAllPrimes(int n) {
boolean[] isPrimes = new boolean[n + 1];
Arrays.fill(isPrimes, true);
isPrimes[1] = false;
for (int i = 2; i * i <= n; i++) {
if (isPrimes[i]) {
for (int j = 2; i * j <= n; j++) {
isPrimes[j * i] = false;
}
}
}
return isPrimes;
}
}