結果
| 問題 | No.1103 Directed Length Sum | 
| コンテスト | |
| ユーザー |  tenten | 
| 提出日時 | 2020-12-28 17:52:20 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                TLE
                                 
                             | 
| 実行時間 | - | 
| コード長 | 1,450 bytes | 
| コンパイル時間 | 1,873 ms | 
| コンパイル使用メモリ | 78,988 KB | 
| 実行使用メモリ | 318,040 KB | 
| 最終ジャッジ日時 | 2024-10-03 08:22:42 | 
| 合計ジャッジ時間 | 7,975 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | TLE * 1 -- * 21 | 
ソースコード
import java.util.*;
public class Main {
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    static int[] children;
    static int[] depth;
    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];
        depth = new int[n];
        int[] counts = new int[n];
        for (int i = 0; i < n; i++) {
            graph.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]);
            counts[bArr[i]]++;
        }
        int root = 0;
        for (int i = 0; i < n; i++) {
            if (counts[i] == 0) {
                root = i;
                break;
            }
        }
        getChildren(root, 1);
        long ans = 0;
        for (int i = 0; i < n - 1; i++) {
            ans += (long)(depth[aArr[i]]) * children[bArr[i]] % MOD;
            ans %= MOD;
        }
        System.out.println(ans);
    }
    
    static int getChildren(int idx, int d) {
        children[idx] = 1;
        depth[idx] = d;
        for (int x : graph.get(idx)) {
            children[idx] += getChildren(x, d + 1);
        }
        return children[idx];
    }
    
}
            
            
            
        