結果
| 問題 | No.1103 Directed Length Sum |
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2020-12-28 18:01:18 |
| 言語 | Java (openjdk 25.0.1) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,294 bytes |
| 記録 | |
| コンパイル時間 | 2,546 ms |
| コンパイル使用メモリ | 78,236 KB |
| 実行使用メモリ | 255,568 KB |
| 最終ジャッジ日時 | 2024-10-03 08:43:39 |
| 合計ジャッジ時間 | 8,268 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / 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 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());
int[] counts = new int[n];
for (int i = 0; i < n; i++) {
graph.add(new ArrayList<>());
}
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);
counts[b]++;
}
int root = 0;
for (int i = 0; i < n; i++) {
if (counts[i] == 0) {
root = i;
break;
}
}
getChildren(root, 1);
System.out.println(ans);
}
static int getChildren(int idx, int d) {
int c = 1;
for (int x : graph.get(idx)) {
int tmp = getChildren(x, d + 1);
ans += (long)d * tmp % MOD;
ans %= MOD;
c += tmp;
}
return c;
}
}
tenten