結果

問題 No.329 全射
ユーザー Pachicobue
提出日時 2017-11-07 21:50:41
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 2,182 bytes
コンパイル時間 1,828 ms
コンパイル使用メモリ 182,088 KB
実行使用メモリ 11,392 KB
最終ジャッジ日時 2024-11-24 04:52:58
合計ジャッジ時間 3,807 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

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