結果

問題 No.1153 ねこちゃんゲーム
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-08-07 21:49:41
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 371 ms / 2,500 ms
コード長 4,911 bytes
コンパイル時間 805 ms
コンパイル使用メモリ 121,972 KB
実行使用メモリ 58,612 KB
最終ジャッジ日時 2024-06-22 07:59:52
合計ジャッジ時間 23,449 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 3 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 333 ms
24,828 KB
testcase_08 AC 333 ms
24,824 KB
testcase_09 AC 326 ms
24,832 KB
testcase_10 AC 333 ms
24,932 KB
testcase_11 AC 329 ms
24,832 KB
testcase_12 AC 328 ms
24,796 KB
testcase_13 AC 319 ms
24,896 KB
testcase_14 AC 327 ms
24,852 KB
testcase_15 AC 322 ms
24,976 KB
testcase_16 AC 333 ms
24,892 KB
testcase_17 AC 320 ms
24,820 KB
testcase_18 AC 328 ms
24,860 KB
testcase_19 AC 325 ms
24,852 KB
testcase_20 AC 325 ms
24,892 KB
testcase_21 AC 316 ms
24,860 KB
testcase_22 AC 321 ms
24,892 KB
testcase_23 AC 320 ms
24,836 KB
testcase_24 AC 327 ms
24,816 KB
testcase_25 AC 316 ms
24,800 KB
testcase_26 AC 316 ms
24,824 KB
testcase_27 AC 371 ms
57,632 KB
testcase_28 AC 369 ms
58,612 KB
testcase_29 AC 363 ms
54,528 KB
testcase_30 AC 359 ms
54,096 KB
testcase_31 AC 360 ms
44,932 KB
testcase_32 AC 275 ms
27,644 KB
testcase_33 AC 278 ms
26,780 KB
testcase_34 AC 272 ms
27,688 KB
testcase_35 AC 272 ms
26,468 KB
testcase_36 AC 298 ms
27,104 KB
testcase_37 AC 235 ms
27,124 KB
testcase_38 AC 209 ms
28,968 KB
testcase_39 AC 223 ms
27,488 KB
testcase_40 AC 231 ms
27,248 KB
testcase_41 AC 213 ms
26,624 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