結果

問題 No.1103 Directed Length Sum
ユーザー tentententen
提出日時 2020-08-18 08:36:08
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,087 bytes
コンパイル時間 1,971 ms
コンパイル使用メモリ 79,568 KB
実行使用メモリ 179,416 KB
最終ジャッジ日時 2024-04-20 03:15:37
合計ジャッジ時間 19,700 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 104 ms
41,180 KB
testcase_01 AC 97 ms
40,396 KB
testcase_02 AC 2,827 ms
166,848 KB
testcase_03 AC 2,611 ms
147,996 KB
testcase_04 AC 2,378 ms
128,084 KB
testcase_05 TLE -
testcase_06 AC 1,701 ms
103,100 KB
testcase_07 AC 712 ms
60,420 KB
testcase_08 AC 925 ms
71,020 KB
testcase_09 AC 576 ms
53,912 KB
testcase_10 AC 1,231 ms
74,692 KB
testcase_11 AC 2,674 ms
124,148 KB
testcase_12 AC 1,664 ms
102,980 KB
testcase_13 AC 1,144 ms
74,932 KB
testcase_14 AC 528 ms
51,440 KB
testcase_15 AC 1,378 ms
87,872 KB
testcase_16 AC 2,754 ms
136,020 KB
testcase_17 AC 2,787 ms
140,120 KB
testcase_18 AC 1,158 ms
85,920 KB
testcase_19 AC 2,429 ms
132,764 KB
testcase_20 AC 623 ms
67,900 KB
testcase_21 AC 894 ms
80,508 KB
testcase_22 AC 1,990 ms
126,388 KB
testcase_23 AC 1,445 ms
98,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static final int MOD = 1000000007;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        HashMap<Integer, Integer> graph = new HashMap<>();
        for (int i = 0; i < n - 1; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            graph.put(b, a);
        }
        int[] depth = new int[n];
        Arrays.fill(depth, -1);
        for (int i = 0; i < n; i++) {
            getDepth(i, graph, depth);
        }
        long ans = 0;
        for (int x : depth) {
            ans += (long)x * (x + 1) / 2 % MOD;
            ans %= MOD;
        }
        System.out.println(ans);
    }
    
    static int getDepth(int idx, HashMap<Integer, Integer> graph, int[] depth) {
        if (depth[idx] == -1) {
            if (graph.containsKey(idx))  {
                depth[idx] = getDepth(graph.get(idx), graph, depth) + 1;
            } else {
                depth[idx] = 0;
            }
        }
        return depth[idx];
    }
}
0