結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 47 ms
50,100 KB
testcase_02 AC 48 ms
50,036 KB
testcase_03 AC 49 ms
50,140 KB
testcase_04 AC 48 ms
50,080 KB
testcase_05 AC 47 ms
48,276 KB
testcase_06 AC 46 ms
50,276 KB
testcase_07 AC 48 ms
50,068 KB
testcase_08 RE -
testcase_09 AC 48 ms
50,408 KB
testcase_10 AC 48 ms
50,164 KB
testcase_11 RE -
testcase_12 AC 50 ms
50,556 KB
testcase_13 AC 50 ms
50,160 KB
testcase_14 AC 52 ms
50,380 KB
testcase_15 AC 98 ms
55,240 KB
testcase_16 AC 133 ms
62,516 KB
testcase_17 AC 181 ms
73,364 KB
testcase_18 AC 271 ms
91,304 KB
testcase_19 AC 239 ms
81,288 KB
testcase_20 AC 139 ms
58,844 KB
testcase_21 AC 204 ms
64,380 KB
testcase_22 AC 121 ms
55,924 KB
testcase_23 AC 268 ms
79,996 KB
testcase_24 AC 195 ms
63,060 KB
testcase_25 AC 395 ms
117,844 KB
testcase_26 RE -
testcase_27 AC 249 ms
73,272 KB
testcase_28 AC 301 ms
117,556 KB
testcase_29 AC 362 ms
165,376 KB
testcase_30 AC 359 ms
187,972 KB
testcase_31 RE -
testcase_32 AC 387 ms
89,948 KB
testcase_33 AC 406 ms
110,460 KB
testcase_34 AC 357 ms
151,120 KB
testcase_35 AC 375 ms
163,836 KB
testcase_36 RE -
testcase_37 AC 290 ms
89,836 KB
testcase_38 AC 413 ms
110,800 KB
testcase_39 AC 424 ms
116,276 KB
testcase_40 AC 369 ms
164,608 KB
testcase_41 AC 49 ms
50,112 KB
testcase_42 AC 49 ms
50,116 KB
testcase_43 AC 48 ms
48,072 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;
        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