結果

問題 No.1103 Directed Length Sum
ユーザー tentententen
提出日時 2020-12-28 17:44:39
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,734 bytes
コンパイル時間 3,664 ms
コンパイル使用メモリ 88,304 KB
実行使用メモリ 422,356 KB
最終ジャッジ日時 2024-04-14 11:20:08
合計ジャッジ時間 10,966 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
61,008 KB
testcase_01 AC 131 ms
54,180 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 ArrayList<ArrayList<Integer>> rgraph = new ArrayList<>();
    static int[] children;
    static int[] rchildren;
    static final int MOD = 1000000007;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] aArr = new int[n - 1];
        int[] bArr = new int[n - 1];
        children = new int[n];
        rchildren = new int[n];
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
            rgraph.add(new ArrayList<>());
        }
        for (int i = 0; i < n - 1; i++) {
            aArr[i] = sc.nextInt() - 1;
            bArr[i] = sc.nextInt() - 1;
            graph.get(aArr[i]).add(bArr[i]);
            rgraph.get(bArr[i]).add(aArr[i]);
        }
        for (int i = 0; i < n; i++) {
            if (children[i] == 0) {
                getChildren(i);
            }
            if (rchildren[i] == 0) {
                getRChildren(i);
            }
        }
        long ans = 0;
        for (int i = 0; i < n - 1; i++) {
            ans += (long)(rchildren[aArr[i]]) * children[bArr[i]] % MOD;
            ans %= MOD;
        }
        System.out.println(ans);
    }
    
    static int getChildren(int idx) {
        children[idx] = 1;
        for (int x : graph.get(idx)) {
            children[idx] += getChildren(x);
        }
        return children[idx];
    }
    
    static int getRChildren(int idx) {
        rchildren[idx] = 1;
        for (int x : rgraph.get(idx)) {
            rchildren[idx] += getRChildren(x);
        }
        return rchildren[idx];
    }
}
0