結果
問題 | No.1212 Second Path |
ユーザー | 👑 hos.lyric |
提出日時 | 2020-08-30 17:00:19 |
言語 | D (dmd 2.106.1) |
結果 |
WA
|
実行時間 | - |
コード長 | 6,064 bytes |
コンパイル時間 | 888 ms |
コンパイル使用メモリ | 123,044 KB |
実行使用メモリ | 82,792 KB |
最終ジャッジ日時 | 2024-06-22 08:47:35 |
合計ジャッジ時間 | 18,946 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 299 ms
81,996 KB |
testcase_01 | AC | 492 ms
81,568 KB |
testcase_02 | AC | 506 ms
80,260 KB |
testcase_03 | AC | 505 ms
80,024 KB |
testcase_04 | AC | 507 ms
77,224 KB |
testcase_05 | AC | 509 ms
77,148 KB |
testcase_06 | AC | 254 ms
71,684 KB |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | AC | 306 ms
81,608 KB |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | AC | 73 ms
6,940 KB |
testcase_24 | AC | 79 ms
6,944 KB |
testcase_25 | AC | 79 ms
6,944 KB |
testcase_26 | AC | 81 ms
6,944 KB |
testcase_27 | AC | 78 ms
6,940 KB |
testcase_28 | AC | 79 ms
6,940 KB |
testcase_29 | AC | 423 ms
82,792 KB |
testcase_30 | AC | 424 ms
82,112 KB |
testcase_31 | AC | 418 ms
81,632 KB |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | WA | - |
testcase_39 | WA | - |
testcase_40 | WA | - |
testcase_41 | WA | - |
testcase_42 | AC | 1 ms
5,376 KB |
testcase_43 | AC | 1 ms
5,376 KB |
testcase_44 | AC | 1 ms
5,376 KB |
testcase_45 | AC | 272 ms
72,128 KB |
testcase_46 | AC | 257 ms
71,736 KB |
testcase_47 | AC | 252 ms
71,696 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.mathspecial, 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 N; int[] A, B; long[] C; int[][] G; int[] par, dep; long[] dist; void dfs(int u, int p, int ip) { par[u] = p; dep[u] = (p == -1) ? 0 : (dep[p] + 1); dist[u] = (p == -1) ? 0 : (dist[p] + C[ip]); foreach (i; G[u]) { const v = A[i] ^ B[i] ^ u; if (v != p) { dfs(v, u, i); } } } enum E = 17; enum INF = 10L^^18; struct Info { int last; long mn; long[2] xs; }; Info mul(in Info f, in Info g) { Info h; h.last = g.last; h.mn = min(f.mn, (g.xs[0] != dist[f.last] - dist[par[f.last]]) ? g.xs[0] : g.xs[1], g.mn); h.xs = f.xs; return h; } void main() { try { for (; ; ) { N = readInt(); A = new int[N - 1]; B = new int[N - 1]; C = new long[N - 1]; foreach (i; 0 .. N - 1) { A[i] = readInt() - 1; B[i] = readInt() - 1; C[i] = readLong(); } const Q = readInt(); auto X = new int[Q]; auto Y = new int[Q]; foreach (q; 0 .. Q) { X[q] = readInt() - 1; Y[q] = readInt() - 1; } G = new int[][N]; foreach (i; 0 .. N - 1) { G[A[i]] ~= i; G[B[i]] ~= i; } par = new int[N]; dep = new int[N]; dist = new long[N]; dfs(0, -1, -1); auto dd = new Info[][](E, N); foreach (u; 0 .. N) { dd[0][u].last = u; dd[0][u].mn = INF; dd[0][u].xs[] = INF; foreach (i; G[u]) { const v = A[i] ^ B[i] ^ u; if (v != par[u]) { long tmp = C[i]; foreach (k; 0 .. 2) { if (dd[0][u].xs[k] > tmp) { swap(dd[0][u].xs[k], tmp); } } } } } foreach (e; 0 .. E - 1) { foreach (u; 0 .. N) { const l = dd[e][u].last; if (l == -1 || par[l] == -1) { dd[e + 1][u] = dd[e][u]; } else { dd[e + 1][u] = mul(dd[e][u], dd[e][par[l]]); } } } debug { foreach (e; 0 .. E - 1) { writefln("dd[%s] = %s", e, dd[e]); } } auto mns = new long[][](N, 3); foreach (u; 0 .. N) { mns[u][] = INF; foreach (i; G[u]) { long tmp = C[i]; foreach (k; 0 .. 3) { if (mns[u][k] > tmp) { swap(mns[u][k], tmp); } } } } foreach (q; 0 .. Q) { int u = X[q], v = Y[q]; if (dep[u] > dep[v]) { swap(u, v); } debug { writefln("dep[%s] = %s, dep[%s] = %s", u, dep[u], v, dep[v]); } int lca; long ans = INF; Info fu, fv; fu.last = fv.last = -2; foreach_reverse (e; 0 .. E) { if (dep[u] <= dep[v] - (1 << e)) { debug { writefln("%s -> %s", v, par[dd[e][v].last]); } fv = (fv.last == -2) ? dd[e][v] : mul(fv, dd[e][v]); v = par[dd[e][v].last]; } } if (u == v) { debug { writeln("u == v"); writeln("fv = ", fv); } lca = u; chmin(ans, (mns[u][0] != dist[fv.last] - dist[u]) ? mns[u][0] : mns[u][1]); chmin(ans, fv.mn); chmin(ans, fv.xs[0]); } else { foreach_reverse (e; 0 .. E) { if (dep[u] - (1 << e) >= 0 && par[dd[e][u].last] != par[dd[e][v].last]) { debug { writefln("%s -> %s, %s -> %s", u, par[dd[e][u].last], v, par[dd[e][v].last]); } fu = (fu.last == -2) ? dd[e][u] : mul(fu, dd[e][u]); fv = (fv.last == -2) ? dd[e][v] : mul(fv, dd[e][v]); u = par[dd[e][u].last]; v = par[dd[e][v].last]; } } debug { writeln("lca = ", lca); writeln("fu = ", fu); writeln("fv = ", fv); } lca = par[u]; long[2] tmps = [dist[u] - dist[lca], dist[v] - dist[lca]]; if (tmps[0] > tmps[1]) { swap(tmps[0], tmps[1]); } chmin(ans, (mns[lca][0] != tmps[0]) ? mns[lca][0] : (mns[lca][1] != tmps[1]) ? mns[lca][1] : mns[lca][2]); if (fu.last == -2) { chmin(ans, dd[0][u].xs[0]); } else { chmin(ans, fu.mn); chmin(ans, fu.xs[0]); } if (fv.last == -2) { chmin(ans, dd[0][v].xs[0]); } else { chmin(ans, fv.mn); chmin(ans, fv.xs[0]); } } debug { writeln("lca = ", lca, ", ans = ", ans); } if (ans >= INF) { writeln(-1); } else { ans *= 2; ans += dist[X[q]] + dist[Y[q]] - 2 * dist[lca]; writeln(ans); } } } } catch (EOFException e) { } }