結果

問題 No.1153 ねこちゃんゲーム
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-08-07 21:49:41
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 610 ms / 2,500 ms
コード長 4,911 bytes
コンパイル時間 744 ms
コンパイル使用メモリ 107,968 KB
実行使用メモリ 59,488 KB
最終ジャッジ日時 2023-09-04 08:51:40
合計ジャッジ時間 32,662 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 3 ms
4,384 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 505 ms
25,012 KB
testcase_08 AC 559 ms
25,012 KB
testcase_09 AC 504 ms
24,984 KB
testcase_10 AC 509 ms
25,200 KB
testcase_11 AC 491 ms
25,096 KB
testcase_12 AC 503 ms
25,188 KB
testcase_13 AC 503 ms
25,064 KB
testcase_14 AC 486 ms
25,120 KB
testcase_15 AC 502 ms
25,268 KB
testcase_16 AC 518 ms
25,072 KB
testcase_17 AC 539 ms
25,136 KB
testcase_18 AC 519 ms
25,000 KB
testcase_19 AC 501 ms
25,028 KB
testcase_20 AC 494 ms
25,076 KB
testcase_21 AC 510 ms
25,152 KB
testcase_22 AC 507 ms
25,208 KB
testcase_23 AC 494 ms
24,972 KB
testcase_24 AC 488 ms
24,996 KB
testcase_25 AC 489 ms
25,248 KB
testcase_26 AC 519 ms
25,144 KB
testcase_27 AC 610 ms
58,260 KB
testcase_28 AC 598 ms
59,488 KB
testcase_29 AC 574 ms
54,780 KB
testcase_30 AC 551 ms
54,532 KB
testcase_31 AC 551 ms
45,392 KB
testcase_32 AC 375 ms
27,004 KB
testcase_33 AC 378 ms
27,844 KB
testcase_34 AC 377 ms
27,124 KB
testcase_35 AC 367 ms
28,000 KB
testcase_36 AC 372 ms
26,564 KB
testcase_37 AC 294 ms
26,808 KB
testcase_38 AC 268 ms
29,220 KB
testcase_39 AC 281 ms
28,116 KB
testcase_40 AC 293 ms
26,904 KB
testcase_41 AC 266 ms
26,732 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[] par;
int[] dp, DP;

void dfs(int u, int p) {
  par[u] = p;
  // init
  const lim = cast(int)(G[u].length) + 2;
  auto cnt = new int[lim];
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      dfs(v, u);
    }
  }
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      // add dp[v]
      if (dp[v] < lim) {
        ++cnt[dp[v]];
      }
    }
  }
  // calc dp[u]
  foreach (g; 0 .. lim) {
    if (cnt[g] == 0) {
      dp[u] = g;
      break;
    }
  }
}

void DFS(int u, int p) {
  // init
  const lim = cast(int)(G[u].length + 2);
  auto cnt = new int[lim];
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      // add dp[v]
      if (dp[v] < lim) {
        ++cnt[dp[v]];
      }
    }
  }
  if (p != -1) {
    // add DP[u]
    if (DP[u] < lim) {
      ++cnt[DP[u]];
    }
  }
  int g0;
  foreach (g; 0 .. lim) {
    if (cnt[g] == 0) {
      g0 = g;
      break;
    }
  }
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      // calc DP[v], removing dp[v]
      DP[v] = g0;
      if (dp[v] < lim) {
        --cnt[dp[v]];
        if (cnt[dp[v]] == 0) {
          chmin(DP[v], dp[v]);
        }
        ++cnt[dp[v]];
      }
    }
  }
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      DFS(v, u);
    }
  }
}


void main() {
  try {
    for (; ; ) {
      N = readInt();
      const M = readInt();
      auto C = new int[M];
      foreach (j; 0 .. M) {
        C[j] = readInt() - 1;
      }
      A = new int[N - 1];
      B = new int[N - 1];
      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;
      }
      
      par = new int[N];
      dp = new int[N];
      DP = new int[N];
      par[] = -1;
      dp[] = -1;
      DP[] = -1;
      const rt = 0;
      dfs(rt, -1);
      DFS(rt, -1);
      debug {
        writeln("rt = ", rt);
        writeln("dp = ", dp);
        writeln("DP = ", DP);
      }
      auto gs = new int[N];
      gs[] = -1;
      foreach (u; 0 .. N) {
        // init
        const lim = cast(int)(G[u].length) + 2;
        auto cnt = new int[lim];
        foreach (i; G[u]) {
          const v = A[i] ^ B[i] ^ u;
          if (v != par[u]) {
            // add dp[v]
            if (dp[v] < lim) {
              ++cnt[dp[v]];
            }
          }
        }
        if (u != rt) {
          // add DP[u]
          if (DP[u] < lim) {
            ++cnt[DP[u]];
          }
        }
        // calc ans, rooted at u
        foreach (g; 0 .. lim) {
          if (cnt[g] == 0) {
            gs[u] = g;
            break;
          }
        }
      }
      debug {
        writeln("gs = ", gs);
      }
      
      int sum;
      foreach (j; 0 .. M) {
        sum ^= gs[C[j]];
      }
      if (sum) {
        int jm = -1, vm = -1;
        foreach (j; 0 .. M) {
          const target = sum ^ gs[C[j]];
          if (target < gs[C[j]]) {
            jm = j;
            const u = C[j];
            foreach (i; G[u]) {
              const v = A[i] ^ B[i] ^ u;
              if (v != par[u]) {
                // add dp[v]
                if (dp[v] == target) {
                  vm = v;
                  goto found;
                }
              }
            }
            if (u != rt) {
              // add DP[u]
              if (DP[u] == target) {
                vm = par[u];
                goto found;
              }
            }
          }
        }
       found:
        writeln(jm + 1, " ", vm + 1);
      } else {
        writeln("-1 -1");
      }
    }
  } catch (EOFException e) {
  }
}
0