結果
問題 | No.399 動的な領主 |
ユーザー | 👑 hos.lyric |
提出日時 | 2019-01-21 09:55:08 |
言語 | D (dmd 2.106.1) |
結果 |
AC
|
実行時間 | 254 ms / 2,000 ms |
コード長 | 3,036 bytes |
コンパイル時間 | 774 ms |
コンパイル使用メモリ | 120,448 KB |
実行使用メモリ | 25,344 KB |
最終ジャッジ日時 | 2024-06-13 03:28:09 |
合計ジャッジ時間 | 5,211 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | AC | 3 ms
5,376 KB |
testcase_05 | AC | 21 ms
5,376 KB |
testcase_06 | AC | 228 ms
16,384 KB |
testcase_07 | AC | 254 ms
16,256 KB |
testcase_08 | AC | 247 ms
16,512 KB |
testcase_09 | AC | 220 ms
16,384 KB |
testcase_10 | AC | 4 ms
5,376 KB |
testcase_11 | AC | 20 ms
5,376 KB |
testcase_12 | AC | 199 ms
17,152 KB |
testcase_13 | AC | 191 ms
17,152 KB |
testcase_14 | AC | 161 ms
25,344 KB |
testcase_15 | AC | 169 ms
25,344 KB |
testcase_16 | AC | 180 ms
20,608 KB |
testcase_17 | AC | 213 ms
16,384 KB |
testcase_18 | AC | 209 ms
16,384 KB |
ソースコード
import std.conv, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, 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; } void chmin(T)(ref T t, in T f) { if (t > f) t = f; } void chmax(T)(ref T t, in T f) { if (t < f) t = f; } int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; } int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); } int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); } enum LIM_LOGN = 17; int N; int[] U, V; int Q; int[] A, B; int[][] G; int[][] par; int[] dep; void dfs(int u, int p, int d) { par[0][u] = p; dep[u] = d; foreach (v; G[u]) { if (v != p) { dfs(v, u, d + 1); } } } int getLCA(int u, int v) { foreach_reverse (e; 0 .. LIM_LOGN) { 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 .. LIM_LOGN) { 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; } long[] sum, sumL; void dfs1(int u) { foreach (v; G[u]) { if (v != par[0][u]) { dfs1(v); sum[u] += sum[v]; } } } void main() { try { for (; ; ) { N = readInt(); U = new int[N - 1]; V = new int[N - 1]; foreach (i; 0 .. N - 1) { U[i] = readInt() - 1; V[i] = readInt() - 1; } Q = readInt(); 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 .. N - 1) { G[U[i]] ~= V[i]; G[V[i]] ~= U[i]; } par = new int[][](LIM_LOGN, N); dep = new int[N]; const rt = 0; dfs(rt, rt, 0); foreach (e; 1 .. LIM_LOGN) { foreach (u; 0 .. N) { par[e][u] = par[e - 1][par[e - 1][u]]; } } sum = new long[N]; sumL = new long[N]; foreach (q; 0 .. Q) { const l = getLCA(A[q], B[q]); sum[A[q]] += 1; sum[B[q]] += 1; sum[l] -= 2; sumL[l] += 1; } dfs1(rt); long ans; foreach (u; 0 .. N) { const c = sum[u] + sumL[u]; ans += c * (c + 1) / 2; debug { writeln(u, " ", c); } } writeln(ans); } } catch (EOFException e) { } }