結果

問題 No.196 典型DP (1)
ユーザー hama_duhama_du
提出日時 2015-05-12 08:56:07
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 5,383 bytes
コンパイル時間 4,702 ms
コンパイル使用メモリ 74,816 KB
実行使用メモリ 92,448 KB
最終ジャッジ日時 2023-09-20 03:40:16
合計ジャッジ時間 14,470 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
53,576 KB
testcase_01 AC 45 ms
49,160 KB
testcase_02 AC 43 ms
47,548 KB
testcase_03 AC 45 ms
49,344 KB
testcase_04 AC 45 ms
49,420 KB
testcase_05 AC 44 ms
49,132 KB
testcase_06 AC 45 ms
49,528 KB
testcase_07 AC 45 ms
49,340 KB
testcase_08 AC 45 ms
49,256 KB
testcase_09 AC 45 ms
49,036 KB
testcase_10 AC 43 ms
49,584 KB
testcase_11 AC 45 ms
49,064 KB
testcase_12 AC 45 ms
49,160 KB
testcase_13 AC 45 ms
49,544 KB
testcase_14 AC 48 ms
49,292 KB
testcase_15 AC 117 ms
53,432 KB
testcase_16 AC 104 ms
52,744 KB
testcase_17 AC 151 ms
54,212 KB
testcase_18 AC 215 ms
55,184 KB
testcase_19 AC 240 ms
55,204 KB
testcase_20 AC 204 ms
55,200 KB
testcase_21 AC 240 ms
55,264 KB
testcase_22 AC 248 ms
55,272 KB
testcase_23 AC 1,207 ms
64,776 KB
testcase_24 AC 753 ms
62,044 KB
testcase_25 AC 823 ms
59,656 KB
testcase_26 TLE -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

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;

    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 i = 0 ; i < tbl.length ; i++) {
                    if (tbl[i] != 0) {
                        for (int j = 0 ; j < nc ; j++) {
                            if (ret[f][j] != 0) {
                                ret[t][i+j] += (ret[f][j] * tbl[i]) % 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