結果

問題 No.1103 Directed Length Sum
ユーザー tentententen
提出日時 2021-02-05 07:40:07
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 983 bytes
コンパイル時間 2,158 ms
コンパイル使用メモリ 74,952 KB
実行使用メモリ 329,012 KB
最終ジャッジ日時 2023-09-14 13:00:27
合計ジャッジ時間 10,418 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
56,032 KB
testcase_01 AC 125 ms
56,140 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    static long ans = 0;
    static final int MOD = 1000000007;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        TreeSet<Integer> unused =  new TreeSet<>();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
            unused.add(i);
        }
        for (int i = 0; i < n - 1; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            graph.get(a).add(b);
            unused.remove(b);
        }
        calc(unused.first(), 0);
        System.out.println(ans);
    }
    
    static int calc(int idx, long depth) {
        int children = 1;
        for (int x : graph.get(idx)) {
            children += calc(x, depth + 1);
        }
        ans += children * depth % MOD;
        ans %= MOD;
        return children;
    }
}
0