Skip to content

Commit ad7bddc

Browse files
committed
modify code
1 parent d989590 commit ad7bddc

7 files changed

+124
-123
lines changed

src/class166/Code01_SegmentTreeDivideAndConquer1.java

Lines changed: 71 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,42 @@
11
package class166;
22

33
// 线段树分治模版题,java版
4-
// 测试链接 : https://www.luogu.com.cn/problem/P5787
5-
// 提交以下的code,提交时请把类名改成"Main",可以通过所有测试用例
4+
// 测试链接 : https://loj.ac/p/121
5+
// 提交以下的code,提交时请把类名改成"Main"
6+
// 测试平台看似支持java语言,其实无法通过
7+
// 内存过大跳警告,导致验证失败,想通过用C++实现
8+
// 本节课Code01_SegmentTreeDivideAndConquer2文件就是C++的实现
9+
// 逻辑完全一样,C++实现可以通过全部测试用例
610

711
import java.io.IOException;
812
import java.io.InputStream;
913
import java.io.OutputStream;
1014

1115
public class Code01_SegmentTreeDivideAndConquer1 {
1216

13-
public static int MAXN = 100001;
14-
public static int MAXT = 3000001;
15-
public static int n, m, k;
17+
public static int MAXN = 5001;
18+
public static int MAXM = 500001;
19+
public static int MAXT = 5000001;
20+
public static int n, m;
1621

17-
public static int[] father = new int[MAXN << 1];
18-
public static int[] siz = new int[MAXN << 1];
19-
public static int[][] rollback = new int[MAXN << 1][2];
22+
public static int[] op = new int[MAXM];
23+
public static int[] a = new int[MAXM];
24+
public static int[] b = new int[MAXM];
25+
26+
public static int[][] last = new int[MAXN][MAXN];
27+
28+
public static int[] father = new int[MAXN];
29+
public static int[] siz = new int[MAXN];
30+
public static int[][] rollback = new int[MAXN][2];
2031
public static int opsize = 0;
2132

22-
public static int[] head = new int[MAXN << 2];
33+
public static int[] head = new int[MAXM << 2];
2334
public static int[] next = new int[MAXT];
2435
public static int[] tox = new int[MAXT];
2536
public static int[] toy = new int[MAXT];
2637
public static int cnt = 0;
2738

28-
public static boolean[] ans = new boolean[MAXN];
39+
public static boolean[] ans = new boolean[MAXM];
2940

3041
public static void addEdge(int i, int x, int y) {
3142
next[++cnt] = head[i];
@@ -66,7 +77,7 @@ public static void add(int jobl, int jobr, int jobx, int joby, int l, int r, int
6677
if (jobl <= l && r <= jobr) {
6778
addEdge(i, jobx, joby);
6879
} else {
69-
int mid = (l + r) / 2;
80+
int mid = (l + r) >> 1;
7081
if (jobl <= mid) {
7182
add(jobl, jobr, jobx, joby, l, mid, i << 1);
7283
}
@@ -77,62 +88,76 @@ public static void add(int jobl, int jobr, int jobx, int joby, int l, int r, int
7788
}
7889

7990
public static void dfs(int l, int r, int i) {
80-
boolean check = true;
8191
int unionCnt = 0;
8292
for (int ei = head[i], x, y, fx, fy; ei > 0; ei = next[ei]) {
8393
x = tox[ei];
8494
y = toy[ei];
8595
fx = find(x);
8696
fy = find(y);
87-
if (fx == fy) {
88-
check = false;
89-
break;
90-
} else {
91-
union(x, y + n);
92-
union(y, x + n);
93-
unionCnt += 2;
97+
if (fx != fy) {
98+
union(fx, fy);
99+
unionCnt++;
94100
}
95101
}
96-
if (check) {
97-
if (l == r) {
98-
ans[l] = true;
99-
} else {
100-
int mid = (l + r) / 2;
101-
dfs(l, mid, i << 1);
102-
dfs(mid + 1, r, i << 1 | 1);
102+
if (l == r) {
103+
if (op[l] == 2) {
104+
ans[l] = find(a[l]) == find(b[l]);
103105
}
104106
} else {
105-
for (int k = l; k <= r; k++) {
106-
ans[k] = false;
107-
}
107+
int mid = (l + r) / 2;
108+
dfs(l, mid, i << 1);
109+
dfs(mid + 1, r, i << 1 | 1);
108110
}
109-
for (int k = 1; k <= unionCnt; k++) {
111+
for (int j = 1; j <= unionCnt; j++) {
110112
undo();
111113
}
112114
}
113115

116+
public static void prepare() {
117+
for (int i = 1; i <= n; i++) {
118+
father[i] = i;
119+
siz[i] = 1;
120+
}
121+
for (int i = 1, x, y; i <= m; i++) {
122+
x = a[i];
123+
y = b[i];
124+
if (op[i] == 0) {
125+
last[x][y] = i;
126+
} else if (op[i] == 1) {
127+
add(last[x][y], i - 1, x, y, 1, m, 1);
128+
last[x][y] = 0;
129+
}
130+
}
131+
for (int x = 1; x <= n; x++) {
132+
for (int y = x + 1; y <= n; y++) {
133+
if (last[x][y] != 0) {
134+
add(last[x][y], m, x, y, 1, m, 1);
135+
}
136+
}
137+
}
138+
}
139+
114140
public static void main(String[] args) {
115141
FastIO io = new FastIO(System.in, System.out);
116142
n = io.nextInt();
117143
m = io.nextInt();
118-
k = io.nextInt();
119-
for (int i = 1; i <= n * 2; i++) {
120-
father[i] = i;
121-
siz[i] = 1;
122-
}
123-
for (int i = 1, x, y, l, r; i <= m; i++) {
144+
for (int i = 1, x, y, t; i <= m; i++) {
145+
t = io.nextInt();
124146
x = io.nextInt();
125147
y = io.nextInt();
126-
l = io.nextInt();
127-
r = io.nextInt();
128-
add(l + 1, r, x, y, 1, k, 1);
129-
}
130-
dfs(1, k, 1);
131-
for (int i = 1; i <= k; i++) {
132-
if (ans[i]) {
133-
io.write("Yes\n");
134-
} else {
135-
io.write("No\n");
148+
op[i] = t;
149+
a[i] = Math.min(x, y);
150+
b[i] = Math.max(x, y);
151+
}
152+
prepare();
153+
dfs(1, m, 1);
154+
for (int i = 1; i <= m; i++) {
155+
if (op[i] == 2) {
156+
if (ans[i]) {
157+
io.write("Y\n");
158+
} else {
159+
io.write("N\n");
160+
}
136161
}
137162
}
138163
io.flush();

src/class166/Code06_Connectivity2.java renamed to src/class166/Code01_SegmentTreeDivideAndConquer2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package class166;
22

3-
// 连通性离线处理,C++版
3+
// 线段树分治模版题,C++版
44
// 测试链接 : https://loj.ac/p/121
55
// 如下实现是C++的版本,C++版本和java版本逻辑完全一样
66
// 提交如下代码,可以通过所有测试用例

src/class166/Code06_Connectivity1.java renamed to src/class166/Code02_CheckBipartiteGraph1.java

Lines changed: 48 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,31 @@
11
package class166;
22

3-
// 连通性离线处理,java版
4-
// 测试链接 : https://loj.ac/p/121
5-
// 提交以下的code,提交时请把类名改成"Main"
6-
// 测试平台看似支持java语言,其实无法通过,内存过大跳警告,导致验证失败
7-
// 想通过用C++实现,本节课Code06_Connectivity2文件就是C++的实现
8-
// 逻辑完全一样,C++实现可以通过全部测试用例
3+
// 检查二分图,java版
4+
// 测试链接 : https://www.luogu.com.cn/problem/P5787
5+
// 提交以下的code,提交时请把类名改成"Main",可以通过所有测试用例
96

107
import java.io.IOException;
118
import java.io.InputStream;
129
import java.io.OutputStream;
1310

14-
public class Code06_Connectivity1 {
11+
public class Code02_CheckBipartiteGraph1 {
1512

16-
public static int MAXN = 5001;
17-
public static int MAXM = 500001;
18-
public static int MAXT = 5000001;
19-
public static int n, m;
13+
public static int MAXN = 100001;
14+
public static int MAXT = 3000001;
15+
public static int n, m, k;
2016

21-
public static int[] op = new int[MAXM];
22-
public static int[] a = new int[MAXM];
23-
public static int[] b = new int[MAXM];
24-
25-
public static int[][] last = new int[MAXN][MAXN];
26-
27-
public static int[] father = new int[MAXN];
28-
public static int[] siz = new int[MAXN];
29-
public static int[][] rollback = new int[MAXN][2];
17+
public static int[] father = new int[MAXN << 1];
18+
public static int[] siz = new int[MAXN << 1];
19+
public static int[][] rollback = new int[MAXN << 1][2];
3020
public static int opsize = 0;
3121

32-
public static int[] head = new int[MAXM << 2];
22+
public static int[] head = new int[MAXN << 2];
3323
public static int[] next = new int[MAXT];
3424
public static int[] tox = new int[MAXT];
3525
public static int[] toy = new int[MAXT];
3626
public static int cnt = 0;
3727

38-
public static boolean[] ans = new boolean[MAXM];
28+
public static boolean[] ans = new boolean[MAXN];
3929

4030
public static void addEdge(int i, int x, int y) {
4131
next[++cnt] = head[i];
@@ -76,7 +66,7 @@ public static void add(int jobl, int jobr, int jobx, int joby, int l, int r, int
7666
if (jobl <= l && r <= jobr) {
7767
addEdge(i, jobx, joby);
7868
} else {
79-
int mid = (l + r) >> 1;
69+
int mid = (l + r) / 2;
8070
if (jobl <= mid) {
8171
add(jobl, jobr, jobx, joby, l, mid, i << 1);
8272
}
@@ -87,76 +77,62 @@ public static void add(int jobl, int jobr, int jobx, int joby, int l, int r, int
8777
}
8878

8979
public static void dfs(int l, int r, int i) {
80+
boolean check = true;
9081
int unionCnt = 0;
9182
for (int ei = head[i], x, y, fx, fy; ei > 0; ei = next[ei]) {
9283
x = tox[ei];
9384
y = toy[ei];
9485
fx = find(x);
9586
fy = find(y);
96-
if (fx != fy) {
97-
union(fx, fy);
98-
unionCnt++;
87+
if (fx == fy) {
88+
check = false;
89+
break;
90+
} else {
91+
union(x, y + n);
92+
union(y, x + n);
93+
unionCnt += 2;
9994
}
10095
}
101-
if (l == r) {
102-
if (op[l] == 2) {
103-
ans[l] = find(a[l]) == find(b[l]);
96+
if (check) {
97+
if (l == r) {
98+
ans[l] = true;
99+
} else {
100+
int mid = (l + r) / 2;
101+
dfs(l, mid, i << 1);
102+
dfs(mid + 1, r, i << 1 | 1);
104103
}
105104
} else {
106-
int mid = (l + r) / 2;
107-
dfs(l, mid, i << 1);
108-
dfs(mid + 1, r, i << 1 | 1);
109-
}
110-
for (int j = 1; j <= unionCnt; j++) {
111-
undo();
112-
}
113-
}
114-
115-
public static void prepare() {
116-
for (int i = 1; i <= n; i++) {
117-
father[i] = i;
118-
siz[i] = 1;
119-
}
120-
for (int i = 1, x, y; i <= m; i++) {
121-
x = a[i];
122-
y = b[i];
123-
if (op[i] == 0) {
124-
last[x][y] = i;
125-
} else if (op[i] == 1) {
126-
add(last[x][y], i - 1, x, y, 1, m, 1);
127-
last[x][y] = 0;
105+
for (int k = l; k <= r; k++) {
106+
ans[k] = false;
128107
}
129108
}
130-
for (int x = 1; x <= n; x++) {
131-
for (int y = x + 1; y <= n; y++) {
132-
if (last[x][y] != 0) {
133-
add(last[x][y], m, x, y, 1, m, 1);
134-
}
135-
}
109+
for (int k = 1; k <= unionCnt; k++) {
110+
undo();
136111
}
137112
}
138113

139114
public static void main(String[] args) {
140115
FastIO io = new FastIO(System.in, System.out);
141116
n = io.nextInt();
142117
m = io.nextInt();
143-
for (int i = 1, x, y, t; i <= m; i++) {
144-
t = io.nextInt();
118+
k = io.nextInt();
119+
for (int i = 1; i <= n * 2; i++) {
120+
father[i] = i;
121+
siz[i] = 1;
122+
}
123+
for (int i = 1, x, y, l, r; i <= m; i++) {
145124
x = io.nextInt();
146125
y = io.nextInt();
147-
op[i] = t;
148-
a[i] = Math.min(x, y);
149-
b[i] = Math.max(x, y);
150-
}
151-
prepare();
152-
dfs(1, m, 1);
153-
for (int i = 1; i <= m; i++) {
154-
if (op[i] == 2) {
155-
if (ans[i]) {
156-
io.write("Y\n");
157-
} else {
158-
io.write("N\n");
159-
}
126+
l = io.nextInt();
127+
r = io.nextInt();
128+
add(l + 1, r, x, y, 1, k, 1);
129+
}
130+
dfs(1, k, 1);
131+
for (int i = 1; i <= k; i++) {
132+
if (ans[i]) {
133+
io.write("Yes\n");
134+
} else {
135+
io.write("No\n");
160136
}
161137
}
162138
io.flush();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.io.InputStream;
99
import java.io.OutputStream;
1010

11-
public class Code02_MinimumMexTree1 {
11+
public class Code03_MinimumMexTree1 {
1212

1313
public static int MAXN = 1000001;
1414
public static int MAXV = 100001;

src/class166/Code03_UniqueOccurrences1.java renamed to src/class166/Code04_UniqueOccurrences1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.io.InputStream;
1010
import java.io.OutputStream;
1111

12-
public class Code03_UniqueOccurrences1 {
12+
public class Code04_UniqueOccurrences1 {
1313

1414
public static int MAXN = 500001;
1515
public static int MAXT = 10000001;

src/class166/Code04_GreatIntegration1.java renamed to src/class166/Code05_GreatIntegration1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.io.PrintWriter;
1111
import java.util.Arrays;
1212

13-
public class Code04_GreatIntegration1 {
13+
public class Code05_GreatIntegration1 {
1414

1515
public static int MAXN = 100001;
1616
public static int MAXT = 3000001;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.io.OutputStream;
1010
import java.util.Arrays;
1111

12-
public class Code05_ConnectedGraph1 {
12+
public class Code06_ConnectedGraph1 {
1313

1414
public static int MAXN = 100001;
1515
public static int MAXM = 200001;

0 commit comments

Comments
 (0)