結果
問題 | No.386 貪欲な領主 |
ユーザー | 👑 hos.lyric |
提出日時 | 2019-01-21 09:43:16 |
言語 | D (dmd 2.106.1) |
結果 |
AC
|
実行時間 | 429 ms / 2,000 ms |
コード長 | 3,363 bytes |
コンパイル時間 | 1,013 ms |
コンパイル使用メモリ | 120,320 KB |
実行使用メモリ | 50,048 KB |
最終ジャッジ日時 | 2024-06-13 03:27:57 |
合計ジャッジ時間 | 4,861 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
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 | 2 ms
5,376 KB |
testcase_04 | AC | 429 ms
50,048 KB |
testcase_05 | AC | 314 ms
30,720 KB |
testcase_06 | AC | 324 ms
33,136 KB |
testcase_07 | AC | 4 ms
5,376 KB |
testcase_08 | AC | 43 ms
6,272 KB |
testcase_09 | AC | 6 ms
5,376 KB |
testcase_10 | AC | 1 ms
5,376 KB |
testcase_11 | AC | 1 ms
5,376 KB |
testcase_12 | AC | 3 ms
5,376 KB |
testcase_13 | AC | 11 ms
5,376 KB |
testcase_14 | AC | 313 ms
30,592 KB |
testcase_15 | AC | 356 ms
39,552 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[] A, B; long[] U; int M; int[] S, T; long[] C; 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); } } } void main() { try { for (; ; ) { N = readInt(); A = new int[N - 1]; B = new int[N - 1]; foreach (i; 0 .. N - 1) { A[i] = readInt(); B[i] = readInt(); } U = new long[N]; foreach (u; 0 .. N) { U[u] = readLong(); } M = readInt(); S = new int[M]; T = new int[M]; C = new long[M]; foreach (m; 0 .. M) { S[m] = readInt(); T[m] = readInt(); C[m] = readLong(); } G = new int[][N]; foreach (i; 0 .. N - 1) { G[A[i]] ~= B[i]; G[B[i]] ~= A[i]; } par = new int[][](LIM_LOGN, N); dep = new int[N]; const rt = 0; dfs(rt, rt, 0); auto parCost = new long[][](LIM_LOGN, N); foreach (u; 0 .. N) { parCost[0][u] = (u == rt) ? 0 : U[u]; } foreach (e; 1 .. LIM_LOGN) { foreach (u; 0 .. N) { par[e][u] = par[e - 1][par[e - 1][u]]; parCost[e][u] = parCost[e - 1][par[e - 1][u]] + parCost[e - 1][u]; } } long ans; foreach (m; 0 .. M) { long cost; int u = S[m], v = T[m]; foreach_reverse (e; 0 .. LIM_LOGN) { if (dep[u] - (1 << e) >= dep[v]) { cost += parCost[e][u]; u = par[e][u]; } if (dep[v] - (1 << e) >= dep[u]) { cost += parCost[e][v]; v = par[e][v]; } } foreach_reverse (e; 0 .. LIM_LOGN) { if (par[e][u] != par[e][v]) { cost += parCost[e][u]; cost += parCost[e][v]; u = par[e][u]; v = par[e][v]; } } if (u != v) { cost += parCost[0][u]; cost += parCost[0][v]; u = par[0][u]; v = par[0][v]; } assert(u == v); cost += U[u]; ans += C[m] * cost; debug { writefln("%s %s: %s", S[m], T[m], cost); } } writeln(ans); } } catch (EOFException e) { } }