結果
問題 | No.196 典型DP (1) |
ユーザー | 夕叢霧香(ゆうむらきりか) |
提出日時 | 2018-01-22 11:44:14 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 555 ms / 2,000 ms |
コード長 | 2,918 bytes |
コンパイル時間 | 3,824 ms |
コンパイル使用メモリ | 87,152 KB |
実行使用メモリ | 175,944 KB |
最終ジャッジ日時 | 2024-12-25 22:15:40 |
合計ジャッジ時間 | 15,013 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 41 |
ソースコード
import java.io.*;import java.util.*;class Main {static final long MOD=1000000007;static ArrayList<Integer>[]g;static int k;static long[][][]dp;static void mul(long[]x,long[]y,int xl,int yl){long[]r=new long[k+1];for(int i=0;i<=xl;++i){for(int j=0;j<=Math.min(k-i,yl);++j){r[i+j]=(r[i+j]+x[i]*y[j])%MOD;}}System.arraycopy(r,0,x,0,k+1);}static int dfs(int v,int p){int c=1;dp[0][v][0]=1;if(k>=1)dp[1][v][1]=1;long[]tmp=new long[k+1];for(int w:g[v]){if(p==w)continue;int sub=dfs(w,v);mul(dp[1][v],dp[1][w],c,sub);for(int j=0;j<=k;++j)tmp[j]=(dp[0][w][j]+dp[1][w][j])%MOD;mul(dp[0][v],tmp,c,sub);c+=sub;}return c;}@SuppressWarnings("unchecked")public static void main(String[] args) {MyScanner sc = new MyScanner();out = new PrintWriter(new BufferedOutputStream(System.out));int n=sc.nextInt();k=sc.nextInt();g=new ArrayList[n];Arrays.setAll(g,x->new ArrayList<Integer>());for(int i=0;i<n-1;++i){int a=sc.nextInt();int b=sc.nextInt();g[a].add(b);g[b].add(a);}dp=new long[2][n][k+1];dfs(0,-1);//System.err.println(Arrays.deepToString(dp));out.println((dp[0][0][k]+dp[1][0][k])%MOD);out.close();}// http://codeforces.com/blog/entry/7018//-----------PrintWriter for faster output---------------------------------public static PrintWriter out;//-----------MyScanner class for faster input----------public static class MyScanner {BufferedReader br;StringTokenizer st;public MyScanner() {br = new BufferedReader(new InputStreamReader(System.in));}String next() {while (st == null || !st.hasMoreElements()) {try {st = new StringTokenizer(br.readLine());} catch (IOException e) {e.printStackTrace();}}return st.nextToken();}int nextInt() {return Integer.parseInt(next());}long nextLong() {return Long.parseLong(next());}double nextDouble() {return Double.parseDouble(next());}String nextLine(){String str = "";try {str = br.readLine();} catch (IOException e) {e.printStackTrace();}return str;}int[] nextIntArray(int n){int[]r=new int[n];for(int i=0;i<n;++i)r[i]=nextInt();return r;}}}