結果

問題 No.196 典型DP (1)
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2018-01-22 11:44:14
言語 Java21
(openjdk 21)
結果
AC  
実行時間 538 ms / 2,000 ms
コード長 2,918 bytes
コンパイル時間 3,196 ms
コンパイル使用メモリ 79,124 KB
実行使用メモリ 186,888 KB
最終ジャッジ日時 2023-08-26 20:19:54
合計ジャッジ時間 12,441 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
50,000 KB
testcase_01 AC 51 ms
49,948 KB
testcase_02 AC 50 ms
50,284 KB
testcase_03 AC 50 ms
50,084 KB
testcase_04 AC 49 ms
50,184 KB
testcase_05 AC 50 ms
50,048 KB
testcase_06 AC 48 ms
50,020 KB
testcase_07 AC 48 ms
50,196 KB
testcase_08 AC 48 ms
50,708 KB
testcase_09 AC 47 ms
50,092 KB
testcase_10 AC 50 ms
50,144 KB
testcase_11 AC 49 ms
50,088 KB
testcase_12 AC 52 ms
50,028 KB
testcase_13 AC 53 ms
50,348 KB
testcase_14 AC 54 ms
50,692 KB
testcase_15 AC 104 ms
55,116 KB
testcase_16 AC 141 ms
63,296 KB
testcase_17 AC 184 ms
73,088 KB
testcase_18 AC 267 ms
91,464 KB
testcase_19 AC 233 ms
79,180 KB
testcase_20 AC 136 ms
59,208 KB
testcase_21 AC 172 ms
63,920 KB
testcase_22 AC 120 ms
55,472 KB
testcase_23 AC 267 ms
78,860 KB
testcase_24 AC 203 ms
62,580 KB
testcase_25 AC 418 ms
117,796 KB
testcase_26 AC 99 ms
53,584 KB
testcase_27 AC 235 ms
73,196 KB
testcase_28 AC 311 ms
117,224 KB
testcase_29 AC 358 ms
164,404 KB
testcase_30 AC 538 ms
186,888 KB
testcase_31 AC 105 ms
52,996 KB
testcase_32 AC 399 ms
89,408 KB
testcase_33 AC 398 ms
109,788 KB
testcase_34 AC 367 ms
147,708 KB
testcase_35 AC 348 ms
162,292 KB
testcase_36 AC 106 ms
52,652 KB
testcase_37 AC 290 ms
89,668 KB
testcase_38 AC 437 ms
110,572 KB
testcase_39 AC 432 ms
116,496 KB
testcase_40 AC 395 ms
163,636 KB
testcase_41 AC 48 ms
50,120 KB
testcase_42 AC 47 ms
50,088 KB
testcase_43 AC 48 ms
50,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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