Submission #1940837


Source Code Expand

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

public class Main {
    private FastScanner in;
    private PrintWriter out;

    final int mod = (int) 1e9 + 7;

    ArrayList<Integer>[] g;


    long[][] go(int v) {
        long[][] dp = new long[3][1];
        dp[0][0] = 1;
        for (int to : g[v]) {
            long[][] child = go(to);
            int maxK = child[0].length;
            long[][] ndp = new long[3][dp[0].length + maxK];
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < dp[i].length; j++) {
                    long cur = dp[i][j];
                    if (cur == 0) {
                        continue;
                    }
                    for (int i2 = 0; i2 < 2; i2++) {
                        for (int j2 = 0; j2 < child[i2].length; j2++) {
                            long chWays = child[i2][j2];
                            if (chWays == 0) {
                                continue;
                            }
                            int nextI = i + i2;
                            if (nextI >= 3) {
                                continue;
                            }
                            int nextJ = j + j2;
                            ndp[nextI][nextJ] = (int) ((ndp[nextI][nextJ] + cur * chWays) % mod);
                        }
                    }
                }
            }
            dp = ndp;
        }
        long[][] res = new long[2][dp[0].length + 1];
        for (int j = 0; j < dp[0].length; j++) {
            {
                long cur0 = dp[0][j];
                res[0][j] += cur0;
                if (res[0][j] >= mod) {
                    res[0][j] -= mod;
                }
                res[1][j + 1] += cur0;
                if (res[1][j + 1] >= mod) {
                    res[1][j + 1] -= mod;
                }
            }
            {
                res[0][j] += dp[1][j] + dp[1][j];
                while (res[0][j] >= mod) {
                    res[0][j] -= mod;
                }
                res[1][j + 1] += dp[1][j];
                if (res[1][j + 1] >= mod) {
                    res[1][j + 1] -= mod;
                }
            }
            {
                res[0][j] += dp[2][j] + dp[2][j];
                while (res[0][j] >= mod) {
                    res[0][j] -= mod;
                }
            }
        }
        return res;
    }

    private void solve() {
        int n = in.nextInt();
        g = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            g[i] = new ArrayList<>();
        }
        for (int i = 1; i < n; i++) {
            g[in.nextInt() - 1].add(i);
        }
        long[][] go = go(0);
        long[] fact = new long[n + 1];
        fact[0] = 1;
        for (int i = 1; i < fact.length; i++) {
            fact[i] = fact[i - 1] * i % mod;
        }
        long result = 0;
        for (int i = 0; i < go[0].length; i++) {
            if (go[0][i] == 0) {
                continue;
            }
            long ways = go[0][i] * fact[n - i] % mod;
            if (i % 2 == 1) {
                ways = mod - ways;
            }
            result += ways;
        }
        out.println(result % mod);
    }

    private void run() {
        try {
            in = new FastScanner(new File("CF17_Exhibition_A.in"));
            out = new PrintWriter(new File("CF17_Exhibition_A.out"));

            solve();

            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void runIO() {
        in = new FastScanner(System.in);
        out = new PrintWriter(System.out);

        solve();

        out.close();
    }

    private class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        FastScanner(File f) {
            try {
                br = new BufferedReader(new FileReader(f));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        FastScanner(InputStream f) {
            br = new BufferedReader(new InputStreamReader(f));
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return null;
                st = new StringTokenizer(s);
            }
            return st.nextToken();
        }

        boolean hasMoreTokens() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return false;
                st = new StringTokenizer(s);
            }
            return true;
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }
    }

    public static void main(String[] args) {
        new Main().runIO();
    }
}

Submission Info

Submission Time
Task A - Awkward
User qwerty787788
Language Java8 (OpenJDK 1.8.0)
Score 1000
Code Size 5493 Byte
Status AC
Exec Time 387 ms
Memory 90828 KB

Compile Error

Note: ./Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1000 / 1000
Status
AC × 4
AC × 55
Set Name Test Cases
Sample a01, a02, a03, a04
All a01, a02, a03, a04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, b34, b35, b36, b37, b38, b39, b40, b41, b42, b43, b44, b45, b46, b47, b48, b49, b50, b51, b52, b53, b54, b55
Case Name Status Exec Time Memory
a01 AC 72 ms 18900 KB
a02 AC 73 ms 19028 KB
a03 AC 71 ms 19668 KB
a04 AC 74 ms 19668 KB
b05 AC 73 ms 20564 KB
b06 AC 182 ms 55444 KB
b07 AC 363 ms 90112 KB
b08 AC 195 ms 29540 KB
b09 AC 181 ms 23516 KB
b10 AC 172 ms 26036 KB
b11 AC 150 ms 24100 KB
b12 AC 126 ms 29344 KB
b13 AC 148 ms 36264 KB
b14 AC 174 ms 56140 KB
b15 AC 189 ms 22260 KB
b16 AC 128 ms 23236 KB
b17 AC 75 ms 19536 KB
b18 AC 73 ms 20820 KB
b19 AC 193 ms 26092 KB
b20 AC 193 ms 25700 KB
b21 AC 192 ms 25552 KB
b22 AC 191 ms 26204 KB
b23 AC 202 ms 28372 KB
b24 AC 225 ms 25448 KB
b25 AC 211 ms 40012 KB
b26 AC 181 ms 60624 KB
b27 AC 211 ms 44436 KB
b28 AC 195 ms 56824 KB
b29 AC 313 ms 62120 KB
b30 AC 304 ms 58500 KB
b31 AC 259 ms 47636 KB
b32 AC 367 ms 60684 KB
b33 AC 195 ms 23976 KB
b34 AC 244 ms 28620 KB
b35 AC 267 ms 29396 KB
b36 AC 199 ms 23988 KB
b37 AC 217 ms 27972 KB
b38 AC 250 ms 26432 KB
b39 AC 229 ms 24256 KB
b40 AC 238 ms 27588 KB
b41 AC 232 ms 28948 KB
b42 AC 226 ms 27160 KB
b43 AC 233 ms 23524 KB
b44 AC 220 ms 28132 KB
b45 AC 356 ms 80436 KB
b46 AC 387 ms 65208 KB
b47 AC 369 ms 88784 KB
b48 AC 371 ms 87476 KB
b49 AC 372 ms 90828 KB
b50 AC 192 ms 54328 KB
b51 AC 181 ms 61484 KB
b52 AC 177 ms 62044 KB
b53 AC 197 ms 59112 KB
b54 AC 182 ms 56820 KB
b55 AC 260 ms 60636 KB