Skip to content

Commit 7c9e88d

Browse files
committed
modify code
1 parent 9a74687 commit 7c9e88d

File tree

2 files changed

+273
-0
lines changed

2 files changed

+273
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package class165;
2+
3+
// 可持久化并查集,java版
4+
// 测试链接 : https://www.luogu.com.cn/problem/P3402
5+
// 提交以下的code,提交时请把类名改成"Main",可以通过所有测试用例
6+
7+
import java.io.BufferedReader;
8+
import java.io.IOException;
9+
import java.io.InputStreamReader;
10+
import java.io.OutputStreamWriter;
11+
import java.io.PrintWriter;
12+
import java.io.StreamTokenizer;
13+
14+
public class Code01_PersistentUnionFind1 {
15+
16+
public static int MAXM = 200001;
17+
public static int MAXT = 8000001;
18+
public static int n, m;
19+
20+
public static int[] rootfa = new int[MAXM];
21+
public static int[] rootsiz = new int[MAXM];
22+
public static int[] ls = new int[MAXT];
23+
public static int[] rs = new int[MAXT];
24+
public static int[] val = new int[MAXT];
25+
public static int cnt = 0;
26+
27+
public static int buildfa(int l, int r) {
28+
int rt = ++cnt;
29+
if (l == r) {
30+
val[rt] = l;
31+
} else {
32+
int mid = (l + r) / 2;
33+
ls[rt] = buildfa(l, mid);
34+
rs[rt] = buildfa(mid + 1, r);
35+
}
36+
return rt;
37+
}
38+
39+
public static int buildsiz(int l, int r) {
40+
int rt = ++cnt;
41+
if (l == r) {
42+
val[rt] = 1;
43+
} else {
44+
int mid = (l + r) / 2;
45+
ls[rt] = buildsiz(l, mid);
46+
rs[rt] = buildsiz(mid + 1, r);
47+
}
48+
return rt;
49+
}
50+
51+
public static int update(int jobi, int jobv, int l, int r, int i) {
52+
int rt = ++cnt;
53+
ls[rt] = ls[i];
54+
rs[rt] = rs[i];
55+
if (l == r) {
56+
val[rt] = jobv;
57+
} else {
58+
int mid = (l + r) / 2;
59+
if (jobi <= mid) {
60+
ls[rt] = update(jobi, jobv, l, mid, ls[rt]);
61+
} else {
62+
rs[rt] = update(jobi, jobv, mid + 1, r, rs[rt]);
63+
}
64+
}
65+
return rt;
66+
}
67+
68+
public static int query(int jobi, int l, int r, int i) {
69+
if (l == r) {
70+
return val[i];
71+
}
72+
int mid = (l + r) / 2;
73+
if (jobi <= mid) {
74+
return query(jobi, l, mid, ls[i]);
75+
} else {
76+
return query(jobi, mid + 1, r, rs[i]);
77+
}
78+
}
79+
80+
public static int find(int x, int version) {
81+
int fa = query(x, 1, n, rootfa[version]);
82+
if (x == fa) {
83+
return x;
84+
}
85+
return find(fa, version);
86+
}
87+
88+
public static void merge(int x, int y, int version) {
89+
int fx = find(x, version);
90+
int fy = find(y, version);
91+
if (fx != fy) {
92+
int xsiz = query(fx, 1, n, rootsiz[version]);
93+
int ysiz = query(fy, 1, n, rootsiz[version]);
94+
if (xsiz >= ysiz) {
95+
rootfa[version] = update(fy, fx, 1, n, rootfa[version]);
96+
rootsiz[version] = update(fx, xsiz + ysiz, 1, n, rootsiz[version]);
97+
} else {
98+
rootfa[version] = update(fx, fy, 1, n, rootfa[version]);
99+
rootsiz[version] = update(fy, xsiz + ysiz, 1, n, rootsiz[version]);
100+
}
101+
}
102+
}
103+
104+
public static void main(String[] args) throws IOException {
105+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
106+
StreamTokenizer in = new StreamTokenizer(br);
107+
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
108+
in.nextToken();
109+
n = (int) in.nval;
110+
in.nextToken();
111+
m = (int) in.nval;
112+
rootfa[0] = buildfa(1, n);
113+
rootsiz[0] = buildsiz(1, n);
114+
for (int version = 1, op, x, y; version <= m; version++) {
115+
in.nextToken();
116+
op = (int) in.nval;
117+
rootfa[version] = rootfa[version - 1];
118+
rootsiz[version] = rootsiz[version - 1];
119+
if (op == 1) {
120+
in.nextToken();
121+
x = (int) in.nval;
122+
in.nextToken();
123+
y = (int) in.nval;
124+
merge(x, y, version);
125+
} else if (op == 2) {
126+
in.nextToken();
127+
x = (int) in.nval;
128+
rootfa[version] = rootfa[x];
129+
rootsiz[version] = rootsiz[x];
130+
} else {
131+
in.nextToken();
132+
x = (int) in.nval;
133+
in.nextToken();
134+
y = (int) in.nval;
135+
if (find(x, version) == find(y, version)) {
136+
out.println(1);
137+
} else {
138+
out.println(0);
139+
}
140+
}
141+
}
142+
out.flush();
143+
out.close();
144+
br.close();
145+
}
146+
147+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package class165;
2+
3+
// 可持久化并查集,C++版
4+
// 测试链接 : https://www.luogu.com.cn/problem/P3402
5+
// 如下实现是C++的版本,C++版本和java版本逻辑完全一样
6+
// 提交如下代码,可以通过所有测试用例
7+
8+
//#include <iostream>
9+
//
10+
//using namespace std;
11+
//
12+
//const int MAXM = 200001;
13+
//const int MAXT = 8000001;
14+
//int n, m;
15+
//int rootfa[MAXM];
16+
//int rootsiz[MAXM];
17+
//int ls[MAXT];
18+
//int rs[MAXT];
19+
//int val[MAXT];
20+
//int cnt = 0;
21+
//
22+
//int buildfa(int l, int r) {
23+
// int rt = ++cnt;
24+
// if (l == r) {
25+
// val[rt] = l;
26+
// } else {
27+
// int mid = (l + r) / 2;
28+
// ls[rt] = buildfa(l, mid);
29+
// rs[rt] = buildfa(mid + 1, r);
30+
// }
31+
// return rt;
32+
//}
33+
//
34+
//int buildsiz(int l, int r) {
35+
// int rt = ++cnt;
36+
// if (l == r) {
37+
// val[rt] = 1;
38+
// } else {
39+
// int mid = (l + r) / 2;
40+
// ls[rt] = buildsiz(l, mid);
41+
// rs[rt] = buildsiz(mid + 1, r);
42+
// }
43+
// return rt;
44+
//}
45+
//
46+
//int update(int jobi, int jobv, int l, int r, int i) {
47+
// int rt = ++cnt;
48+
// ls[rt] = ls[i];
49+
// rs[rt] = rs[i];
50+
// if (l == r) {
51+
// val[rt] = jobv;
52+
// } else {
53+
// int mid = (l + r) / 2;
54+
// if (jobi <= mid) {
55+
// ls[rt] = update(jobi, jobv, l, mid, ls[rt]);
56+
// } else {
57+
// rs[rt] = update(jobi, jobv, mid + 1, r, rs[rt]);
58+
// }
59+
// }
60+
// return rt;
61+
//}
62+
//
63+
//int query(int jobi, int l, int r, int i) {
64+
// if (l == r) {
65+
// return val[i];
66+
// }
67+
// int mid = (l + r) / 2;
68+
// if (jobi <= mid) {
69+
// return query(jobi, l, mid, ls[i]);
70+
// } else {
71+
// return query(jobi, mid + 1, r, rs[i]);
72+
// }
73+
//}
74+
//
75+
//int find(int x, int version) {
76+
// int fa = query(x, 1, n, rootfa[version]);
77+
// if (x == fa) {
78+
// return x;
79+
// }
80+
// return find(fa, version);
81+
//}
82+
//
83+
//void merge(int x, int y, int version) {
84+
// int fx = find(x, version);
85+
// int fy = find(y, version);
86+
// if (fx != fy) {
87+
// int xsiz = query(fx, 1, n, rootsiz[version]);
88+
// int ysiz = query(fy, 1, n, rootsiz[version]);
89+
// if (xsiz >= ysiz) {
90+
// rootfa[version] = update(fy, fx, 1, n, rootfa[version]);
91+
// rootsiz[version] = update(fx, xsiz + ysiz, 1, n, rootsiz[version]);
92+
// } else {
93+
// rootfa[version] = update(fx, fy, 1, n, rootfa[version]);
94+
// rootsiz[version] = update(fy, xsiz + ysiz, 1, n, rootsiz[version]);
95+
// }
96+
// }
97+
//}
98+
//
99+
//int main() {
100+
// ios::sync_with_stdio(false);
101+
// cin.tie(nullptr);
102+
// cin >> n >> m;
103+
// rootfa[0] = buildfa(1, n);
104+
// rootsiz[0] = buildsiz(1, n);
105+
// for (int version = 1, op, x, y; version <= m; version++) {
106+
// cin >> op;
107+
// rootfa[version] = rootfa[version - 1];
108+
// rootsiz[version] = rootsiz[version - 1];
109+
// if (op == 1) {
110+
// cin >> x >> y;
111+
// merge(x, y, version);
112+
// } else if (op == 2) {
113+
// cin >> x;
114+
// rootfa[version] = rootfa[x];
115+
// rootsiz[version] = rootsiz[x];
116+
// } else {
117+
// cin >> x >> y;
118+
// if (find(x, version) == find(y, version)) {
119+
// cout << 1 << "\n";
120+
// } else {
121+
// cout << 0 << "\n";
122+
// }
123+
// }
124+
// }
125+
// return 0;
126+
//}

0 commit comments

Comments
 (0)