結果

問題 No.329 全射
ユーザー GrenacheGrenache
提出日時 2015-12-25 19:49:31
言語 Java19
(openjdk 21)
結果
AC  
実行時間 398 ms / 2,000 ms
コード長 3,060 bytes
コンパイル時間 4,019 ms
コンパイル使用メモリ 79,392 KB
実行使用メモリ 70,692 KB
最終ジャッジ日時 2023-10-19 03:45:55
合計ジャッジ時間 12,448 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
63,496 KB
testcase_01 AC 83 ms
63,516 KB
testcase_02 AC 84 ms
63,512 KB
testcase_03 AC 84 ms
63,456 KB
testcase_04 AC 83 ms
63,496 KB
testcase_05 AC 84 ms
63,560 KB
testcase_06 AC 84 ms
63,524 KB
testcase_07 AC 81 ms
63,524 KB
testcase_08 AC 83 ms
63,528 KB
testcase_09 AC 82 ms
63,516 KB
testcase_10 AC 84 ms
63,472 KB
testcase_11 AC 84 ms
63,516 KB
testcase_12 AC 85 ms
63,528 KB
testcase_13 AC 381 ms
70,304 KB
testcase_14 AC 215 ms
67,944 KB
testcase_15 AC 289 ms
70,288 KB
testcase_16 AC 349 ms
70,292 KB
testcase_17 AC 310 ms
70,404 KB
testcase_18 AC 398 ms
70,396 KB
testcase_19 AC 374 ms
69,768 KB
testcase_20 AC 375 ms
70,324 KB
testcase_21 AC 336 ms
66,488 KB
testcase_22 AC 309 ms
70,240 KB
testcase_23 AC 256 ms
70,692 KB
testcase_24 AC 250 ms
68,436 KB
testcase_25 AC 281 ms
70,564 KB
testcase_26 AC 233 ms
67,564 KB
testcase_27 AC 132 ms
64,500 KB
testcase_28 AC 89 ms
63,528 KB
testcase_29 AC 112 ms
64,084 KB
testcase_30 AC 108 ms
63,976 KB
testcase_31 AC 82 ms
63,508 KB
testcase_32 AC 118 ms
64,080 KB
testcase_33 AC 94 ms
63,584 KB
testcase_34 AC 92 ms
64,048 KB
testcase_35 AC 92 ms
63,572 KB
testcase_36 AC 91 ms
63,564 KB
testcase_37 AC 91 ms
64,088 KB
testcase_38 AC 93 ms
63,556 KB
testcase_39 AC 93 ms
63,552 KB
testcase_40 AC 88 ms
63,560 KB
testcase_41 AC 94 ms
63,560 KB
testcase_42 AC 91 ms
64,084 KB
権限があれば一括ダウンロードができます

ソースコード

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.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(jj).add(ii);
        	}
        }

        long ret = 0;
        for (int s = 0; s < n; s++) {
        	Queue<Integer> q = new ArrayDeque<Integer>();
        	boolean[] used = new boolean[n];
        	q.add(s);
        	used[s] = true;

        	while (!q.isEmpty()) {
        		int u = q.remove();
    			ret = (ret + getF(w[u], w[s])) % MOD;

        		for (int e : edges.get(u)) {
        			if (!used[e] && w[e] >= w[s]) {
        				q.add(e);
        				used[e] = true;
        			}
        		}
        	}
        }

        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