結果
| 問題 | No.1103 Directed Length Sum |
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2021-02-05 07:43:44 |
| 言語 | Java (openjdk 25.0.1) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,157 bytes |
| 記録 | |
| コンパイル時間 | 3,321 ms |
| コンパイル使用メモリ | 78,660 KB |
| 実行使用メモリ | 315,068 KB |
| 最終ジャッジ日時 | 2024-07-01 20:28:16 |
| 合計ジャッジ時間 | 8,477 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | TLE * 1 -- * 21 |
ソースコード
import java.util.*;
import java.io.*;
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) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
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++) {
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);
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