洛谷 P4547 & bzoj 5006 随机二分图 —— 状压DP+期望
题目:https://www.luogu.org/problemnew/show/P4547
https://www.lydsy.com/JudgeOnline/problem.php?id=5006
参考博客:https://www.cnblogs.com/yanshannan/p/9452802.html
注意同一个点连出去的两条边本来就不能一起选!
每次调用 map 会很慢!所以修改的时候新定义一个 &tmp,就能过了。
代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#define il inline
using namespace std;
typedef long long ll;
int rd()
{
int ret=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=; ch=getchar();}
while(ch>=''&&ch<='')ret=ret*+ch-'',ch=getchar();
return f?ret:-ret;
}
int const xn=,xm=(<<)+,mod=1e9+;
int n,m,bin[],ct;
map<int,int>f[xm];
struct N{
int s,t,w;
N(int s=,int t=,int w=):s(s),t(t),w(w) {}
}ed[];
il ll pw(ll a,int b){ll ret=; for(;b;b>>=,a=a*a%mod)if(b&)ret=ret*a%mod; return ret;}
il int upt(int x){while(x>=mod)x-=mod; while(x<)x+=mod; return x;}
il int dfs(int s,int t)
{
if(!s&&!t)return ;
if(f[s][t])return f[s][t];
int &tmp=f[s][t];//
for(int i=;i<=ct;i++)
{
if((ed[i].s<<)<=s||(ed[i].s|s)!=s||(ed[i].t|t)!=t)continue;//t
tmp=(tmp+(ll)dfs(s^ed[i].s,t^ed[i].t)*ed[i].w)%mod;
}
return tmp=upt(tmp);//-
}
int main()
{
n=rd(); m=rd();
bin[]=; for(int i=;i<=;i++)bin[i]=bin[i-]*;
int inv2=pw(,mod-),inv4=pw(,mod-);
for(int i=,tp,a,b,c,d;i<=m;i++)
{
tp=rd(); a=rd(); b=rd();
ed[++ct]=N(bin[a-],bin[b-],inv2);
if(!tp)continue; c=rd(); d=rd();
ed[++ct]=N(bin[c-],bin[d-],inv2);
if(a==c||b==d)continue;//!
if(tp==)ed[++ct]=N(bin[a-]|bin[c-],bin[b-]|bin[d-],inv4);
if(tp==)ed[++ct]=N(bin[a-]|bin[c-],bin[b-]|bin[d-],-inv4);
}
printf("%lld\n",(ll)dfs(bin[n]-,bin[n]-)*bin[n]%mod);
return ;
}
洛谷 P4547 & bzoj 5006 随机二分图 —— 状压DP+期望的更多相关文章
- 题解 洛谷 P4547 【[THUWC2017]随机二分图】
根据题意,题目中所求的即为所有\(n!\)种完美匹配的各自的出现概率之和再乘上\(2^n\)的值. 发现\(n\)很小,考虑状压\(DP\).设\(f_{S,T}\)为左部图匹配情况为\(S\),右部 ...
- UOJ #129 / BZOJ 4197 / 洛谷 P2150 - [NOI2015]寿司晚宴 (状压dp+数论+容斥)
题面传送门 题意: 你有一个集合 \(S={2,3,\dots,n}\) 你要选择两个集合 \(A\) 和 \(B\),满足: \(A \subseteq S\),\(B \subseteq S\), ...
- 【题解】洛谷P2704 [NOI2001] 炮兵阵地(状压DP)
洛谷P2704:https://www.luogu.org/problemnew/show/P2704 思路 这道题一开始以为是什么基于状压的高端算法 没想到只是一道加了一行状态判断的状压DP而已 与 ...
- 【题解】洛谷P1896 [SCOI2005] 互不侵犯(状压DP)
洛谷P1896:https://www.luogu.org/problemnew/show/P1896 前言 这是一道状压DP的经典题 原来已经做过了 但是快要NOIP 复习一波 关于一些位运算的知识 ...
- 洛谷P1171 售货员的难题【状压DP】
题目描述 某乡有n个村庄(1 输入格式: 村庄数n和各村之间的路程(均是整数). 输出格式: 最短的路程. 输入样例: 3 0 2 1 1 0 2 2 1 0 输出样例 3 说明 输入解释 3 {村庄 ...
- 2018.07.18 洛谷P1171 售货员的难题(状压dp)
传送门 感觉是一道经典的状压dp,随便写了一发卡了卡常数开了个O(2)" role="presentation" style="position: relati ...
- 洛谷P2761 软件补丁问题(状压dp)
传送门 啊咧……这题不是网络流二十四题么……为啥是个状压dp…… 把每一个漏洞看成一个状态,直接硬上状压dp 然后因为有后效型,得用spfa //minamoto #include<iostre ...
- 洛谷$P3226\ [HNOI2012]$集合选数 状压$dp$
正解:$dp$ 解题报告: 传送门$QwQ$ 考虑列一个横坐标为比值为2的等比数列,纵坐标为比值为3的等比数列的表格.发现每个数要选就等价于它的上下左右不能选. 于是就是个状压$dp$板子了$QwQ$ ...
- 洛谷 P2622 关灯问题II【状压DP】
传送门:https://www.luogu.org/problemnew/show/P2622 题面: 题目描述 现有n盏灯,以及m个按钮.每个按钮可以同时控制这n盏灯--按下了第i个按钮,对于所有的 ...
随机推荐
- django启动时报错:Apps aren't loaded yet.
1.解决方法 编辑manage.py文件,在文件顶部引入django模块.
- 关于Future
1 为什么需要Callable和Future Runnable没有返回值,也不抛异常,这样主线程不能知道子线程的执行结果. 为了解决这个问题就有了Callable和Future.Callable提供的 ...
- 什么是GIL锁以及作用
全局解释锁,每次只能一个线程获得cpu的使用权:为了线程安全,也就是为了解决多线程之间的数据完整性和状态同步而加的锁,因为我们知道线程之间的数据是共享的.
- git本地仓库管理远程仓库
git remote add origin https://xxxxxgit push -u origin master
- C#快速入门笔记(1)——基础语法
C#快速入门笔记(1)——基础语法 总体框架:
- 事务的四大特性ACID
ACID是指数据库事务的四大特性,是由Jim Gray在19世纪70年代后期提出的概念,1983年Andreas Reuter and Theo Härder创造了ACID这个缩略语用来描述这四大特性 ...
- Data Structure Stack: Reverse a stack using recursion
http://www.geeksforgeeks.org/reverse-a-stack-using-recursion/ #include <iostream> #include < ...
- Data Structure Array: Largest subarray with equal number of 0s and 1s
http://www.geeksforgeeks.org/largest-subarray-with-equal-number-of-0s-and-1s/ #include <iostream& ...
- Vue组件通信(传值)
先介绍一下什么是组件把: 创建组件的两种方式: 全局组件 // 组件就是vue的一个拓展实例 let component=Vue.extend({ data(){ return{ //与vue实例中的 ...
- String类型的对象,是保存在堆里还是在栈里呢?
在Java的实现中,new出来的String对象一般是放在堆中的. 如果是 String s ="xxx"; 这种,那就是放在常量池中. JDK6将常量池放在方法区中. 方法区此时 ...