【题解】【THUWC2017】随机二分图(动态规划)概率DP+状压

题面题解 yyb把后两种边拆分,因为只考虑边对完美匹配的方案的贡献,即该边出现且被使用才贡献(出现但是没有被使用视为没有出现)这个状压需要记左右点的匹配状态,因为存在强制选四个点的边。并且为了去重,每次只能枚举左边最小存在点的出边,合法状态不多,直接记忆化如果是一般的完美匹配,直接记f(i,S) : 考虑到左边第i个点,挨着匹配即可注意如果一对边有公共顶点,不用加/减额外的贡献,因为不...
摘要由CSDN通过智能技术生成

题面

题解 yyb

把后两种边拆分,
因为只考虑边对完美匹配的方案的贡献,即该边出现且被使用才贡献(出现但是没有被使用视为没有出现)
这个状压需要记左右点的匹配状态,因为存在强制选四个点的边。并且为了去重,每次只能枚举左边最小存在点的出边,合法状态不多,直接记忆化
如果是一般的完美匹配,直接记f(i,S) : 考虑到左边第i个点,挨着匹配即可
注意如果一对边有公共顶点,不用加/减额外的贡献,因为不会同时计入匹配

要手写hashmap啊!
pbds也不如手写方便,并且还用不了pair
调题依然浪费了40min!!没有理清细节就开始写题,就是浪费时间,必须静心把所有细节想一遍!

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, a, b) for (int i = a; i >= b; --i)
#define repd(i, a, b) for (int i = a; i >= b; --i)
#define rvc(i, S) for (int i = 0; i < (int)S.size(); ++i)
#define fore(i, x) for (int i = head[x]; i; i = e[i].next)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define lowbit(x) (x & (-x))
using namespace std;
#define map cc_hash_table

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pr;
const long long inf = 2e9;
const int maxn = 2020;

const ll mod = 1e9 + 7, inv2 = 500000004, inv4 = 250000002;

map<ll, ll> f;
struct node {
    ll p;
    int num, num2;
    node() { p = 0, num = num2 = 0; }
};
vector<node> e[40];
int n, m, cnt, s;

inline ll power(ll x, ll y) {
    ll res = 1;
    while (y) {
        if (y & 1)
            res = res * x % mod;
        x = x * x % mod;
        y >>= 1;
    }
    return res;
}
inline void up(ll &x, ll y) { x = (x + y) % mod; }
ll DP(ll S) {
    if (!S)
        return 1;
    if (f.find(S) != f.end())
        return f[S];
    int S1 = S >> (n + 1) , S2 = S & s ,  x = __builtin_ctz(S1);
    ll res = 0;
    rvc(i, e[x]) {
        node c = e[x][i];
        if (((S1 & c.num2) == c.num2) && ((S2 & c.num) == c.num)) {
            up(res, DP((((ll)S1 ^ c.num2) << (n + 1))|(S2 ^ c.num)) * c.p);
        }
    }
    return f[S] = res;
}
int main() {
    scanf("%d %d", &n, &m);
    rep(i, 1, m) {
        int tp, x1, y1, x2, y2;
        node cur;
        scanf("%d", &tp);
        if (tp == 0) {
            scanf("%d %d", &x1, &y1);
            cur.p = inv2, cur.num = 1 << y1, cur.num2 = 1 << x1;
            e[x1].pb(cur);
        } else if (tp == 1) {
            scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
            cur.p = inv2, cur.num = 1 << y1, cur.num2 = 1 << x1;
            e[x1].pb(cur);
            cur.p = inv2, cur.num = 1 << y2, cur.num2 = 1 << x2;
            e[x2].pb(cur);
			
            if ( x1 == x2 || y1 == y2 ) continue;
            cur.p = inv4, cur.num = (1 << y1) + (1 << y2) * (y1 != y2) , cur.num2 = (1 << x1) + (1 << x2) ;
            if (x1 < x2)
                e[x1].pb(cur);
            else
                e[x2].pb(cur);
        } else {
            scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
            cur.p = inv2, cur.num = 1 << y1, cur.num2 = 1 << x1;
            e[x1].pb(cur);
            cur.p = inv2, cur.num = 1 << y2, cur.num2 = 1 << x2;
            e[x2].pb(cur);
            if ( x1 == x2 || y1 == y2 ) continue;
			cur.p = mod - inv4, cur.num = (1 << y1) + (1 << y2), cur.num2 = (1 << x1) + (1 << x2);
            if (x1 < x2)
                e[x1].pb(cur);
            else
                e[x2].pb(cur);
        }
    }
    s = ((1 << n) - 1) << 1;
    cout << DP(((ll)s << (n + 1)) | s) * power(2, n) % mod;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值