Skip to content

Commit 63c0ef6

Browse files
authored
Merge branch 'algorithmzuo:main' into main
2 parents c39c29e + 78640af commit 63c0ef6

File tree

6 files changed

+265
-38
lines changed

6 files changed

+265
-38
lines changed

src/class119/Code04_EmergencyAssembly1.java renamed to src/class119/Code01_EmergencyAssembly1.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// 1 <= m <= 5 * 10^5
1111
// 测试链接 : https://www.luogu.com.cn/problem/P4281
1212
// 如下实现是正确的,但是洛谷平台对空间卡的很严,只有使用C++能全部通过
13-
// C++版本就是本节代码中的Code04_EmergencyAssembly2文件
13+
// C++版本就是本节代码中的Code01_EmergencyAssembly2文件
1414
// C++版本和java版本逻辑完全一样,但只有C++版本可以通过所有测试用例
1515
// 这是洛谷平台没有照顾各种语言的实现所导致的
1616
// 在真正笔试、比赛时,一定是兼顾各种语言的,该实现是一定正确的
@@ -24,7 +24,7 @@
2424
import java.io.StreamTokenizer;
2525
import java.util.Arrays;
2626

27-
public class Code04_EmergencyAssembly1 {
27+
public class Code01_EmergencyAssembly1 {
2828

2929
public static int MAXN = 500001;
3030

@@ -40,10 +40,10 @@ public class Code04_EmergencyAssembly1 {
4040

4141
public static int cnt;
4242

43-
public static int[][] stjump = new int[MAXN][LIMIT];
44-
4543
public static int[] deep = new int[MAXN];
4644

45+
public static int[][] stjump = new int[MAXN][LIMIT];
46+
4747
public static int togather;
4848

4949
public static long cost;
@@ -138,9 +138,12 @@ public static void main(String[] args) throws IOException {
138138
}
139139

140140
public static void compute(int a, int b, int c) {
141+
// 来自对结构关系的深入分析,课上重点解释
141142
int h1 = lca(a, b), h2 = lca(a, c), h3 = lca(b, c);
142-
togather = h1 == h2 ? h3 : (h1 == h3 ? h2 : h1);
143-
cost = (long) deep[a] + deep[b] + deep[c] - deep[h1] - deep[h2] - deep[h3];
143+
int high = h1 != h2 ? (deep[h1] < deep[h2] ? h1 : h2) : h1;
144+
int low = h1 != h2 ? (deep[h1] > deep[h2] ? h1 : h2) : h3;
145+
togather = low;
146+
cost = (long) deep[a] + deep[b] + deep[c] - deep[high] * 2 - deep[low];
144147
}
145148

146149
}

src/class119/Code04_EmergencyAssembly2.java renamed to src/class119/Code01_EmergencyAssembly2.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
//int edgeNext[MAXN << 1];
2828
//int edgeTo[MAXN << 1];
2929
//int cnt;
30-
//int stjump[MAXN][LIMIT];
3130
//int deep[MAXN];
31+
//int stjump[MAXN][LIMIT];
3232
//int togather;
3333
//long long cost;
3434
//
@@ -83,9 +83,11 @@
8383
//}
8484
//
8585
//void compute(int a, int b, int c) {
86-
// int h1 = lca(a, b), h2 = lca(a, c), h3 = lca(b, c);
87-
// togather = h1 == h2 ? h3 : (h1 == h3 ? h2 : h1);
88-
// cost = deep[a] + deep[b] + deep[c] - deep[h1] - deep[h2] - deep[h3];
86+
// int h1 = lca(a, b), h2 = lca(a, c), h3 = lca(b, c);
87+
// int high = h1 != h2 ? (deep[h1] < deep[h2] ? h1 : h2) : h1;
88+
// int low = h1 != h2 ? (deep[h1] > deep[h2] ? h1 : h2) : h3;
89+
// togather = low;
90+
// cost = (long) deep[a] + deep[b] + deep[c] - deep[high] * 2 - deep[low];
8991
//}
9092
//
9193
//int main() {

src/class119/Code03_Trucking.java renamed to src/class119/Code02_Trucking.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,21 @@
2323
import java.io.StreamTokenizer;
2424
import java.util.Arrays;
2525

26-
public class Code03_Trucking {
26+
public class Code02_Trucking {
2727

2828
public static int MAXN = 10001;
2929

3030
public static int MAXM = 50001;
3131

3232
public static int LIMIT = 21;
3333

34+
public static int power;
35+
3436
public static int[][] edges = new int[MAXM][3];
3537

38+
// 并查集
3639
public static int[] father = new int[MAXN];
3740

38-
public static int power;
39-
4041
public static int cnt;
4142

4243
public static int[] head = new int[MAXN];

src/class119/Code02_QueryPathMinimumChangesToSame.java renamed to src/class119/Code03_QueryPathMinimumChangesToSame.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// 0 <= u、v、a、b < n
1212
// 1 <= w <= 26
1313
// 测试链接 : https://leetcode.cn/problems/minimum-edge-weight-equilibrium-queries-in-a-tree/
14-
public class Code02_QueryPathMinimumChangesToSame {
14+
public class Code03_QueryPathMinimumChangesToSame {
1515

1616
public static int MAXN = 10001;
1717

@@ -39,7 +39,7 @@ public class Code02_QueryPathMinimumChangesToSame {
3939

4040
public static int qcnt;
4141

42-
public static int[][] cnts = new int[MAXN][MAXW + 1];
42+
public static int[][] stcnt = new int[MAXN][MAXW + 1];
4343

4444
public static boolean[] visited = new boolean[MAXN];
4545

@@ -65,7 +65,7 @@ public int[] minOperationsQueries(int n, int[][] edges, int[][] queries) {
6565
for (int j = 1; j <= MAXW; j++) {
6666
// 特别重要的结论
6767
// 很多题可以用到
68-
pathCnt = cnts[queries[i][0]][j] + cnts[queries[i][1]][j] - 2 * cnts[lca[i]][j];
68+
pathCnt = stcnt[queries[i][0]][j] + stcnt[queries[i][1]][j] - 2 * stcnt[lca[i]][j];
6969
maxCnt = Math.max(maxCnt, pathCnt);
7070
allCnt += pathCnt;
7171
}
@@ -108,12 +108,12 @@ public static int find(int i) {
108108
public void tarjan(int u, int w, int f) {
109109
visited[u] = true;
110110
if (u == 0) {
111-
Arrays.fill(cnts[u], 0);
111+
Arrays.fill(stcnt[u], 0);
112112
} else {
113113
for (int i = 1; i <= MAXW; i++) {
114-
cnts[u][i] = cnts[f][i];
114+
stcnt[u][i] = stcnt[f][i];
115115
}
116-
cnts[u][w]++;
116+
stcnt[u][w]++;
117117
}
118118
for (int e = headEdge[u]; e != 0; e = edgeNext[e]) {
119119
if (edgeTo[e] != f) {

src/class119/Code01_PassingBallMaximizeValue.java renamed to src/class119/Code04_PassingBallMaximizeValue.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// f(x) = x + receiver[x] + receiver[receiver[x]] + ...
1515
// 你的任务时选择开始玩家x,目的是最大化f(x),返回函数的最大值
1616
// 测试链接 : https://leetcode.cn/problems/maximize-value-of-function-in-a-ball-passing-game/
17-
public class Code01_PassingBallMaximizeValue {
17+
public class Code04_PassingBallMaximizeValue {
1818

1919
public static int MAXN = 100001;
2020

@@ -28,8 +28,26 @@ public class Code01_PassingBallMaximizeValue {
2828

2929
public static long[][] stsum = new long[MAXN][LIMIT];
3030

31-
// 该方法实现了树上倍增的解法,打败比例很一般,但是非常好想
32-
// 这个题最优解来自基环树分析,后面的课会安排这部分内容
31+
public static void build(long k) {
32+
power = 0;
33+
while ((1L << power) <= (k >> 1)) {
34+
power++;
35+
}
36+
m = 0;
37+
// 数字k在哪些位有1
38+
// 都收集起来
39+
for (int p = power; p >= 0; p--) {
40+
if ((1L << p) <= k) {
41+
kbits[m++] = p;
42+
k -= 1L << p;
43+
}
44+
}
45+
}
46+
47+
// 该方法是树上倍增的解法
48+
// 打败比例很一般但是非常好想
49+
// 这个题最优解来自对基环树的分析
50+
// 后面的课会安排基环树的内容
3351
public static long getMaxFunctionValue(List<Integer> receiver, long k) {
3452
build(k);
3553
int n = receiver.size();
@@ -57,20 +75,4 @@ public static long getMaxFunctionValue(List<Integer> receiver, long k) {
5775
return ans;
5876
}
5977

60-
public static void build(long k) {
61-
power = 0;
62-
while ((1L << power) <= (k >> 1)) {
63-
power++;
64-
}
65-
m = 0;
66-
// 数字k在哪些位有1
67-
// 都收集起来
68-
for (int p = power; p >= 0; p--) {
69-
if ((1L << p) <= k) {
70-
kbits[m++] = p;
71-
k -= 1L << p;
72-
}
73-
}
74-
}
75-
7678
}

0 commit comments

Comments
 (0)