Skip to content

Commit d989590

Browse files
committed
modify code
1 parent 2f3948e commit d989590

File tree

4 files changed

+404
-3
lines changed

4 files changed

+404
-3
lines changed

src/class166/Code04_GreatIntegration1.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ public static void prepare() {
115115
sorted[i][1] = event[i][1];
116116
sorted[i][2] = event[i][2];
117117
}
118-
Arrays.sort(sorted, 1, q + 1,
119-
(a, b) -> a[1] != b[1] ? a[1] - b[1] : (a[2] != b[2] ? a[2] - b[2] : a[0] - b[0]));
118+
Arrays.sort(sorted, 1, q + 1, (a, b) -> a[1] != b[1] ? a[1] - b[1] : a[2] != b[2] ? a[2] - b[2] : a[0] - b[0]);
120119
for (int l = 1, r = 1; l <= q; l = ++r) {
121120
int t = sorted[l][0];
122121
int x = sorted[l][1];

src/class166/Code05_ConnectedGraph1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public static void prepare() {
126126
father[i] = i;
127127
siz[i] = 1;
128128
}
129-
Arrays.sort(event, 1, ecnt + 1, (a, b) -> a[0] != b[0] ? (a[0] - b[0]) : (a[1] - b[1]));
129+
Arrays.sort(event, 1, ecnt + 1, (a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]);
130130
int eid, t;
131131
for (int l = 1, r = 1; l <= ecnt; l = ++r) {
132132
eid = event[l][0];
Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
package class166;
2+
3+
// 连通性离线处理,java版
4+
// 测试链接 : https://loj.ac/p/121
5+
// 提交以下的code,提交时请把类名改成"Main"
6+
// 测试平台看似支持java语言,其实无法通过,内存过大跳警告,导致验证失败
7+
// 想通过用C++实现,本节课Code06_Connectivity2文件就是C++的实现
8+
// 逻辑完全一样,C++实现可以通过全部测试用例
9+
10+
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.io.OutputStream;
13+
14+
public class Code06_Connectivity1 {
15+
16+
public static int MAXN = 5001;
17+
public static int MAXM = 500001;
18+
public static int MAXT = 5000001;
19+
public static int n, m;
20+
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];
30+
public static int opsize = 0;
31+
32+
public static int[] head = new int[MAXM << 2];
33+
public static int[] next = new int[MAXT];
34+
public static int[] tox = new int[MAXT];
35+
public static int[] toy = new int[MAXT];
36+
public static int cnt = 0;
37+
38+
public static boolean[] ans = new boolean[MAXM];
39+
40+
public static void addEdge(int i, int x, int y) {
41+
next[++cnt] = head[i];
42+
tox[cnt] = x;
43+
toy[cnt] = y;
44+
head[i] = cnt;
45+
}
46+
47+
public static int find(int i) {
48+
while (i != father[i]) {
49+
i = father[i];
50+
}
51+
return i;
52+
}
53+
54+
public static void union(int x, int y) {
55+
int fx = find(x);
56+
int fy = find(y);
57+
if (siz[fx] < siz[fy]) {
58+
int tmp = fx;
59+
fx = fy;
60+
fy = tmp;
61+
}
62+
father[fy] = fx;
63+
siz[fx] += siz[fy];
64+
rollback[++opsize][0] = fx;
65+
rollback[opsize][1] = fy;
66+
}
67+
68+
public static void undo() {
69+
int fx = rollback[opsize][0];
70+
int fy = rollback[opsize--][1];
71+
father[fy] = fy;
72+
siz[fx] -= siz[fy];
73+
}
74+
75+
public static void add(int jobl, int jobr, int jobx, int joby, int l, int r, int i) {
76+
if (jobl <= l && r <= jobr) {
77+
addEdge(i, jobx, joby);
78+
} else {
79+
int mid = (l + r) >> 1;
80+
if (jobl <= mid) {
81+
add(jobl, jobr, jobx, joby, l, mid, i << 1);
82+
}
83+
if (jobr > mid) {
84+
add(jobl, jobr, jobx, joby, mid + 1, r, i << 1 | 1);
85+
}
86+
}
87+
}
88+
89+
public static void dfs(int l, int r, int i) {
90+
int unionCnt = 0;
91+
for (int ei = head[i], x, y, fx, fy; ei > 0; ei = next[ei]) {
92+
x = tox[ei];
93+
y = toy[ei];
94+
fx = find(x);
95+
fy = find(y);
96+
if (fx != fy) {
97+
union(fx, fy);
98+
unionCnt++;
99+
}
100+
}
101+
if (l == r) {
102+
if (op[l] == 2) {
103+
ans[l] = find(a[l]) == find(b[l]);
104+
}
105+
} 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;
128+
}
129+
}
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+
}
136+
}
137+
}
138+
139+
public static void main(String[] args) {
140+
FastIO io = new FastIO(System.in, System.out);
141+
n = io.nextInt();
142+
m = io.nextInt();
143+
for (int i = 1, x, y, t; i <= m; i++) {
144+
t = io.nextInt();
145+
x = io.nextInt();
146+
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+
}
160+
}
161+
}
162+
io.flush();
163+
}
164+
165+
// 读写工具类
166+
static class FastIO {
167+
private final InputStream is;
168+
private final OutputStream os;
169+
private final byte[] inbuf = new byte[1 << 16];
170+
private int lenbuf = 0;
171+
private int ptrbuf = 0;
172+
private final StringBuilder outBuf = new StringBuilder();
173+
174+
public FastIO(InputStream is, OutputStream os) {
175+
this.is = is;
176+
this.os = os;
177+
}
178+
179+
private int readByte() {
180+
if (ptrbuf >= lenbuf) {
181+
ptrbuf = 0;
182+
try {
183+
lenbuf = is.read(inbuf);
184+
} catch (IOException e) {
185+
throw new RuntimeException(e);
186+
}
187+
if (lenbuf == -1) {
188+
return -1;
189+
}
190+
}
191+
return inbuf[ptrbuf++] & 0xff;
192+
}
193+
194+
private int skip() {
195+
int b;
196+
while ((b = readByte()) != -1) {
197+
if (b > ' ') {
198+
return b;
199+
}
200+
}
201+
return -1;
202+
}
203+
204+
public int nextInt() {
205+
int b = skip();
206+
if (b == -1) {
207+
throw new RuntimeException("No more integers (EOF)");
208+
}
209+
boolean negative = false;
210+
if (b == '-') {
211+
negative = true;
212+
b = readByte();
213+
}
214+
int val = 0;
215+
while (b >= '0' && b <= '9') {
216+
val = val * 10 + (b - '0');
217+
b = readByte();
218+
}
219+
return negative ? -val : val;
220+
}
221+
222+
public void write(String s) {
223+
outBuf.append(s);
224+
}
225+
226+
public void writeInt(int x) {
227+
outBuf.append(x);
228+
}
229+
230+
public void writelnInt(int x) {
231+
outBuf.append(x).append('\n');
232+
}
233+
234+
public void flush() {
235+
try {
236+
os.write(outBuf.toString().getBytes());
237+
os.flush();
238+
outBuf.setLength(0);
239+
} catch (IOException e) {
240+
throw new RuntimeException(e);
241+
}
242+
}
243+
}
244+
245+
}

0 commit comments

Comments
 (0)