码迷,mamicode.com
首页 > 其他好文 > 详细

[BZOJ5006][LOJ#2290][THUWC2017]随机二分图(概率+状压DP)

时间:2018-04-25 12:13:33      阅读:360      评论:0      收藏:0      [点我收藏+]

标签:define   names   cto   stdin   fine   blog   can   stdout   style   

https://loj.ac/problem/2290

题解:https://blog.csdn.net/Vectorxj/article/details/78905660

不是很好理解,对于边(x1,y1)和(x2,y2),可以分“x1或y1已匹配”,“x2或y2已匹配”,“x1,x2,y1,y2均未匹配”三种情况考虑拆边的正确性。

状压的时候,对于当前左边已经匹配的集合,只需要枚举左边已匹配的最后一个是用哪条边匹配的即可,也就是程序里的S<(1<<T)。

不要用顺推,记忆化搜索会忽略一些用不到的状态,所以会快一些。

 1 #include<map>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #define rep(i,l,r) for (int i=l; i<=r; i++)
 5 using namespace std;
 6 
 7 const int N=16,mod=1000000007,inv2=(mod+1)>>1,inv4=(mod+1)>>2;
 8 int n,m,cnt,t,x,y;
 9 struct E{ int S,p,c; E(int _S=0,int _p=0,int _c=0):S(_S),p(_p),c(_c){} }G[N*N<<2];
10 map<int,int>f[1<<N];
11 
12 inline void add(int &x,int y){ x+=y; if (x>=mod) x-=mod; }
13 int F(int S){
14     if (!S) return 1;
15     int T0=S>>n,S0=S^(T0<<n);
16     if (f[S0].count(T0)) return f[S0][T0];
17     int res=0;
18     rep(i,1,cnt){
19         int T=G[i].S;
20         if ((T&S)==T && S<(T<<1)) add(res,1ll*F(S^T)*G[i].p%mod);
21     }
22     return f[S0][T0]=res;
23 }
24 
25 int main(){
26     freopen("bzoj5006.in","r",stdin);
27     freopen("bzoj5006.out","w",stdout);
28     scanf("%d%d",&n,&m);
29     rep(i,1,m){
30         scanf("%d%d%d",&t,&x,&y);
31         int S1=(1<<(x-1))|(1<<(y+n-1)); G[++cnt]=E(S1,inv2,1);
32         if (t){
33             scanf("%d%d",&x,&y);
34             int S2=(1<<(x-1))|(1<<(y+n-1)); G[++cnt]=E(S2,inv2,1);
35             if (S1 & S2) continue;
36             G[++cnt]=E(S1|S2,((t==1)?inv4:mod-inv4),2);
37         }
38     }
39     printf("%lld\n",(1ll<<n)*F((1<<(n*2))-1)%mod);
40     return 0;
41 }

 

[BZOJ5006][LOJ#2290][THUWC2017]随机二分图(概率+状压DP)

标签:define   names   cto   stdin   fine   blog   can   stdout   style   

原文地址:https://www.cnblogs.com/HocRiser/p/8941925.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!