結果

問題 No.1103 Directed Length Sum
ユーザー mumumumupomumumumupo
提出日時 2020-07-03 21:55:17
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,402 bytes
コンパイル時間 2,480 ms
コンパイル使用メモリ 78,732 KB
実行使用メモリ 463,160 KB
最終ジャッジ日時 2023-10-17 02:05:53
合計ジャッジ時間 8,300 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
53,628 KB
testcase_01 AC 55 ms
53,636 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.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());
        }

    }
}

0