結果

問題 No.1718 Random Squirrel
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-10-22 22:23:22
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 165 ms / 2,000 ms
コード長 4,184 bytes
コンパイル時間 805 ms
コンパイル使用メモリ 125,472 KB
実行使用メモリ 30,616 KB
最終ジャッジ日時 2024-06-22 12:52:06
合計ジャッジ時間 5,942 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

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)); }


enum INF = 10^^9;

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

int[][] G;
int[] par;
alias Result = Tuple!(int, "sum", int, "mx");
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
  dp[u].sum = 0;
  dp[u].mx = C[u] ? 0 : -INF;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      // add dp[v]
      if (dp[v].mx >= 0) {
        dp[u].sum += 1 + dp[v].sum;
        chmax(dp[u].mx, 1 + dp[v].mx);
      }
    }
  }
  // calc dp[u]
}

void DFS(int u, int p) {
  // init
  int sum;
  int[2] mxs = [C[u] ? 0 : -INF, -INF];
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      // add dp[v]
      if (dp[v].mx >= 0) {
        sum += 1 + dp[v].sum;
        int tmp = 1 + dp[v].mx;
        foreach (k; 0 .. 2) {
          if (mxs[k] < tmp) {
            swap(mxs[k], tmp);
          }
        }
      }
    }
  }
  if (p != -1) {
    // add DP[u]
    if (DP[u].mx >= 0) {
      sum += 1 + DP[u].sum;
      int tmp = 1 + DP[u].mx;
      foreach (k; 0 .. 2) {
        if (mxs[k] < tmp) {
          swap(mxs[k], tmp);
        }
      }
    }
  }
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      // calc DP[v], removing dp[v]
      DP[v].sum = sum;
      DP[v].mx = mxs[0];
      if (dp[v].mx >= 0) {
        DP[v].sum -= (1 + dp[v].sum);
        if (mxs[0] == 1 + dp[v].mx) {
          DP[v].mx = mxs[1];
        }
      }
    }
  }
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      DFS(v, u);
    }
  }
}


void main() {
  try {
    for (; ; ) {
      N = readInt();
      const K = 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 bool[N];
      foreach (k; 0 .. K) {
        const u = readInt() - 1;
        C[u] = true;
      }
      
      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[A[i]] ~= i;
        G[B[i]] ~= i;
      }
      par = new int[N];
      dp = new Result[N];
      DP = new Result[N];
      
      const rt = 0;
      dfs(rt, -1);
      DFS(rt, -1);
      debug {
        writeln("rt = ", rt);
        writeln("dp = ", dp);
        writeln("DP = ", DP);
      }
      foreach (u; 0 .. N) {
        // init
        int sum;
        int mx = C[u] ? 0 : -INF;
        foreach (i; G[u]) {
          const v = A[i] ^ B[i] ^ u;
          if (v != par[u]) {
            // add dp[v]
            if (dp[v].mx >= 0) {
              sum += 1 + dp[v].sum;
              chmax(mx, 1 + dp[v].mx);
            }
          }
        }
        if (u != rt) {
          // add DP[u]
          if (DP[u].mx >= 0) {
            sum += 1 + DP[u].sum;
            chmax(mx, 1 + DP[u].mx);
          }
        }
        // calc ans, rooted at u
        assert(mx >= 0);
        const ans = 2 * sum - mx;
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}
0