結果
| 問題 |
No.1103 Directed Length Sum
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-07-03 21:54:38 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,402 bytes |
| コンパイル時間 | 2,667 ms |
| コンパイル使用メモリ | 78,560 KB |
| 実行使用メモリ | 342,472 KB |
| 最終ジャッジ日時 | 2024-09-17 00:39:32 |
| 合計ジャッジ時間 | 30,182 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | WA * 2 |
| other | WA * 22 |
ソースコード
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.*;
public class Main {
static int N;
static ArrayList<HashSet<Integer>> edge;
static long[] dp;
static long[] cnt;
static int mod = 1_000_000_007;
public static void solve(int i) {
if (dp[i] != -1) return;
long val = 0;
long val_cnt = 1;
for (Integer tmp : edge.get(i)) {
if (dp[i] != -1) solve(tmp);
val = (val+dp[tmp]+cnt[tmp])%mod;
val_cnt += cnt[tmp];
}
dp[i] = val;
cnt[i] = val_cnt;
}
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
N = in.nextInt();
edge = new ArrayList<HashSet<Integer>>();
for (int i=0;i<N;i++) {
HashSet<Integer> add = new HashSet<Integer>();
edge.add(add);
}
boolean[] not_root = new boolean[N];
for (int i=0;i<N-1;i++) {
int A = in.nextInt()-1;
int B = in.nextInt()-1;
edge.get(A).add(B);
not_root[B] = true;
}
int root = -1;
for (int i=0;i<N;i++) {
if (!not_root[i]) root = i;
}
dp = new long[N];
cnt = new long[N];
for (int i=0;i<N;i++) {
dp[i] = -1;
}
solve(root);
long ans = 0L;
for (int i=0;i<N;i++) {
ans = (ans+dp[i])%mod;
}
out.println(ans);
out.close();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}