結果

問題 No.1103 Directed Length Sum
ユーザー tenten
提出日時 2021-03-15 21:06:43
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 1,165 bytes
コンパイル時間 2,781 ms
コンパイル使用メモリ 78,840 KB
実行使用メモリ 316,144 KB
最終ジャッジ日時 2024-11-07 09:55:35
合計ジャッジ時間 10,720 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other TLE * 1 -- * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    static final int MOD = 1000000007;
    static long ans = 0;
	public static void main (String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		TreeSet<Integer> used = new TreeSet<>();
		for (int i = 0; i < n; i++) {
		    graph.add(new ArrayList<>());
		    used.add(i);
		}
		int[] ins = new int[n - 1];
		int[] outs = new int[n - 1];
		for (int i = 0; i < n - 1; i++) {
		    String[] line = br.readLine().split(" ", 2);
		    ins[i] = Integer.parseInt(line[0]) - 1;
		    outs[i] = Integer.parseInt(line[1]) - 1;
		    graph.get(ins[i]).add(outs[i]);
		    used.remove(outs[i]);
		}
		calcAnswer(used.first(), 1);
		System.out.println(ans);
   }
   
   static int calcAnswer(int idx, long depth) {
       int count = 1;
       for (int x : graph.get(idx)) {
           int child = calcAnswer(x, depth + 1);
           ans += depth * child % MOD;
           ans %= MOD;
           count += child;
       }
       return count;
   }
}
0