結果

問題 No.1507 Road Blocked
ユーザー tentententen
提出日時 2022-08-17 20:11:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 817 ms / 2,000 ms
コード長 2,116 bytes
コンパイル時間 3,169 ms
コンパイル使用メモリ 79,716 KB
実行使用メモリ 97,768 KB
最終ジャッジ日時 2024-04-15 08:22:22
合計ジャッジ時間 27,941 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
50,272 KB
testcase_01 AC 64 ms
50,000 KB
testcase_02 AC 60 ms
50,316 KB
testcase_03 AC 590 ms
97,768 KB
testcase_04 AC 760 ms
76,824 KB
testcase_05 AC 766 ms
77,068 KB
testcase_06 AC 639 ms
72,524 KB
testcase_07 AC 792 ms
76,704 KB
testcase_08 AC 779 ms
76,752 KB
testcase_09 AC 781 ms
76,876 KB
testcase_10 AC 711 ms
74,016 KB
testcase_11 AC 632 ms
70,704 KB
testcase_12 AC 764 ms
76,744 KB
testcase_13 AC 745 ms
76,744 KB
testcase_14 AC 789 ms
76,636 KB
testcase_15 AC 779 ms
77,132 KB
testcase_16 AC 773 ms
77,000 KB
testcase_17 AC 771 ms
76,836 KB
testcase_18 AC 758 ms
76,444 KB
testcase_19 AC 809 ms
76,704 KB
testcase_20 AC 779 ms
76,876 KB
testcase_21 AC 765 ms
76,744 KB
testcase_22 AC 784 ms
76,928 KB
testcase_23 AC 817 ms
76,796 KB
testcase_24 AC 790 ms
76,568 KB
testcase_25 AC 771 ms
76,800 KB
testcase_26 AC 758 ms
76,616 KB
testcase_27 AC 754 ms
76,888 KB
testcase_28 AC 787 ms
76,832 KB
testcase_29 AC 785 ms
76,796 KB
testcase_30 AC 746 ms
78,788 KB
testcase_31 AC 768 ms
76,832 KB
testcase_32 AC 764 ms
76,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    static long ans = 0;
    static int n;
    static final int MOD = 998244353;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < n - 1; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            graph.get(a).add(b);
            graph.get(b).add(a);
        }
        getCount(0, 0);
        System.out.println(ans * pow((n - 1) * getComb(n) % MOD, MOD - 2) % MOD);
    }
    
    static int getCount(int idx, int p)  {
        int count = 1;
        for (int x : graph.get(idx)) {
            if (x == p) {
                continue;
            }
            int tmp = getCount(x, idx);
            ans += getComb(tmp);
            ans += getComb(n - tmp);
            ans %= MOD;
            count += tmp;
        }
        return count;
    }
    
    static long pow(long x, int p) {
        if (p == 0) {
            return 1;
        } else if (p % 2 == 0) {
            return pow(x * x % MOD, p / 2);
        } else {
            return pow(x, p - 1) * x % MOD;
        }
    }
    
    static long getComb(long x) {
        return x * (x - 1) / 2 % MOD;
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0