結果

問題 No.196 典型DP (1)
ユーザー hama_duhama_du
提出日時 2015-05-12 09:37:10
言語 Java21
(openjdk 21)
結果
AC  
実行時間 291 ms / 2,000 ms
コード長 5,505 bytes
コンパイル時間 5,210 ms
コンパイル使用メモリ 75,036 KB
実行使用メモリ 88,872 KB
最終ジャッジ日時 2023-09-20 03:42:03
合計ジャッジ時間 13,622 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,220 KB
testcase_01 AC 44 ms
49,332 KB
testcase_02 AC 44 ms
49,236 KB
testcase_03 AC 44 ms
49,228 KB
testcase_04 AC 44 ms
47,252 KB
testcase_05 AC 43 ms
49,060 KB
testcase_06 AC 44 ms
49,132 KB
testcase_07 AC 44 ms
49,172 KB
testcase_08 AC 44 ms
49,204 KB
testcase_09 AC 44 ms
49,024 KB
testcase_10 AC 44 ms
49,236 KB
testcase_11 AC 45 ms
49,176 KB
testcase_12 AC 44 ms
49,352 KB
testcase_13 AC 45 ms
49,216 KB
testcase_14 AC 45 ms
49,032 KB
testcase_15 AC 69 ms
51,988 KB
testcase_16 AC 100 ms
52,996 KB
testcase_17 AC 156 ms
55,004 KB
testcase_18 AC 169 ms
54,992 KB
testcase_19 AC 162 ms
54,684 KB
testcase_20 AC 207 ms
55,500 KB
testcase_21 AC 195 ms
54,768 KB
testcase_22 AC 206 ms
54,704 KB
testcase_23 AC 223 ms
64,796 KB
testcase_24 AC 221 ms
61,228 KB
testcase_25 AC 217 ms
59,040 KB
testcase_26 AC 280 ms
88,496 KB
testcase_27 AC 267 ms
88,120 KB
testcase_28 AC 259 ms
88,364 KB
testcase_29 AC 291 ms
88,548 KB
testcase_30 AC 278 ms
88,872 KB
testcase_31 AC 214 ms
54,868 KB
testcase_32 AC 212 ms
55,332 KB
testcase_33 AC 208 ms
55,472 KB
testcase_34 AC 211 ms
55,156 KB
testcase_35 AC 209 ms
55,452 KB
testcase_36 AC 207 ms
54,868 KB
testcase_37 AC 184 ms
55,152 KB
testcase_38 AC 214 ms
55,060 KB
testcase_39 AC 198 ms
54,612 KB
testcase_40 AC 210 ms
55,072 KB
testcase_41 AC 44 ms
49,276 KB
testcase_42 AC 43 ms
49,200 KB
testcase_43 AC 43 ms
49,248 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