結果

問題 No.363 門松サイクル
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-24 14:25:02
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 307 ms / 4,000 ms
コード長 4,277 bytes
コンパイル時間 847 ms
コンパイル使用メモリ 106,204 KB
実行使用メモリ 37,156 KB
最終ジャッジ日時 2023-09-03 23:02:29
合計ジャッジ時間 7,077 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 5 ms
4,376 KB
testcase_03 AC 4 ms
5,100 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 5 ms
4,376 KB
testcase_06 AC 5 ms
4,376 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 123 ms
12,504 KB
testcase_10 AC 128 ms
13,048 KB
testcase_11 AC 226 ms
26,400 KB
testcase_12 AC 258 ms
25,028 KB
testcase_13 AC 253 ms
18,776 KB
testcase_14 AC 219 ms
18,424 KB
testcase_15 AC 206 ms
23,744 KB
testcase_16 AC 176 ms
22,408 KB
testcase_17 AC 307 ms
35,456 KB
testcase_18 AC 292 ms
20,828 KB
testcase_19 AC 264 ms
20,548 KB
testcase_20 AC 288 ms
21,064 KB
testcase_21 AC 303 ms
31,720 KB
testcase_22 AC 277 ms
21,108 KB
testcase_23 AC 258 ms
28,296 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 266 ms
35,920 KB
testcase_27 AC 268 ms
37,156 KB
testcase_28 AC 277 ms
27,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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) {
  }
}
0