結果
| 問題 |
No.329 全射
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-12-25 19:49:31 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 405 ms / 2,000 ms |
| コード長 | 3,060 bytes |
| コンパイル時間 | 4,254 ms |
| コンパイル使用メモリ | 79,428 KB |
| 実行使用メモリ | 57,288 KB |
| 最終ジャッジ日時 | 2024-09-18 23:59:13 |
| 合計ジャッジ時間 | 13,005 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 40 |
ソースコード
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);
}
}
}