結果
問題 | No.196 典型DP (1) |
ユーザー | kazuma |
提出日時 | 2018-11-27 13:20:00 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 25 ms / 2,000 ms |
コード長 | 1,990 bytes |
コンパイル時間 | 1,819 ms |
コンパイル使用メモリ | 169,868 KB |
実行使用メモリ | 19,584 KB |
最終ジャッジ日時 | 2024-06-23 08:41:22 |
合計ジャッジ時間 | 3,744 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 13 ms
19,200 KB |
testcase_01 | AC | 13 ms
19,200 KB |
testcase_02 | AC | 13 ms
19,200 KB |
testcase_03 | AC | 13 ms
19,200 KB |
testcase_04 | AC | 13 ms
19,200 KB |
testcase_05 | AC | 13 ms
19,200 KB |
testcase_06 | AC | 13 ms
19,072 KB |
testcase_07 | AC | 14 ms
19,200 KB |
testcase_08 | AC | 14 ms
19,200 KB |
testcase_09 | AC | 14 ms
19,200 KB |
testcase_10 | AC | 13 ms
19,328 KB |
testcase_11 | AC | 13 ms
19,328 KB |
testcase_12 | AC | 14 ms
19,200 KB |
testcase_13 | AC | 13 ms
19,200 KB |
testcase_14 | AC | 13 ms
19,328 KB |
testcase_15 | AC | 13 ms
19,200 KB |
testcase_16 | AC | 15 ms
19,200 KB |
testcase_17 | AC | 16 ms
19,200 KB |
testcase_18 | AC | 17 ms
19,200 KB |
testcase_19 | AC | 17 ms
19,200 KB |
testcase_20 | AC | 19 ms
19,072 KB |
testcase_21 | AC | 20 ms
19,200 KB |
testcase_22 | AC | 20 ms
19,328 KB |
testcase_23 | AC | 21 ms
19,328 KB |
testcase_24 | AC | 20 ms
19,328 KB |
testcase_25 | AC | 20 ms
19,328 KB |
testcase_26 | AC | 20 ms
19,328 KB |
testcase_27 | AC | 20 ms
19,456 KB |
testcase_28 | AC | 20 ms
19,456 KB |
testcase_29 | AC | 20 ms
19,584 KB |
testcase_30 | AC | 20 ms
19,584 KB |
testcase_31 | AC | 25 ms
19,328 KB |
testcase_32 | AC | 25 ms
19,072 KB |
testcase_33 | AC | 25 ms
19,328 KB |
testcase_34 | AC | 25 ms
19,200 KB |
testcase_35 | AC | 25 ms
19,200 KB |
testcase_36 | AC | 25 ms
19,200 KB |
testcase_37 | AC | 20 ms
19,200 KB |
testcase_38 | AC | 24 ms
19,200 KB |
testcase_39 | AC | 24 ms
19,200 KB |
testcase_40 | AC | 24 ms
19,200 KB |
testcase_41 | AC | 13 ms
19,200 KB |
testcase_42 | AC | 14 ms
19,200 KB |
testcase_43 | AC | 13 ms
19,200 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; template<int MOD> class mod_int { unsigned x; public: mod_int() : x(0) { } mod_int(int sig) { int sigt = sig % MOD; if (sigt < 0) sigt += MOD; x = sigt; } mod_int(long long sig) { int sigt = sig % MOD; if (sigt < 0) sigt += MOD; x = sigt; } int get() const { return (int)x; } mod_int &operator+=(mod_int that) { if ((x += that.x) >= MOD) x -= MOD; return *this; } mod_int &operator-=(mod_int that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; } mod_int &operator*=(mod_int that) { x = (unsigned long long)x * that.x % MOD; return *this; } mod_int &operator/=(mod_int that) { return *this *= that.inverse(); } mod_int operator+(mod_int that) const { return mod_int(*this) += that; } mod_int operator-(mod_int that) const { return mod_int(*this) -= that; } mod_int operator*(mod_int that) const { return mod_int(*this) *= that; } mod_int operator/(mod_int that) const { return mod_int(*this) /= that; } bool operator==(const mod_int& that) const { return x == that.x; } mod_int inverse() const { long long a = x, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } return mod_int(u); } }; const int mod = 1e9 + 7; using mint = mod_int<mod>; const int MAX = 2000; vector<int> G[MAX]; int size[MAX]; mint dp[MAX][MAX + 1]; mint tmp[MAX + 1]; void dfs(int v, int prev) { size[v] = 0; dp[v][0] = 1; for (auto to : G[v]) if (to != prev) { dfs(to, v); for (int j = 0; j <= size[v]; j++) { tmp[j] = dp[v][j]; dp[v][j] = 0; } for (int j = size[v]; j >= 0; j--) { for (int k = 0; k <= size[to]; k++) { dp[v][j + k] += tmp[j] * dp[to][k]; } } size[v] += size[to]; } size[v] += 1; dp[v][size[v]] += 1; } int main() { int N, K; cin >> N >> K; for (int i = 0; i < N - 1; i++) { int u, v; cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } dfs(0, -1); cout << dp[0][K].get() << endl; return 0; }