結果

問題 No.1103 Directed Length Sum
ユーザー mumumumupomumumumupo
提出日時 2020-07-03 21:54:38
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,402 bytes
コンパイル時間 2,373 ms
コンパイル使用メモリ 78,584 KB
実行使用メモリ 351,512 KB
最終ジャッジ日時 2023-10-17 02:05:13
合計ジャッジ時間 26,476 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
権限があれば一括ダウンロードができます

ソースコード

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