1 条题解

  • 0
    @ 2026-11-2 13:31:48

    二分图完美匹配计数(Matching) 题解

    思路分析

    按题目要求使用对应数据结构与算法模拟/递推求解,注意数据范围边界与时间复杂度。

    参考代码

    #include <iostream>
    #include <vector>
    using namespace std;
    
    const int MOD = 1000000007;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n;
        if (!(cin >> n)) return 0;
        vector<int> adjMask(n, 0);
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                int a;
                cin >> a;
                if (a) adjMask[i] |= (1 << j);
            }
        }
        int tot = 1 << n;
        vector<int> dp(tot, 0);
        dp[0] = 1;
        for (int mask = 0; mask < tot - 1; ++mask) {
            if (!dp[mask]) continue;
            int i = __builtin_popcount(mask);
            int avail = adjMask[i] & (~mask);
            while (avail) {
                int lb = avail & -avail;
                int nmask = mask | lb;
                dp[nmask] += dp[mask];
                if (dp[nmask] >= MOD) dp[nmask] -= MOD;
                avail -= lb;
            }
        }
        cout << dp[tot - 1] << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    8697
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者