結果

問題 No.196 典型DP (1)
ユーザー tentententen
提出日時 2021-05-20 09:59:54
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,175 bytes
コンパイル時間 2,665 ms
コンパイル使用メモリ 79,880 KB
実行使用メモリ 200,864 KB
最終ジャッジ日時 2024-04-18 13:59:53
合計ジャッジ時間 38,568 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
37,092 KB
testcase_01 AC 52 ms
37,204 KB
testcase_02 AC 52 ms
36,988 KB
testcase_03 AC 52 ms
37,184 KB
testcase_04 AC 52 ms
37,028 KB
testcase_05 AC 53 ms
37,236 KB
testcase_06 AC 52 ms
36,684 KB
testcase_07 AC 53 ms
37,180 KB
testcase_08 AC 52 ms
37,132 KB
testcase_09 AC 52 ms
37,104 KB
testcase_10 AC 53 ms
37,028 KB
testcase_11 AC 56 ms
37,056 KB
testcase_12 AC 65 ms
37,540 KB
testcase_13 AC 78 ms
38,048 KB
testcase_14 AC 85 ms
39,256 KB
testcase_15 AC 176 ms
45,396 KB
testcase_16 AC 427 ms
57,276 KB
testcase_17 AC 653 ms
60,660 KB
testcase_18 AC 895 ms
66,076 KB
testcase_19 AC 979 ms
65,624 KB
testcase_20 AC 1,100 ms
65,500 KB
testcase_21 AC 1,111 ms
65,448 KB
testcase_22 AC 1,126 ms
65,416 KB
testcase_23 AC 1,894 ms
153,188 KB
testcase_24 AC 1,677 ms
126,468 KB
testcase_25 AC 1,603 ms
120,948 KB
testcase_26 TLE -
testcase_27 TLE -
testcase_28 TLE -
testcase_29 TLE -
testcase_30 TLE -
testcase_31 AC 1,058 ms
60,460 KB
testcase_32 AC 1,041 ms
59,928 KB
testcase_33 AC 1,075 ms
63,948 KB
testcase_34 AC 1,057 ms
62,368 KB
testcase_35 AC 1,045 ms
59,656 KB
testcase_36 AC 1,032 ms
62,668 KB
testcase_37 AC 1,120 ms
63,224 KB
testcase_38 AC 1,240 ms
62,624 KB
testcase_39 AC 1,241 ms
63,996 KB
testcase_40 AC 1,234 ms
62,308 KB
testcase_41 AC 53 ms
37,256 KB
testcase_42 AC 53 ms
37,024 KB
testcase_43 AC 54 ms
37,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    static ArrayList<TreeMap<Integer, Long>> dp = new ArrayList<>();
    static final int MOD = 1000000007;
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int k = sc.nextInt();
        for (int i = 0; i < n; i++) {
            dp.add(new TreeMap<>());
            dp.get(i).put(0, 1L);
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < n - 1; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            graph.get(a).add(b);
            graph.get(b).add(a);
        }
        getChildren(0, 0);
        System.out.println(dp.get(0).getOrDefault(k, 0L));
    }
    
    static int getChildren(int idx, int parent) {
        int count = 1;
        for (int x : graph.get(idx)) {
            if (x == parent) {
                continue;
            }
            count += getChildren(x, idx);
            Integer key = Integer.MAX_VALUE;
            while ((key = dp.get(idx).lowerKey(key)) != null) {
                long org = dp.get(idx).get(key);
                for (int y : dp.get(x).keySet()) {
                    if (y == 0) {
                        continue;
                    }
                    dp.get(idx).put(key + y, (dp.get(idx).getOrDefault(key + y, 0L) + org * dp.get(x).get(y) % MOD) % MOD);
                }
            }
        }
        dp.get(idx).put(count, 1L);
        return count;
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0