結果

問題 No.1295 木と駒
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-11-21 03:32:49
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 368 ms / 2,000 ms
コード長 5,420 bytes
コンパイル時間 1,186 ms
コンパイル使用メモリ 117,536 KB
実行使用メモリ 47,260 KB
最終ジャッジ日時 2023-09-04 11:10:32
合計ジャッジ時間 14,858 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 303 ms
15,672 KB
testcase_15 AC 301 ms
15,660 KB
testcase_16 AC 307 ms
15,740 KB
testcase_17 AC 305 ms
15,680 KB
testcase_18 AC 299 ms
15,616 KB
testcase_19 AC 294 ms
18,132 KB
testcase_20 AC 320 ms
47,260 KB
testcase_21 AC 305 ms
26,184 KB
testcase_22 AC 298 ms
20,444 KB
testcase_23 AC 306 ms
32,224 KB
testcase_24 AC 310 ms
31,648 KB
testcase_25 AC 296 ms
18,744 KB
testcase_26 AC 269 ms
43,968 KB
testcase_27 AC 269 ms
44,456 KB
testcase_28 AC 368 ms
44,092 KB
testcase_29 AC 270 ms
44,988 KB
testcase_30 AC 268 ms
44,168 KB
testcase_31 AC 267 ms
43,932 KB
testcase_32 AC 335 ms
34,680 KB
testcase_33 AC 306 ms
15,760 KB
testcase_34 AC 327 ms
15,628 KB
testcase_35 AC 305 ms
15,284 KB
testcase_36 AC 309 ms
15,336 KB
testcase_37 AC 310 ms
17,552 KB
testcase_38 AC 319 ms
17,292 KB
testcase_39 AC 303 ms
15,368 KB
testcase_40 AC 305 ms
15,408 KB
testcase_41 AC 292 ms
18,656 KB
testcase_42 AC 302 ms
18,116 KB
testcase_43 AC 295 ms
18,144 KB
testcase_44 AC 303 ms
19,468 KB
testcase_45 AC 299 ms
18,664 KB
testcase_46 AC 296 ms
20,852 KB
testcase_47 AC 294 ms
19,680 KB
testcase_48 AC 300 ms
20,860 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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;

int[][] G;
int home(int u) {
  if (G[u].empty) {
    return -1;
  }
  const i = G[u][0];
  return A[i] ^ B[i] ^ u;
}

int[] par;
// 0: come back, 1: go
alias Result = int[2];
Result[] dp, DP;

void dfs(int u, int p) {
  par[u] = p;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      dfs(v, u);
    }
  }
  // init
  Result[] rs;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      // add dp[v]
      rs ~= dp[v];
    }
  }
  // calc dp[u]
  const len = cast(int)(rs.length);
  dp[u][0] = (p != -1 && p == home(u)) ? 1 : 0;
  foreach (j; 0 .. len) {
    dp[u][0] &= rs[j][0];
  }
  dp[u][1] = 1;
  if (len >= 1) {
    int okL = 1, okR = 1;
    okL &= (p != (A[G[u][0]] ^ B[G[u][0]] ^ u)) ? 1 : 0;
    okL &= rs[0][1];
    foreach (j; 1 .. len) {
      okL &= rs[j][0];
    }
    foreach (j; 0 .. len - 1) {
      okR &= rs[j][0];
    }
    okR &= rs[len - 1][1];
    dp[u][1] &= okL | okR;
  }
}

void DFS(int u, int p) {
  // init
  alias Entry = Tuple!(Result, "r", int, "v");
  Entry[] es;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    /*
    if (v != p) {
      // add dp[v]
    }
    */
    if (v != p) {
      es ~= Entry(dp[v], v);
    } else {
      es ~= Entry(DP[u], -1);
    }
  }
  /*
  if (p != -1) {
    // add DP[u]
  }
  */
  /*
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      // calc DP[v], removing dp[v]
    }
  }
  */
  const len = cast(int)(es.length);
  auto sums = new int[len + 1];
  foreach (j; 0 .. len) {
    sums[j + 1] = sums[j] + es[j].r[0];
  }
  foreach (j; 0 .. len) {
    const v = es[j].v;
    if (v != -1) {
      DP[v][0] = (home(u) == v) ? 1 : 0;
      DP[v][0] &= (sums[j] + (sums[len] - sums[j + 1]) >= len - 1) ? 1 : 0;
      DP[v][1] = 1;
      if (len - 1 >= 1) {
        int okL = 1, okR = 1;
        if (j == 0) {
          /*
          okL &= es[1].r[1];
          okL &= (sums[len] - sums[2] >= len - 2) ? 1 : 0;
          */
          okL = 0;
        } else {
          okL &= es[0].r[1];
          okL &= ((sums[j] - sums[1]) + (sums[len] - sums[j + 1]) >= len - 2) ? 1 : 0;
        }
        if (j == len - 1) {
          okR &= (sums[len - 2] >= len - 2) ? 1 : 0;
          okR &= es[len - 2].r[1];
        } else {
          okR &= (sums[j] + (sums[len - 1] - sums[j + 1]) >= len - 2) ? 1 : 0;
          okR &= es[len - 1].r[1];
        }
        DP[v][1] &= okL | okR;
      }
    }
  }
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      DFS(v, u);
    }
  }
}

void main() {
  try {
    for (; ; ) {
      N = readInt();
      A = new int[N];
      B = new int[N];
      foreach (i; 0 .. N - 1) {
        A[i] = readInt() - 1;
        B[i] = readInt() - 1;
      }
      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[A[i]] ~= i;
        G[B[i]] ~= i;
      }
      foreach (u; 0 .. N) {
        G[u].sort!((i0, i1) => ((A[i0] ^ B[i0] ^ u) < (A[i1] ^ B[i1] ^ u)));
      }
      debug {
        writeln("G = ", G);
      }
      
      const rt = 0;
      par = new int[N];
      dp = new Result[N];
      dfs(rt, -1);
      DP = new Result[N];
      DFS(rt, -1);
      debug {
        writeln("rt = ", rt);
        writeln("dp = ", dp);
        writeln("DP = ", DP);
      }
      foreach (u; 0 .. N) {
        // init
        Result[] rs;
        foreach (i; G[u]) {
          const v = A[i] ^ B[i] ^ u;
          /*
          if (v != par[u]) {
            // add dp[v]
          }
          */
          if (v != par[u]) {
            rs ~= dp[v];
          } else {
            rs ~= DP[u];
          }
        }
        /*
        if (u != rt) {
          // add DP[u]
        }
        */
        debug {
          writeln(u, ": ", rs);
        }
        // calc ans, rooted at u
        const len = cast(int)(rs.length);
        int ans = 1;
        if (len >= 1) {
          int okL = 1, okR = 1;
          okL &= rs[0][1];
          foreach (j; 1 .. len) {
            okL &= rs[j][0];
          }
          foreach (j; 0 .. len - 1) {
            okR &= rs[j][0];
          }
          okR &= rs[len - 1][1];
          ans &= okL | okR;
        }
        writeln(ans ? "Yes" : "No");
      }
    }
  } catch (EOFException e) {
  }
}
0