結果

問題 No.329 全射
ユーザー GrenacheGrenache
提出日時 2015-12-23 21:51:19
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 3,186 bytes
コンパイル時間 6,099 ms
コンパイル使用メモリ 79,908 KB
実行使用メモリ 413,360 KB
最終ジャッジ日時 2023-10-19 01:48:43
合計ジャッジ時間 10,853 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
70,472 KB
testcase_01 AC 86 ms
63,400 KB
testcase_02 AC 87 ms
63,452 KB
testcase_03 AC 84 ms
63,452 KB
testcase_04 AC 83 ms
63,444 KB
testcase_05 AC 87 ms
63,480 KB
testcase_06 AC 86 ms
63,400 KB
testcase_07 AC 83 ms
63,400 KB
testcase_08 AC 88 ms
63,364 KB
testcase_09 AC 86 ms
63,428 KB
testcase_10 AC 87 ms
63,412 KB
testcase_11 AC 87 ms
63,396 KB
testcase_12 AC 86 ms
63,464 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
import java.util.Queue;


public class Main_yukicoder329 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Printer pr = new Printer(System.out);

        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] w = new int[n];
        for (int i = 0; i < n; i++) {
        	w[i] = sc.nextInt();
        }

        List<Queue<Integer>> edges = new ArrayList<Queue<Integer>>();
        for (int i = 0; i < n; i++) {
        	edges.add(new ArrayDeque<Integer>());
        }

        for (int i = 0; i < m; i++) {
        	int ii = sc.nextInt() - 1;
        	int jj = sc.nextInt() - 1;
        	if (ii != jj) {
        		edges.get(ii).add(jj);
        	}
        }

        long ret = 0;
        for (int s = 0; s < n; s++) {
        	Deque<Integer> q = new ArrayDeque<Integer>();
        	int[] used = new int[n];
        	q.push(s);
        	used[s] = w[s];

        	while (!q.isEmpty()) {
        		int u = q.pop();

        		for (int e : edges.get(u)) {
        			if (used[e] < used[u]) {
        				q.add(e);
        				used[e] = Math.min(used[u], w[e]);
        			}
        		}
        	}

        	for (int i = 0; i < n; i++) {
        		if (used[i] >= w[i]) {
        			ret = (ret + getF(w[s], w[i])) % MOD;
        		}
        	}
        }

        pr.println(ret);

        pr.close();
        sc.close();
    }

    private static final long MOD = 1_000_000_007;
	private static long[][] dp;

    private static long getF(int s, int u) {
    	if (dp == null) {
    		dp = new long[1000 + 1][1000 + 1];
    		dp[0][0] = 1;

    		for (int i = 1; i <= 1000; i++) {
    			for (int j = 1; j <= i; j++) {
    				dp[i][j] = (dp[i - 1][j] + dp[i - 1][j - 1]) % MOD;
    				dp[i][j] = dp[i][j] * j % MOD;
    			}
    		}
    	}

    	return dp[s][u];
	}

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
					it = Arrays.asList(br.readLine().split(" ")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		int nextInt() throws RuntimeException {
			return Integer.parseInt(next());
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0