結果

問題 No.1103 Directed Length Sum
ユーザー tentententen
提出日時 2020-08-18 08:55:08
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,323 bytes
コンパイル時間 2,363 ms
コンパイル使用メモリ 79,732 KB
実行使用メモリ 232,432 KB
最終ジャッジ日時 2024-04-20 04:48:58
合計ジャッジ時間 9,062 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
61,048 KB
testcase_01 AC 133 ms
54,372 KB
testcase_02 TLE -
testcase_03 AC 2,820 ms
158,120 KB
testcase_04 TLE -
testcase_05 TLE -
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 final int MOD = 1000000007;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        boolean[] used = new boolean[n];
        for (int i = 0; i < n - 1; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            used[b] = true;
            graph.get(a).add(b);
        }
        ArrayDeque<Path> deq = new ArrayDeque<>();
        for (int i = 0; i < n; i++) {
            if (!used[i]) {
                deq.add(new Path(i, 0));
                break;
            }
        }
        long ans = 0;
        while (deq.size() > 0) {
            Path p = deq.poll();
            ans += (long)p.value * (p.value + 1) / 2 % MOD;
            ans %= MOD;
            for (int x : graph.get(p.idx)) {
                deq.add(new Path(x, p.value + 1));
            }
        }
        System.out.println(ans);
    }
    
    static class Path {
        int idx;
        int value;
        
        public Path(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
        
    }
}
0