|
| 1 | +package class166; |
| 2 | + |
| 3 | +// 连通图 |
| 4 | +// 测试链接 : https://www.luogu.com.cn/problem/P5227 |
| 5 | +// 提交以下的code,提交时请把类名改成"Main",可以通过所有测试用例 |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStream; |
| 9 | +import java.io.OutputStream; |
| 10 | +import java.util.Arrays; |
| 11 | + |
| 12 | +public class Code05_ConnectedGraph1 { |
| 13 | + |
| 14 | + public static int MAXN = 100001; |
| 15 | + public static int MAXM = 200001; |
| 16 | + public static int MAXE = 400001; |
| 17 | + public static int MAXT = 10000001; |
| 18 | + public static int n, m, k; |
| 19 | + |
| 20 | + public static int[] x = new int[MAXM]; |
| 21 | + public static int[] y = new int[MAXM]; |
| 22 | + |
| 23 | + public static int[][] event = new int[MAXE][2]; |
| 24 | + public static int ecnt = 0; |
| 25 | + public static boolean[] visit = new boolean[MAXM]; |
| 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[MAXN << 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[MAXN]; |
| 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 | + boolean check = false; |
| 91 | + int unionCnt = 0; |
| 92 | + for (int ei = head[i], x, y, fx, fy; ei > 0; ei = next[ei]) { |
| 93 | + x = tox[ei]; |
| 94 | + y = toy[ei]; |
| 95 | + fx = find(x); |
| 96 | + fy = find(y); |
| 97 | + if (fx != fy) { |
| 98 | + union(fx, fy); |
| 99 | + unionCnt++; |
| 100 | + } |
| 101 | + if (siz[find(fx)] == n) { |
| 102 | + check = true; |
| 103 | + break; |
| 104 | + } |
| 105 | + } |
| 106 | + if (check) { |
| 107 | + for (int j = l; j <= r; j++) { |
| 108 | + ans[j] = true; |
| 109 | + } |
| 110 | + } else { |
| 111 | + if (l == r) { |
| 112 | + ans[l] = false; |
| 113 | + } else { |
| 114 | + int mid = (l + r) >> 1; |
| 115 | + dfs(l, mid, i << 1); |
| 116 | + dfs(mid + 1, r, i << 1 | 1); |
| 117 | + } |
| 118 | + } |
| 119 | + for (int j = 1; j <= unionCnt; j++) { |
| 120 | + undo(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + public static void prepare() { |
| 125 | + for (int i = 1; i <= n; i++) { |
| 126 | + father[i] = i; |
| 127 | + siz[i] = 1; |
| 128 | + } |
| 129 | + Arrays.sort(event, 1, ecnt + 1, (a, b) -> a[0] != b[0] ? (a[0] - b[0]) : (a[1] - b[1])); |
| 130 | + int eid, t; |
| 131 | + for (int l = 1, r = 1; l <= ecnt; l = ++r) { |
| 132 | + eid = event[l][0]; |
| 133 | + visit[eid] = true; |
| 134 | + while (r + 1 <= ecnt && event[r + 1][0] == eid) { |
| 135 | + r++; |
| 136 | + } |
| 137 | + t = 1; |
| 138 | + for (int i = l; i <= r; i++) { |
| 139 | + if (t <= event[i][1] - 1) { |
| 140 | + add(t, event[i][1] - 1, x[eid], y[eid], 1, k, 1); |
| 141 | + } |
| 142 | + t = event[i][1] + 1; |
| 143 | + } |
| 144 | + if (t <= k) { |
| 145 | + add(t, k, x[eid], y[eid], 1, k, 1); |
| 146 | + } |
| 147 | + } |
| 148 | + for (int i = 1; i <= m; i++) { |
| 149 | + if (!visit[i]) { |
| 150 | + add(1, k, x[i], y[i], 1, k, 1); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + public static void main(String[] args) { |
| 156 | + FastIO io = new FastIO(System.in, System.out); |
| 157 | + n = io.nextInt(); |
| 158 | + m = io.nextInt(); |
| 159 | + for (int i = 1; i <= m; i++) { |
| 160 | + x[i] = io.nextInt(); |
| 161 | + y[i] = io.nextInt(); |
| 162 | + } |
| 163 | + k = io.nextInt(); |
| 164 | + for (int i = 1, s; i <= k; i++) { |
| 165 | + s = io.nextInt(); |
| 166 | + for (int j = 1; j <= s; j++) { |
| 167 | + event[++ecnt][0] = io.nextInt(); |
| 168 | + event[ecnt][1] = i; |
| 169 | + } |
| 170 | + } |
| 171 | + prepare(); |
| 172 | + dfs(1, k, 1); |
| 173 | + for (int i = 1; i <= k; i++) { |
| 174 | + if (ans[i]) { |
| 175 | + io.write("Connected\n"); |
| 176 | + } else { |
| 177 | + io.write("Disconnected\n"); |
| 178 | + } |
| 179 | + } |
| 180 | + io.flush(); |
| 181 | + } |
| 182 | + |
| 183 | + // 读写工具类 |
| 184 | + static class FastIO { |
| 185 | + private final InputStream is; |
| 186 | + private final OutputStream os; |
| 187 | + private final byte[] inbuf = new byte[1 << 16]; |
| 188 | + private int lenbuf = 0; |
| 189 | + private int ptrbuf = 0; |
| 190 | + private final StringBuilder outBuf = new StringBuilder(); |
| 191 | + |
| 192 | + public FastIO(InputStream is, OutputStream os) { |
| 193 | + this.is = is; |
| 194 | + this.os = os; |
| 195 | + } |
| 196 | + |
| 197 | + private int readByte() { |
| 198 | + if (ptrbuf >= lenbuf) { |
| 199 | + ptrbuf = 0; |
| 200 | + try { |
| 201 | + lenbuf = is.read(inbuf); |
| 202 | + } catch (IOException e) { |
| 203 | + throw new RuntimeException(e); |
| 204 | + } |
| 205 | + if (lenbuf == -1) { |
| 206 | + return -1; |
| 207 | + } |
| 208 | + } |
| 209 | + return inbuf[ptrbuf++] & 0xff; |
| 210 | + } |
| 211 | + |
| 212 | + private int skip() { |
| 213 | + int b; |
| 214 | + while ((b = readByte()) != -1) { |
| 215 | + if (b > ' ') { |
| 216 | + return b; |
| 217 | + } |
| 218 | + } |
| 219 | + return -1; |
| 220 | + } |
| 221 | + |
| 222 | + public int nextInt() { |
| 223 | + int b = skip(); |
| 224 | + if (b == -1) { |
| 225 | + throw new RuntimeException("No more integers (EOF)"); |
| 226 | + } |
| 227 | + boolean negative = false; |
| 228 | + if (b == '-') { |
| 229 | + negative = true; |
| 230 | + b = readByte(); |
| 231 | + } |
| 232 | + int val = 0; |
| 233 | + while (b >= '0' && b <= '9') { |
| 234 | + val = val * 10 + (b - '0'); |
| 235 | + b = readByte(); |
| 236 | + } |
| 237 | + return negative ? -val : val; |
| 238 | + } |
| 239 | + |
| 240 | + public void write(String s) { |
| 241 | + outBuf.append(s); |
| 242 | + } |
| 243 | + |
| 244 | + public void writeInt(int x) { |
| 245 | + outBuf.append(x); |
| 246 | + } |
| 247 | + |
| 248 | + public void writelnInt(int x) { |
| 249 | + outBuf.append(x).append('\n'); |
| 250 | + } |
| 251 | + |
| 252 | + public void flush() { |
| 253 | + try { |
| 254 | + os.write(outBuf.toString().getBytes()); |
| 255 | + os.flush(); |
| 256 | + outBuf.setLength(0); |
| 257 | + } catch (IOException e) { |
| 258 | + throw new RuntimeException(e); |
| 259 | + } |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | +} |
0 commit comments