結果
問題 | No.922 東北きりきざむたん |
ユーザー | 👑 hos.lyric |
提出日時 | 2019-11-08 21:40:00 |
言語 | D (dmd 2.106.1) |
結果 |
AC
|
実行時間 | 262 ms / 2,000 ms |
コード長 | 3,870 bytes |
コンパイル時間 | 896 ms |
コンパイル使用メモリ | 122,192 KB |
実行使用メモリ | 26,260 KB |
最終ジャッジ日時 | 2024-06-22 03:00:03 |
合計ジャッジ時間 | 6,185 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,944 KB |
testcase_02 | AC | 1 ms
6,944 KB |
testcase_03 | AC | 1 ms
6,940 KB |
testcase_04 | AC | 1 ms
6,940 KB |
testcase_05 | AC | 2 ms
6,944 KB |
testcase_06 | AC | 2 ms
6,944 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | AC | 2 ms
6,944 KB |
testcase_09 | AC | 68 ms
12,272 KB |
testcase_10 | AC | 58 ms
6,944 KB |
testcase_11 | AC | 65 ms
9,084 KB |
testcase_12 | AC | 23 ms
12,732 KB |
testcase_13 | AC | 21 ms
6,940 KB |
testcase_14 | AC | 97 ms
15,028 KB |
testcase_15 | AC | 13 ms
12,424 KB |
testcase_16 | AC | 186 ms
17,408 KB |
testcase_17 | AC | 189 ms
17,756 KB |
testcase_18 | AC | 191 ms
17,380 KB |
testcase_19 | AC | 188 ms
17,408 KB |
testcase_20 | AC | 193 ms
18,692 KB |
testcase_21 | AC | 199 ms
17,568 KB |
testcase_22 | AC | 201 ms
17,400 KB |
testcase_23 | AC | 262 ms
17,512 KB |
testcase_24 | AC | 249 ms
17,292 KB |
testcase_25 | AC | 217 ms
19,148 KB |
testcase_26 | AC | 206 ms
18,588 KB |
testcase_27 | AC | 220 ms
20,176 KB |
testcase_28 | AC | 74 ms
15,252 KB |
testcase_29 | AC | 198 ms
26,260 KB |
ソースコード
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } int root(int[] uf, int u) { return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u])); } bool connect(int[] uf, int u, int v) { u = uf.root(u); v = uf.root(v); if (u == v) return false; if (uf[u] > uf[v]) swap(u, v); uf[u] += uf[v]; uf[v] = u; return true; } enum E = 18; int N, M, Q; int[] U, V; int[] A, B; int[][] G; long[] freq, freqSum; int[][] par; int[] dep; void dfs(int u, int p) { par[0][u] = p; dep[u] = (p == -1) ? 0 : (dep[p] + 1); freqSum[u] = freq[u]; foreach (v; G[u]) { if (v != p) { dfs(v, u); freqSum[u] += freqSum[v]; } } } int lca(int u, int v) { foreach_reverse (e; 0 .. E) { if (dep[u] - (1 << e) >= dep[v]) { u = par[e][u]; } if (dep[v] - (1 << e) >= dep[u]) { v = par[e][v]; } } foreach_reverse (e; 0 .. E) { if (par[e][u] != par[e][v]) { u = par[e][u]; v = par[e][v]; } } if (u != v) { u = par[0][u]; v = par[0][v]; } return u; } void main() { try { for (; ; ) { N = readInt(); M = readInt(); Q = readInt(); U = new int[M]; V = new int[M]; foreach (i; 0 .. M) { U[i] = readInt() - 1; V[i] = readInt() - 1; } A = new int[Q]; B = new int[Q]; foreach (q; 0 .. Q) { A[q] = readInt() - 1; B[q] = readInt() - 1; } G = new int[][N]; foreach (i; 0 .. M) { G[U[i]] ~= V[i]; G[V[i]] ~= U[i]; } auto uf = new int[N]; uf[] = -1; foreach (i; 0 .. M) { uf.connect(U[i], V[i]); } freq = new long[N]; foreach (q; 0 .. Q) { if (uf.root(A[q]) != uf.root(B[q])) { ++freq[A[q]]; ++freq[B[q]]; } } freqSum = new long[N]; dep = new int[N]; par = new int[][](E, N); foreach (u; 0 .. N) { if (uf[u] < 0) { debug { writeln("root ", u); } dfs(u, u); } } foreach (e; 0 .. E - 1) { foreach (u; 0 .. N) { par[e + 1][u] = par[e][par[e][u]]; } } debug { writeln("freqSum = ", freqSum); } long ans; foreach (q; 0 .. Q) { if (uf.root(A[q]) == uf.root(B[q])) { const l = lca(A[q], B[q]); const cost = dep[A[q]] + dep[B[q]] - 2 * dep[l]; debug { writeln("same ", A[q], " ", B[q], ": ", cost); } ans += cost; } } foreach (u; 0 .. N) { if (par[0][u] != u) { ans += min(freqSum[u], freqSum[uf.root(u)] - freqSum[u]); } } writeln(ans); } } catch (EOFException e) { } }