結果

問題 No.196 典型DP (1)
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-10 17:11:28
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 37 ms / 2,000 ms
コード長 2,114 bytes
コンパイル時間 801 ms
コンパイル使用メモリ 104,424 KB
実行使用メモリ 23,384 KB
最終ジャッジ日時 2023-09-03 21:55:36
合計ジャッジ時間 2,798 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 4 ms
4,376 KB
testcase_17 AC 6 ms
4,380 KB
testcase_18 AC 10 ms
4,384 KB
testcase_19 AC 11 ms
4,376 KB
testcase_20 AC 15 ms
4,376 KB
testcase_21 AC 15 ms
4,376 KB
testcase_22 AC 15 ms
4,380 KB
testcase_23 AC 21 ms
13,180 KB
testcase_24 AC 20 ms
10,100 KB
testcase_25 AC 20 ms
10,624 KB
testcase_26 AC 23 ms
22,760 KB
testcase_27 AC 23 ms
23,372 KB
testcase_28 AC 23 ms
23,384 KB
testcase_29 AC 23 ms
23,240 KB
testcase_30 AC 23 ms
23,364 KB
testcase_31 AC 36 ms
7,460 KB
testcase_32 AC 36 ms
7,472 KB
testcase_33 AC 36 ms
7,460 KB
testcase_34 AC 36 ms
7,468 KB
testcase_35 AC 37 ms
7,464 KB
testcase_36 AC 34 ms
7,464 KB
testcase_37 AC 16 ms
5,552 KB
testcase_38 AC 33 ms
7,412 KB
testcase_39 AC 31 ms
7,488 KB
testcase_40 AC 36 ms
7,472 KB
testcase_41 AC 2 ms
4,504 KB
testcase_42 AC 1 ms
4,504 KB
testcase_43 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, 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[0]; tokens.popFront; return token; }
int readInt() { return readToken().to!int; }
long readLong() { return readToken().to!long; }
real readReal() { return readToken().to!real; }

void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); }


immutable MO = 1_000_000_007L;

int N, K;
int[] A, B;

int[][] G;
int[] sz;
long[][] dp;

void dfs(int u, int p) {
  foreach (v; G[u]) {
    if (v != p) {
      dfs(v, u);
    }
  }
  sz[u] = 0;
  dp[u] = [1];
  foreach (v; G[u]) {
    if (v != p) {
      auto nxt = new long[sz[u] + sz[v] + 1];
      foreach (x; 0 .. sz[u] + 1) foreach (y; 0 .. sz[v] + 1) {
        (nxt[x + y] += dp[u][x] * dp[v][y]) %= MO;
      }
      sz[u] += sz[v];
      dp[u] = nxt;
    }
  }
  sz[u] += 1;
  dp[u] ~= 1;
  debug {
    writefln("sz[%s] = %s", u, sz[u]);
    writefln("dp[%s] = %s", u, dp[u]);
  }
}

void main() {
  try {
    for (; ; ) {
      N = readInt();
      K = readInt();
      A = new int[N - 1];
      B = new int[N - 1];
      foreach (i; 0 .. N - 1) {
        A[i] = readInt();
        B[i] = readInt();
      }
      
      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[A[i]] ~= B[i];
        G[B[i]] ~= A[i];
      }
      sz = new int[N];
      dp = new long[][N];
      dfs(0, -1);
      writeln(dp[0][K]);
    }
  } catch (EOFException e) {
  }
}
0