Skip to content

Commit 71d7e87

Browse files
committed
modify code
1 parent 1446f6f commit 71d7e87

File tree

2 files changed

+74
-68
lines changed

2 files changed

+74
-68
lines changed
Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package class165;
22

3-
// 选球,java版
3+
// 可撤销并查集模版题,java版
44
// 测试链接 : https://www.luogu.com.cn/problem/AT_abc302_h
55
// 测试链接 : https://atcoder.jp/contests/abc302/tasks/abc302_h
66
// 提交以下的code,提交时请把类名改成"Main",可以通过所有测试用例
@@ -12,7 +12,7 @@
1212
import java.io.PrintWriter;
1313
import java.io.StreamTokenizer;
1414

15-
public class Code03_BallCollector1 {
15+
public class Code02_UndoUnionFind1 {
1616

1717
public static int MAXN = 200001;
1818
public static int[][] arr = new int[MAXN][2];
@@ -23,11 +23,11 @@ public class Code03_BallCollector1 {
2323
public static int cnt;
2424

2525
public static int[] father = new int[MAXN];
26-
public static int[] ncnt = new int[MAXN];
27-
public static int[] ecnt = new int[MAXN];
26+
public static int[] siz = new int[MAXN];
27+
public static int[] edgeCnt = new int[MAXN];
2828

2929
public static int[][] opstack = new int[MAXN][2];
30-
public static int stacksiz = 0;
30+
public static int opsize = 0;
3131

3232
public static int[] ans = new int[MAXN];
3333
public static int ball = 0;
@@ -45,48 +45,51 @@ public static int find(int i) {
4545
return i;
4646
}
4747

48-
public static void merge(int h1, int h2) {
49-
int big, small;
50-
if (ncnt[h1] >= ncnt[h2]) {
51-
big = h1;
52-
small = h2;
48+
public static void merge(int x, int y) {
49+
int fx = find(x), fy = find(y);
50+
if (fx == fy) {
51+
opstack[++opsize][0] = 0;
5352
} else {
54-
big = h2;
55-
small = h1;
53+
if (siz[fx] < siz[fy]) {
54+
int tmp = fx;
55+
fx = fy;
56+
fy = tmp;
57+
}
58+
father[fy] = fx;
59+
siz[fx] += siz[fy];
60+
edgeCnt[fx] += edgeCnt[fy] + 1;
61+
opstack[++opsize][0] = fx;
62+
opstack[opsize][1] = fy;
5663
}
57-
father[small] = big;
58-
ncnt[big] += ncnt[small];
59-
ecnt[big] += ecnt[small] + 1;
60-
stacksiz++;
61-
opstack[stacksiz][0] = big;
62-
opstack[stacksiz][1] = small;
6364
}
6465

6566
public static void undo() {
66-
int big = opstack[stacksiz][0];
67-
int small = opstack[stacksiz][1];
68-
stacksiz--;
69-
father[small] = small;
70-
ncnt[big] -= ncnt[small];
71-
ecnt[big] -= ecnt[small] + 1;
67+
if (opsize > 0 && opstack[opsize][0] != 0) {
68+
int fx = opstack[opsize][0];
69+
int fy = opstack[opsize--][1];
70+
father[fy] = fy;
71+
siz[fx] -= siz[fy];
72+
edgeCnt[fx] -= edgeCnt[fy] + 1;
73+
}
7274
}
7375

7476
public static void dfs(int u, int fa) {
75-
int h1 = find(arr[u][0]), h2 = find(arr[u][1]);
77+
int x = arr[u][0], y = arr[u][1];
78+
int fx = find(x), fy = find(y);
7679
boolean merged = false;
7780
int add = 0;
78-
if (h1 == h2) {
79-
ecnt[h1]++;
80-
if (ncnt[h1] == ecnt[h1]) {
81+
if (fx == fy) {
82+
if (edgeCnt[fx] < siz[fx]) {
8183
ball++;
8284
add = 1;
8385
}
86+
edgeCnt[fx]++;
8487
} else {
85-
if (ecnt[h1] < ncnt[h1] || ecnt[h2] < ncnt[h2]) {
88+
if (edgeCnt[fx] < siz[fx] || edgeCnt[fy] < siz[fy]) {
8689
ball++;
8790
add = 1;
8891
}
89-
merge(h1, h2);
92+
merge(x, y);
9093
merged = true;
9194
}
9295
if (u != 1) {
@@ -97,12 +100,12 @@ public static void dfs(int u, int fa) {
97100
dfs(to[e], u);
98101
}
99102
}
103+
ball -= add;
100104
if (merged) {
101105
undo();
102106
} else {
103-
ecnt[h1]--;
107+
edgeCnt[fx]--;
104108
}
105-
ball -= add;
106109
}
107110

108111
public static void main(String[] args) throws IOException {
@@ -127,8 +130,8 @@ public static void main(String[] args) throws IOException {
127130
}
128131
for (int i = 1; i <= n; i++) {
129132
father[i] = i;
130-
ncnt[i] = 1;
131-
ecnt[i] = 0;
133+
siz[i] = 1;
134+
edgeCnt[i] = 0;
132135
}
133136
dfs(1, 0);
134137
for (int i = 2; i < n; i++) {
Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package class165;
22

3-
// 选球,C++版
3+
// 可撤销并查集模版题,C++版
44
// 测试链接 : https://www.luogu.com.cn/problem/AT_abc302_h
55
// 测试链接 : https://atcoder.jp/contests/abc302/tasks/abc302_h
66
// 如下实现是C++的版本,C++版本和java版本逻辑完全一样
@@ -19,11 +19,11 @@
1919
//int cnt;
2020
//
2121
//int father[MAXN];
22-
//int ncnt[MAXN];
23-
//int ecnt[MAXN];
22+
//int siz[MAXN];
23+
//int edgeCnt[MAXN];
2424
//
2525
//int opstack[MAXN][2];
26-
//int stacksiz = 0;
26+
//int opsize = 0;
2727
//
2828
//int ans[MAXN];
2929
//int ball = 0;
@@ -41,48 +41,51 @@
4141
// return i;
4242
//}
4343
//
44-
//void merge(int h1, int h2) {
45-
// int big, small;
46-
// if (ncnt[h1] >= ncnt[h2]) {
47-
// big = h1;
48-
// small = h2;
44+
//void merge(int x, int y) {
45+
// int fx = find(x), fy = find(y);
46+
// if (fx == fy) {
47+
// opstack[++opsize][0] = 0;
4948
// } else {
50-
// big = h2;
51-
// small = h1;
49+
// if (siz[fx] < siz[fy]) {
50+
// int tmp = fx;
51+
// fx = fy;
52+
// fy = tmp;
53+
// }
54+
// father[fy] = fx;
55+
// siz[fx] += siz[fy];
56+
// edgeCnt[fx] += edgeCnt[fy] + 1;
57+
// opstack[++opsize][0] = fx;
58+
// opstack[opsize][1] = fy;
5259
// }
53-
// father[small] = big;
54-
// ncnt[big] += ncnt[small];
55-
// ecnt[big] += ecnt[small] + 1;
56-
// stacksiz++;
57-
// opstack[stacksiz][0] = big;
58-
// opstack[stacksiz][1] = small;
5960
//}
6061
//
6162
//void undo() {
62-
// int big = opstack[stacksiz][0];
63-
// int small = opstack[stacksiz][1];
64-
// stacksiz--;
65-
// father[small] = small;
66-
// ncnt[big] -= ncnt[small];
67-
// ecnt[big] -= ecnt[small] + 1;
63+
// if (opsize > 0 && opstack[opsize][0] != 0) {
64+
// int fx = opstack[opsize][0];
65+
// int fy = opstack[opsize--][1];
66+
// father[fy] = fy;
67+
// siz[fx] -= siz[fy];
68+
// edgeCnt[fx] -= edgeCnt[fy] + 1;
69+
// }
6870
//}
6971
//
7072
//void dfs(int u, int fa) {
71-
// int h1 = find(arr[u][0]), h2 = find(arr[u][1]);
73+
// int x = arr[u][0], y = arr[u][1];
74+
// int fx = find(x), fy = find(y);
7275
// bool merged = false;
7376
// int add = 0;
74-
// if (h1 == h2) {
75-
// ecnt[h1]++;
76-
// if (ncnt[h1] == ecnt[h1]) {
77+
// if (fx == fy) {
78+
// if (edgeCnt[fx] < siz[fx]) {
7779
// ball++;
7880
// add = 1;
79-
// }
81+
// }
82+
// edgeCnt[fx]++;
8083
// } else {
81-
// if (ecnt[h1] < ncnt[h1] || ecnt[h2] < ncnt[h2]) {
84+
// if (edgeCnt[fx] < siz[fx] || edgeCnt[fy] < siz[fy]) {
8285
// ball++;
8386
// add = 1;
8487
// }
85-
// merge(h1, h2);
88+
// merge(x, y);
8689
// merged = true;
8790
// }
8891
// if (u != 1) {
@@ -93,12 +96,12 @@
9396
// dfs(to[e], u);
9497
// }
9598
// }
99+
// ball -= add;
96100
// if (merged) {
97101
// undo();
98102
// } else {
99-
// ecnt[h1]--;
103+
// edgeCnt[fx]--;
100104
// }
101-
// ball -= add;
102105
//}
103106
//
104107
//int main() {
@@ -116,8 +119,8 @@
116119
// }
117120
// for (int i = 1; i <= n; i++) {
118121
// father[i] = i;
119-
// ncnt[i] = 1;
120-
// ecnt[i] = 0;
122+
// siz[i] = 1;
123+
// edgeCnt[i] = 0;
121124
// }
122125
// dfs(1, 0);
123126
// for (int i = 2; i < n; i++) {

0 commit comments

Comments
 (0)