結果
問題 | No.901 K-ary εxtrεεmε |
ユーザー | 👑 hos.lyric |
提出日時 | 2019-10-04 22:20:05 |
言語 | D (dmd 2.106.1) |
結果 |
AC
|
実行時間 | 470 ms / 3,000 ms |
コード長 | 3,804 bytes |
コンパイル時間 | 1,419 ms |
コンパイル使用メモリ | 135,864 KB |
実行使用メモリ | 49,424 KB |
最終ジャッジ日時 | 2024-06-22 02:40:36 |
合計ジャッジ時間 | 14,369 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 306 ms
46,636 KB |
testcase_01 | AC | 1 ms
6,944 KB |
testcase_02 | AC | 8 ms
6,940 KB |
testcase_03 | AC | 8 ms
6,940 KB |
testcase_04 | AC | 7 ms
6,948 KB |
testcase_05 | AC | 8 ms
6,940 KB |
testcase_06 | AC | 8 ms
6,940 KB |
testcase_07 | AC | 387 ms
36,484 KB |
testcase_08 | AC | 378 ms
36,732 KB |
testcase_09 | AC | 367 ms
36,480 KB |
testcase_10 | AC | 383 ms
37,000 KB |
testcase_11 | AC | 382 ms
36,604 KB |
testcase_12 | AC | 451 ms
37,952 KB |
testcase_13 | AC | 437 ms
37,588 KB |
testcase_14 | AC | 444 ms
36,952 KB |
testcase_15 | AC | 425 ms
36,984 KB |
testcase_16 | AC | 425 ms
37,672 KB |
testcase_17 | AC | 350 ms
37,388 KB |
testcase_18 | AC | 358 ms
37,432 KB |
testcase_19 | AC | 366 ms
37,404 KB |
testcase_20 | AC | 367 ms
37,300 KB |
testcase_21 | AC | 360 ms
37,416 KB |
testcase_22 | AC | 450 ms
40,044 KB |
testcase_23 | AC | 458 ms
41,560 KB |
testcase_24 | AC | 448 ms
41,296 KB |
testcase_25 | AC | 461 ms
40,368 KB |
testcase_26 | AC | 470 ms
41,160 KB |
testcase_27 | AC | 324 ms
39,532 KB |
testcase_28 | AC | 328 ms
38,336 KB |
testcase_29 | AC | 312 ms
49,424 KB |
ソースコード
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, 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)); } enum E = 18; int N; int[] U, V; long[] W; int Q; int[] K; int[][] X; int[][] G; int zeit; int[] dis, fin; int[][] par; long[][] ws; int[] dep; void dfs(int u, int p, long pw) { dis[u] = zeit++; par[0][u] = p; ws[0][u] = pw; dep[u] = (p == -1) ? 0 : (dep[p] + 1); foreach (i; G[u]) { const v = U[i] ^ V[i] ^ u; if (v != p) { dfs(v, u, W[i]); } } fin[u] = zeit++; } Tuple!(int, long) lca(int u, int v) { long ret; foreach_reverse (e; 0 .. E) { if (dep[u] - (1 << e) >= dep[v]) { ret += ws[e][u]; u = par[e][u]; } if (dep[v] - (1 << e) >= dep[u]) { ret += ws[e][v]; v = par[e][v]; } } foreach_reverse (e; 0 .. E) { if (par[e][u] != par[e][v]) { ret += ws[e][u]; ret += ws[e][v]; u = par[e][u]; v = par[e][v]; } } if (u != v) { ret += ws[0][u]; ret += ws[0][v]; u = par[0][u]; v = par[0][v]; } return tuple(u, ret); } void main() { try { for (; ; ) { N = readInt(); U = new int[N - 1]; V = new int[N - 1]; W = new long[N - 1]; foreach (i; 0 .. N - 1) { U[i] = readInt(); V[i] = readInt(); W[i] = readLong(); } Q = readInt(); K = new int[Q]; X = new int[][Q]; foreach (q; 0 .. Q) { K[q] = readInt(); X[q] = new int[K[q]]; foreach (k; 0 .. K[q]) { X[q][k] = readInt(); } } G = new int[][N]; foreach (i; 0 .. N - 1) { G[U[i]] ~= i; G[V[i]] ~= i; } zeit = 0; dis = new int[N]; fin = new int[N]; par = new int[][](E, N); ws = new long[][](E, N); dep = new int[N]; const rt = 0; dfs(rt, -1, 0); par[0][rt] = rt; foreach (e; 0 .. E - 1) { foreach (u; 0 .. N) { par[e + 1][u] = par[e][par[e][u]]; ws[e + 1][u] = ws[e][u] + ws[e][par[e][u]]; } } foreach (q; 0 .. Q) { X[q].sort!((a, b) => (dis[a] < dis[b])); auto us = X[q].dup; foreach (k; 0 .. K[q] - 1) { us ~= lca(X[q][k], X[q][k + 1])[0]; } us.sort!((a, b) => (dis[a] < dis[b])); us = us.uniq.array; debug { writeln("us = ", us); } long ans; DList!int stack; foreach (u; us) { for (; !stack.empty && fin[stack.back] < dis[u]; ) { stack.removeBack; } if (!stack.empty) { ans += lca(stack.back, u)[1]; } stack.insertBack(u); } writeln(ans); } } } catch (EOFException e) { } }