結果

問題 No.196 典型DP (1)
ユーザー hama_duhama_du
提出日時 2015-05-12 08:56:07
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 5,383 bytes
コンパイル時間 3,788 ms
コンパイル使用メモリ 78,496 KB
実行使用メモリ 95,756 KB
最終ジャッジ日時 2024-07-05 23:58:27
合計ジャッジ時間 13,976 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
42,196 KB
testcase_01 AC 51 ms
36,548 KB
testcase_02 AC 50 ms
36,716 KB
testcase_03 AC 51 ms
37,028 KB
testcase_04 AC 53 ms
37,112 KB
testcase_05 AC 50 ms
36,560 KB
testcase_06 AC 50 ms
37,036 KB
testcase_07 AC 50 ms
36,556 KB
testcase_08 AC 50 ms
37,020 KB
testcase_09 AC 52 ms
36,560 KB
testcase_10 AC 55 ms
37,068 KB
testcase_11 AC 51 ms
37,024 KB
testcase_12 AC 51 ms
36,896 KB
testcase_13 AC 52 ms
37,028 KB
testcase_14 AC 54 ms
36,844 KB
testcase_15 AC 116 ms
40,040 KB
testcase_16 AC 154 ms
40,972 KB
testcase_17 AC 169 ms
40,980 KB
testcase_18 AC 251 ms
41,976 KB
testcase_19 AC 240 ms
42,352 KB
testcase_20 AC 233 ms
42,120 KB
testcase_21 AC 257 ms
42,284 KB
testcase_22 AC 250 ms
41,856 KB
testcase_23 AC 1,591 ms
55,184 KB
testcase_24 AC 750 ms
51,264 KB
testcase_25 AC 927 ms
49,204 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