|
| 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 | +} |
0 commit comments