結果

問題 No.1878 union-find の数え上げ
ユーザー 37zigen37zigen
提出日時 2021-12-24 06:15:21
言語 Java21
(openjdk 21)
結果
AC  
実行時間 805 ms / 2,000 ms
コード長 1,825 bytes
コンパイル時間 2,501 ms
コンパイル使用メモリ 87,408 KB
実行使用メモリ 93,892 KB
最終ジャッジ日時 2024-04-14 12:06:14
合計ジャッジ時間 10,086 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 642 ms
93,892 KB
testcase_01 AC 740 ms
68,672 KB
testcase_02 AC 791 ms
68,592 KB
testcase_03 AC 785 ms
67,904 KB
testcase_04 AC 805 ms
67,896 KB
testcase_05 AC 402 ms
61,424 KB
testcase_06 AC 412 ms
61,700 KB
testcase_07 AC 135 ms
54,112 KB
testcase_08 AC 132 ms
54,188 KB
testcase_09 AC 132 ms
54,068 KB
testcase_10 AC 131 ms
54,140 KB
testcase_11 AC 130 ms
54,052 KB
testcase_12 AC 131 ms
54,252 KB
testcase_13 AC 132 ms
54,320 KB
testcase_14 AC 508 ms
62,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

class Main{
	
	final long p = 998244353;
	
	long ans=1;
	
	void dfs(int v, int d, ArrayList<Integer>[] g) {
		if (d > 0) ans = ans * d % p;
		for (int u : g[v]) {
			dfs(u, d + 1, g);
		}
	}
	
	void run() throws Exception {
        Scanner sc=new Scanner(System.in);
        PrintWriter pw=new PrintWriter(System.out);
        int N=sc.nextInt();
        ArrayList<Integer>[] g=new ArrayList[N];
        for (int i=0;i<N;++i) g[i]=new ArrayList<>();
        for (int i=1;i<N;++i) {
        	int p=sc.nextInt()-1;
        	g[p].add(i);
        }
        
        dfs(0, 0, g);
        
        System.out.println(ans);
        pw.close();
	}
	
	void generate_random() throws Exception {
		Random rnd=new Random();
		for (int testcase=1;testcase<=5;++testcase) {
			File file=new File("random_"+testcase);
			PrintWriter pw=new PrintWriter(file);
			int N=100000;
			pw.println(N);
			for (int i=2;i<=N;++i) {
				int p=rnd.nextInt(1, i);
				pw.println(p);
			}			
			pw.close();
		}
	}
	
	void generate_line() throws Exception {
		File file=new File("line");
		PrintWriter pw=new PrintWriter(file);
		Random rnd=new Random();
		int N=100000;
		pw.println(N);
		for (int i=2;i<=N;++i) {
			int p=i-1;
			pw.println(p);
		}
		pw.close();
	}
	
	void generate_star() throws Exception {
		File file=new File("star");
		PrintWriter pw=new PrintWriter(file);
		Random rnd=new Random();
		int N=100000;
		pw.println(N);
		for (int i=2;i<=N;++i) {
			int p=1;
			pw.println(p);
		}
		pw.close();
	}
	
    public static void main(String args[]) throws Exception {
    	new Main().run();
    }
    
    void tr(Object...o) {
    	System.out.println(Arrays.deepToString(o));
    }
}
0