結果

問題 No.1103 Directed Length Sum
ユーザー tentententen
提出日時 2021-03-15 21:08:12
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,084 bytes
コンパイル時間 2,378 ms
コンパイル使用メモリ 78,724 KB
実行使用メモリ 303,056 KB
最終ジャッジ日時 2024-04-25 00:31:50
合計ジャッジ時間 8,461 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
41,748 KB
testcase_01 AC 49 ms
36,932 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
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.*;
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);
		}
		for (int i = 0; i < n - 1; i++) {
		    String[] line = br.readLine().split(" ", 2);
		    int a = Integer.parseInt(line[0]) - 1;
		    int b = Integer.parseInt(line[1]) - 1;
		    graph.get(a).add(b);
		    used.remove(b);
		}
		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