結果

問題 No.329 全射
ユーザー PachicobuePachicobue
提出日時 2017-11-07 21:50:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 2,182 bytes
コンパイル時間 1,651 ms
コンパイル使用メモリ 181,032 KB
実行使用メモリ 11,244 KB
最終ジャッジ日時 2023-08-15 20:19:28
合計ジャッジ時間 3,878 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 21 ms
10,972 KB
testcase_14 AC 9 ms
10,988 KB
testcase_15 AC 14 ms
11,124 KB
testcase_16 AC 17 ms
10,956 KB
testcase_17 AC 15 ms
10,916 KB
testcase_18 AC 21 ms
11,244 KB
testcase_19 AC 19 ms
10,964 KB
testcase_20 AC 20 ms
11,028 KB
testcase_21 AC 17 ms
11,040 KB
testcase_22 AC 15 ms
11,164 KB
testcase_23 AC 11 ms
10,596 KB
testcase_24 AC 10 ms
10,384 KB
testcase_25 AC 12 ms
10,372 KB
testcase_26 AC 9 ms
10,372 KB
testcase_27 AC 6 ms
10,424 KB
testcase_28 AC 5 ms
8,592 KB
testcase_29 AC 6 ms
10,424 KB
testcase_30 AC 5 ms
8,580 KB
testcase_31 AC 3 ms
4,568 KB
testcase_32 AC 6 ms
9,440 KB
testcase_33 AC 7 ms
10,500 KB
testcase_34 AC 7 ms
10,500 KB
testcase_35 AC 7 ms
10,496 KB
testcase_36 AC 6 ms
10,384 KB
testcase_37 AC 7 ms
10,496 KB
testcase_38 AC 6 ms
10,376 KB
testcase_39 AC 6 ms
10,440 KB
testcase_40 AC 6 ms
10,432 KB
testcase_41 AC 6 ms
10,384 KB
testcase_42 AC 9 ms
10,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz=" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = 1e9 + 7;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;

struct Graph {
    Graph(const int n)
    {
        edge.resize(n);
    }
    void addEdge(const int from, const int to)
    {
        edge[from].push_back(to);
    }
    vector<vector<int>> edge;
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M;
    cin >> N >> M;
    vector<int> w(N);
    for (int i = 0; i < N; i++) {
        cin >> w[i];
    }
    const int W = *max_element(w.begin(), w.end());
    Graph g(N);
    for (int i = 0; i < M; i++) {
        int to, from;
        cin >> to >> from;
        to--, from--;
        g.addEdge(from, to);
    }

    vector<vector<ll>> dp(W, vector<ll>(W, 0));
    dp[0][0] = 1;
    for (int i = 1; i < W; i++) {
        dp[i][0] = 1;
        for (int j = 1; j <= i; j++) {
            dp[i][j] = ((j + 1) * ((dp[i - 1][j] + dp[i - 1][j - 1]) % MOD)) % MOD;
        }
    }

    ll sum = 0;
    for (int i = 0; i < N; i++) {
        const int weight = w[i];
        vector<bool> used(N, false);
        queue<int> q;
        q.push(i);
        used[i] = true;
        sum += dp[w[i] - 1][w[i] - 1];
        sum %= MOD;
        while (not q.empty()) {
            const int s = q.front();
            q.pop();
            for (const int to : g.edge[s]) {
                if ((not used[to]) and w[to] >= weight) {
                    used[to] = true;
                    q.push(to);
                    sum += dp[w[to] - 1][w[i] - 1];
                    sum %= MOD;
                }
            }
        }
    }
    cout << sum << endl;

    return 0;
}
0