結果
| 問題 | No.1103 Directed Length Sum | 
| コンテスト | |
| ユーザー |  tenten | 
| 提出日時 | 2020-08-18 08:36:08 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                TLE
                                 
                            (最新) 
                                AC
                                 
                            (最初) | 
| 実行時間 | - | 
| コード長 | 1,087 bytes | 
| コンパイル時間 | 2,638 ms | 
| コンパイル使用メモリ | 79,288 KB | 
| 実行使用メモリ | 171,096 KB | 
| 最終ジャッジ日時 | 2024-10-11 22:10:03 | 
| 合計ジャッジ時間 | 19,595 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 2 TLE * 2 -- * 18 | 
ソースコード
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];
    }
}
            
            
            
        