結果
| 問題 |
No.1103 Directed Length Sum
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2021-02-05 07:40:07 |
| 言語 | Java (openjdk 23) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 983 bytes |
| コンパイル時間 | 2,398 ms |
| コンパイル使用メモリ | 80,000 KB |
| 実行使用メモリ | 323,832 KB |
| 最終ジャッジ日時 | 2024-07-01 20:25:19 |
| 合計ジャッジ時間 | 9,538 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | TLE * 1 -- * 21 |
ソースコード
import java.util.*;
public class Main {
static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
static long ans = 0;
static final int MOD = 1000000007;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
TreeSet<Integer> unused = new TreeSet<>();
for (int i = 0; i < n; i++) {
graph.add(new ArrayList<>());
unused.add(i);
}
for (int i = 0; i < n - 1; i++) {
int a = sc.nextInt() - 1;
int b = sc.nextInt() - 1;
graph.get(a).add(b);
unused.remove(b);
}
calc(unused.first(), 0);
System.out.println(ans);
}
static int calc(int idx, long depth) {
int children = 1;
for (int x : graph.get(idx)) {
children += calc(x, depth + 1);
}
ans += children * depth % MOD;
ans %= MOD;
return children;
}
}
tenten