結果

問題 No.1103 Directed Length Sum
ユーザー tentententen
提出日時 2020-08-18 08:36:08
言語 Java19
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,087 bytes
コンパイル時間 3,067 ms
コンパイル使用メモリ 76,072 KB
実行使用メモリ 174,484 KB
最終ジャッジ日時 2023-08-02 02:56:13
合計ジャッジ時間 22,010 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
56,092 KB
testcase_01 AC 117 ms
55,924 KB
testcase_02 AC 2,861 ms
174,424 KB
testcase_03 AC 2,413 ms
155,256 KB
testcase_04 AC 2,339 ms
124,504 KB
testcase_05 TLE -
testcase_06 AC 1,649 ms
98,984 KB
testcase_07 AC 739 ms
71,256 KB
testcase_08 AC 926 ms
76,120 KB
testcase_09 AC 586 ms
68,744 KB
testcase_10 AC 1,091 ms
80,948 KB
testcase_11 AC 2,614 ms
128,408 KB
testcase_12 AC 1,655 ms
99,120 KB
testcase_13 AC 1,116 ms
79,804 KB
testcase_14 AC 513 ms
62,780 KB
testcase_15 AC 1,435 ms
94,776 KB
testcase_16 AC 2,789 ms
143,876 KB
testcase_17 AC 2,880 ms
145,256 KB
testcase_18 AC 1,100 ms
80,392 KB
testcase_19 AC 2,671 ms
129,084 KB
testcase_20 AC 646 ms
69,368 KB
testcase_21 AC 911 ms
75,116 KB
testcase_22 AC 2,128 ms
122,624 KB
testcase_23 AC 1,451 ms
94,976 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