結果

問題 No.196 典型DP (1)
ユーザー hama_duhama_du
提出日時 2015-05-12 09:37:10
言語 Java21
(openjdk 21)
結果
AC  
実行時間 262 ms / 2,000 ms
コード長 5,505 bytes
コンパイル時間 3,912 ms
コンパイル使用メモリ 78,280 KB
実行使用メモリ 78,100 KB
最終ジャッジ日時 2024-07-06 00:00:09
合計ジャッジ時間 9,836 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
36,720 KB
testcase_01 AC 40 ms
37,084 KB
testcase_02 AC 40 ms
36,912 KB
testcase_03 AC 41 ms
37,188 KB
testcase_04 AC 41 ms
36,720 KB
testcase_05 AC 41 ms
36,472 KB
testcase_06 AC 40 ms
37,020 KB
testcase_07 AC 40 ms
36,960 KB
testcase_08 AC 41 ms
36,960 KB
testcase_09 AC 40 ms
37,084 KB
testcase_10 AC 40 ms
36,892 KB
testcase_11 AC 40 ms
37,020 KB
testcase_12 AC 40 ms
36,940 KB
testcase_13 AC 41 ms
36,724 KB
testcase_14 AC 41 ms
36,580 KB
testcase_15 AC 47 ms
37,268 KB
testcase_16 AC 85 ms
38,780 KB
testcase_17 AC 136 ms
41,972 KB
testcase_18 AC 158 ms
42,332 KB
testcase_19 AC 164 ms
42,596 KB
testcase_20 AC 187 ms
42,416 KB
testcase_21 AC 185 ms
42,728 KB
testcase_22 AC 190 ms
42,440 KB
testcase_23 AC 213 ms
55,412 KB
testcase_24 AC 218 ms
51,468 KB
testcase_25 AC 219 ms
49,364 KB
testcase_26 AC 262 ms
77,548 KB
testcase_27 AC 259 ms
77,912 KB
testcase_28 AC 241 ms
77,848 KB
testcase_29 AC 257 ms
78,100 KB
testcase_30 AC 233 ms
77,752 KB
testcase_31 AC 194 ms
41,972 KB
testcase_32 AC 199 ms
42,188 KB
testcase_33 AC 189 ms
42,016 KB
testcase_34 AC 193 ms
42,176 KB
testcase_35 AC 193 ms
42,000 KB
testcase_36 AC 189 ms
41,832 KB
testcase_37 AC 195 ms
42,440 KB
testcase_38 AC 195 ms
41,896 KB
testcase_39 AC 188 ms
41,832 KB
testcase_40 AC 191 ms
41,892 KB
testcase_41 AC 42 ms
36,932 KB
testcase_42 AC 42 ms
37,084 KB
testcase_43 AC 41 ms
36,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

/**
 * Created by dhamada on 15/05/12.
 */
public class P196 {

    static final long MOD = 1_000_000_007;
    static long __cur = 0;

    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int n = in.nextInt();
        int k = in.nextInt();
        graph = buildGraph(in, n, n-1);


        children = new int[n];
        dfs0(0, -1);

        long[] ans = dfs(0, -1);

        out.println(ans[k]);
        out.flush();
    }

    static int dfs0(int now, int parent) {
        children[now] = 1;
        for (int to : graph[now]) {
            if (to != parent) {
                children[now] += dfs0(to, now);
            }
        }
        return children[now];
    }

    static long[] dfs(int now, int parent) {
        int nc = children[now];
        long[][] ret = new long[2][nc+1];

        ret[0][0] = 1;

        int idx = 0;
        for (int to : graph[now]) {
            if (to != parent) {
                int f = idx % 2;
                int t = 1 - f;
                Arrays.fill(ret[t], 0);
                idx++;
                long[] tbl = dfs(to, now);
                for (int j = 0 ; j < nc ; j++) {
                    if (ret[f][j] != 0) {
                        for (int i = 0 ; i < tbl.length ; i++) {
                            if (tbl[i] != 0) {
                                ret[t][i+j] += (ret[f][j] * tbl[i]) % MOD;
                                if (ret[t][i+j] >= MOD) {
                                    ret[t][i+j] -= MOD;
                                }
                            }
                        }

                    }
                }
            }
        }

        ret[idx%2][nc] = 1;
        return ret[idx%2];
    }

    static int[][] graph;
    static int[] children;

    static int[][] buildGraph(InputReader in, int n, int m) {
        int[][] edges = new int[m][];
        int[][] graph = new int[n][];
        int[] deg = new int[n];
        for (int i = 0 ; i < m ; i++) {
            int a = in.nextInt();
            int b = in.nextInt();
            deg[a]++;
            deg[b]++;
            edges[i] = new int[]{a, b};
        }
        for (int i = 0 ; i < n ; i++) {
            graph[i] = new int[deg[i]];
        }
        for (int i = 0 ; i < m ; i++) {
            int a = edges[i][0];
            int b = edges[i][1];
            graph[a][--deg[a]] = b;
            graph[b][--deg[b]] = a;
        }
        return graph;
    }


    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        private int next() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public char nextChar() {
            int c = next();
            while (isSpaceChar(c))
                c = next();
            if ('a' <= c && c <= 'z') {
                return (char)c;
            }
            if ('A' <= c && c <= 'Z') {
                return (char)c;
            }
            throw new InputMismatchException();
        }

        public String nextToken() {
            int c = next();
            while (isSpaceChar(c))
                c = next();
            StringBuilder res = new StringBuilder();
            do {
                res.append((char)c);
                c = next();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public int nextInt() {
            int c = next();
            while (isSpaceChar(c))
                c = next();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = next();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = next();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long nextLong() {
            int c = next();
            while (isSpaceChar(c))
                c = next();
            long sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = next();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = next();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
        }
    }
}
0