結果
問題 | No.363 門松サイクル |
ユーザー | 👑 hos.lyric |
提出日時 | 2019-01-24 14:25:02 |
言語 | D (dmd 2.106.1) |
結果 |
AC
|
実行時間 | 333 ms / 4,000 ms |
コード長 | 4,277 bytes |
コンパイル時間 | 1,184 ms |
コンパイル使用メモリ | 119,544 KB |
実行使用メモリ | 33,920 KB |
最終ジャッジ日時 | 2024-06-13 03:38:35 |
合計ジャッジ時間 | 7,166 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 4 ms
5,376 KB |
testcase_03 | AC | 3 ms
5,376 KB |
testcase_04 | AC | 5 ms
5,376 KB |
testcase_05 | AC | 5 ms
5,376 KB |
testcase_06 | AC | 5 ms
5,376 KB |
testcase_07 | AC | 5 ms
5,376 KB |
testcase_08 | AC | 5 ms
5,376 KB |
testcase_09 | AC | 127 ms
12,884 KB |
testcase_10 | AC | 137 ms
12,372 KB |
testcase_11 | AC | 236 ms
25,220 KB |
testcase_12 | AC | 278 ms
24,392 KB |
testcase_13 | AC | 269 ms
19,120 KB |
testcase_14 | AC | 228 ms
18,540 KB |
testcase_15 | AC | 211 ms
24,080 KB |
testcase_16 | AC | 181 ms
18,592 KB |
testcase_17 | AC | 333 ms
33,872 KB |
testcase_18 | AC | 305 ms
18,480 KB |
testcase_19 | AC | 280 ms
19,116 KB |
testcase_20 | AC | 311 ms
18,484 KB |
testcase_21 | AC | 325 ms
29,744 KB |
testcase_22 | AC | 289 ms
18,332 KB |
testcase_23 | AC | 278 ms
26,496 KB |
testcase_24 | AC | 2 ms
5,376 KB |
testcase_25 | AC | 2 ms
5,376 KB |
testcase_26 | AC | 262 ms
33,792 KB |
testcase_27 | AC | 267 ms
33,920 KB |
testcase_28 | AC | 290 ms
25,984 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; } 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]) ? ((p == R) ? 1 : ((A[u] != A[par[0][p]]) ? (1 + dp[p][1]) : 1)) : 0; dp[u][1] = (u == R) ? 0 : (A[u] > A[p]) ? ((p == R) ? 1 : ((A[u] != A[par[0][p]]) ? (1 + dp[p][0]) : 1)) : 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; } int up(int u, int k) { foreach (e; 0 .. LIM_LOGN) { if ((k >> e) & 1) { u = par[e][u]; } } 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]; const l = LCA(u, v); const dU = dep[u] - dep[l]; const dV = dep[v] - dep[l]; debug { writeln(u, " ", v, ": ", l, " ", dU, " ", dV); } bool ans; 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); if (dU == 0) { // u <- <- v const vv = up(v, dV - 1); ans = ans && (A[v] != A[vv]); ans = ans && (A[u] != A[par[0][v]]); } else if (dV == 0) { // u -> -> v const uu = up(u, dU - 1); ans = ans && (A[u] != A[uu]); ans = ans && (A[v] != A[par[0][u]]); } else { // u -> -> l <- <- <- v const uu = up(u, dU - 1); const vv = up(v, dV - 1); ans = ans && (A[uu] != A[vv]); ans = ans && (A[u] != A[par[0][v]]); ans = ans && (A[v] != A[par[0][u]]); } } writeln(ans ? "YES" : "NO"); } } } catch (EOFException e) { } }