結果

問題 No.912 赤黒木
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-10-18 22:14:58
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 753 ms / 3,000 ms
コード長 3,017 bytes
コンパイル時間 719 ms
コンパイル使用メモリ 106,552 KB
実行使用メモリ 99,856 KB
最終ジャッジ日時 2023-09-04 03:04:54
合計ジャッジ時間 17,613 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 658 ms
57,276 KB
testcase_13 AC 675 ms
59,332 KB
testcase_14 AC 625 ms
53,976 KB
testcase_15 AC 677 ms
59,716 KB
testcase_16 AC 648 ms
56,468 KB
testcase_17 AC 649 ms
58,800 KB
testcase_18 AC 639 ms
54,184 KB
testcase_19 AC 616 ms
54,364 KB
testcase_20 AC 613 ms
52,656 KB
testcase_21 AC 607 ms
52,720 KB
testcase_22 AC 672 ms
58,936 KB
testcase_23 AC 674 ms
60,400 KB
testcase_24 AC 671 ms
60,280 KB
testcase_25 AC 647 ms
56,876 KB
testcase_26 AC 669 ms
60,920 KB
testcase_27 AC 638 ms
57,132 KB
testcase_28 AC 675 ms
99,856 KB
testcase_29 AC 676 ms
98,972 KB
testcase_30 AC 753 ms
98,004 KB
testcase_31 AC 674 ms
93,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, 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)); }


enum INF = 10^^9;

int N;
int[] A, B;
int[] C, D;

int[][] G;
int[] black;
int[][][] dp;

void dfs(int u, int p) {
  foreach (v; G[u]) {
    if (v != p) {
      dfs(v, u);
    }
  }
  auto crt = new int[][](2, 3);
  foreach (s; 0 .. 2) foreach (t; 0 .. 3) {
    crt[s][t] = INF;
  }
  crt[0][0] = 0;
  foreach (v; G[u]) {
    if (v != p) {
      auto nxt = new int[][](2, 3);
      foreach (s; 0 .. 2) foreach (t; 0 .. 3) {
        nxt[s][t] = INF;
      }
      foreach (s; 0 .. 2) foreach (t; 0 .. 3) {
        if (crt[s][t] < INF) {
          foreach (ss; 0 .. 2) foreach (tt; 0 .. 3) {
            if (t + tt < 3) {
              chmin(nxt[s ^ ss][t + tt], crt[s][t] + ss + dp[v][ss][tt]);
            }
          }
        }
      }
      crt = nxt;
    }
  }
  if (black[u]) {
    foreach (s; 0 .. 2) foreach (t; 0 .. 3) {
      chmin(dp[u][s ^ 1][t], crt[s][t]);
      if (t + 1 < 3) {
        chmin(dp[u][s][t + 1], crt[s][t]);
      }
    }
  } else {
    dp[u] = crt;
  }
}

void main() {
  try {
    for (; ; ) {
      N = readInt();
      A = new int[N - 1];
      B = new int[N - 1];
      foreach (i; 0 .. N - 1) {
        A[i] = readInt() - 1;
        B[i] = readInt() - 1;
      }
      C = new int[N - 1];
      D = new int[N - 1];
      foreach (i; 0 .. N - 1) {
        C[i] = readInt() - 1;
        D[i] = readInt() - 1;
      }
      
      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[A[i]] ~= B[i];
        G[B[i]] ~= A[i];
      }
      black = new int[N];
      foreach (i; 0 .. N - 1) {
        black[C[i]] ^= 1;
        black[D[i]] ^= 1;
      }
      dp = new int[][][](N, 2, 3);
      foreach (u; 0 .. N) {
        foreach (s; 0 .. 2) foreach (t; 0 .. 3) {
          dp[u][s][t] = INF;
        }
      }
      dfs(0, -1);
      int ans = dp[0][0][2];
      ans += N - 1;
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0