結果
問題 | No.363 門松サイクル |
ユーザー | 👑 hos.lyric |
提出日時 | 2019-01-24 14:01:06 |
言語 | D (dmd 2.106.1) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,408 bytes |
コンパイル時間 | 871 ms |
コンパイル使用メモリ | 119,552 KB |
実行使用メモリ | 29,184 KB |
最終ジャッジ日時 | 2024-06-13 03:38:23 |
合計ジャッジ時間 | 8,447 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | AC | 4 ms
5,376 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 | 286 ms
29,100 KB |
testcase_18 | AC | 279 ms
18,348 KB |
testcase_19 | WA | - |
testcase_20 | AC | 279 ms
18,476 KB |
testcase_21 | AC | 297 ms
26,292 KB |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | AC | 1 ms
5,376 KB |
testcase_25 | AC | 1 ms
5,376 KB |
testcase_26 | AC | 230 ms
29,184 KB |
testcase_27 | WA | - |
testcase_28 | WA | - |
ソースコード
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; } 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(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; int[] X, Y; int Q; int[] U, V; int R; int[][] G; int[][] par; int[] dep; int[][] dp; void dfs(int u, int p) { par[0][u] = p; dep[u] = (u == R) ? 0 : (dep[p] + 1); dp[u][0] = (u == R) ? 0 : (A[u] < A[p]) ? (1 + dp[p][1]) : 0; dp[u][1] = (u == R) ? 0 : (A[u] > A[p]) ? (1 + dp[p][0]) : 0; foreach (v; G[u]) { if (v != p) { dfs(v, u); } } } int LCA(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; } void main() { try { for (; ; ) { N = readInt(); A = new int[N]; foreach (u; 0 .. N) { A[u] = readInt(); } X = new int[N - 1]; Y = new int[N - 1]; foreach (i; 0 .. N - 1) { X[i] = readInt() - 1; Y[i] = readInt() - 1; } Q = readInt(); U = new int[Q]; V = new int[Q]; foreach (q; 0 .. Q) { U[q] = readInt() - 1; V[q] = readInt() - 1; } R = 0; G = new int[][N]; foreach (i; 0 .. N - 1) { G[X[i]] ~= Y[i]; G[Y[i]] ~= X[i]; } par = new int[][](LIM_LOGN, N); dep = new int[N]; dp = new int[][](N, 2); dfs(R, R); foreach (e; 1 .. LIM_LOGN) { foreach (u; 0 .. N) { par[e][u] = par[e - 1][par[e - 1][u]]; } } debug { writeln("dp = ", dp); } foreach (q; 0 .. Q) { const u = U[q]; const v = V[q]; bool ans; const l = LCA(u, v); const dU = dep[u] - dep[l]; const dV = dep[v] - dep[l]; debug { writeln(u, " ", v, ": ", l, " ", dU, " ", dV); } if (dU + dV + 1 >= 3 && (dU + dV + 1) % 2 == 0) { ans = ans || (A[u] < A[v] && dp[u][0] >= dU && dp[v][1] >= dV); ans = ans || (A[u] > A[v] && dp[u][1] >= dU && dp[v][0] >= dV); } writeln(ans ? "YES" : "NO"); } } } catch (EOFException e) { } }