結果
| 問題 |
No.196 典型DP (1)
|
| コンテスト | |
| ユーザー |
chocorusk
|
| 提出日時 | 2018-12-13 19:55:28 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 80 ms / 2,000 ms |
| コード長 | 1,073 bytes |
| コンパイル時間 | 743 ms |
| コンパイル使用メモリ | 99,408 KB |
| 実行使用メモリ | 66,304 KB |
| 最終ジャッジ日時 | 2024-09-25 04:36:13 |
| 合計ジャッジ時間 | 4,690 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 41 |
ソースコード
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD=1e9+7;
vector<int> g[2000];
bool used[2000];
int num[2000], c[4000];
int s[2000];
int ct;
int n;
void dfs(int x){
s[x]=1;
num[x]=ct;
c[ct]=x;
ct++;
used[x]=1;
for(auto y:g[x]){
if(!used[y]){
dfs(y);
s[x]+=s[y];
}
}
c[ct]=x+n;
ct++;
}
int main()
{
int k;
cin>>n>>k;
for(int i=0; i<n-1; i++){
int a, b;
cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
if(k==n){
cout<<1<<endl;
return 0;
}
dfs(0);
ll dp[4001][2001]={};
dp[0][0]=1;
for(int i=1; i<2*n; i++){
for(int j=0; j<=k; j++){
dp[i][j]+=dp[i-1][j];
if(c[i]>n && j>=s[c[i]-n]) dp[i][j]+=dp[num[c[i]-n]-1][j-s[c[i]-n]];
dp[i][j]%=MOD;
}
}
cout<<dp[2*n-1][k]<<endl;
return 0;
}
chocorusk