BZOJ4596: [Shoi2016]黑暗前的幻想乡
Description
Input
Output
Sample Input
2 3 2 4 2
5 2 1 3 1 3 2 4 1 4 3
4 2 1 3 2 4 1 4 2
Sample Output
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
const int BufferSize=1<<16;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
if(head==tail) {
int l=fread(buffer,1,BufferSize,stdin);
tail=(head=buffer)+l;
}
return *head++;
}
inline int read() {
int x=0,f=1;char c=Getchar();
for(;!isdigit(c);c=Getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=Getchar()) x=x*10+c-'0';
return x*f;
}
typedef long long ll;
const int maxn=20;
const int mod=1000000007;
typedef ll Matrix[maxn][maxn];
void gcd(ll a,ll b,ll& x,ll& y) {
if(!b) x=1,y=0;
else gcd(b,a%b,y,x),y-=x*(a/b);
}
ll getinv(ll a) {
ll b=mod,x,y;gcd(a,b,x,y);
return (x+mod)%mod;
}
Matrix B;
ll gauss(Matrix A,int n) {
ll ans=1;
rep(i,0,n-1) {
int r=i;
rep(j,i+1,n-1) if(abs(A[r][i])<abs(A[j][i])) r=j;
if(r!=i) ans*=-1,swap(A[r],A[i]);
ll inv=getinv(A[i][i]);
rep(k,0,n-1) if(i!=k)
dwn(j,n-1,i) A[k][j]=(A[k][j]-A[k][i]*A[i][j]%mod*inv%mod+mod)%mod;
}
rep(i,0,n-1) (ans*=A[i][i])%=mod;
return (ans+mod)%mod;
}
struct Company {
int m,u[maxn*maxn],v[maxn*maxn];
}A[maxn];
int main() {
int n=read();
rep(i,0,n-2) dwn(j,A[i].m=read(),1) A[i].u[j]=read(),A[i].v[j]=read();
ll ans=0;
rep(S,0,(1<<n-1)-1) {
int cnt=0;
rep(i,0,n-1) rep(j,0,n-1) B[i][j]=0;
rep(i,0,n-2) if(S>>i&1) {
cnt++;
rep(j,1,A[i].m) {
int u=A[i].u[j]-1,v=A[i].v[j]-1;
B[u][u]++;B[v][v]++;B[u][v]--;B[v][u]--;
}
}
rep(i,0,n-1) rep(j,0,n-1) (B[i][j]+=mod)%=mod;
if(n-1-cnt&1) (ans-=gauss(B,n-1))%=mod;
else (ans+=gauss(B,n-1))%=mod;
}
printf("%lld\n",(ans+mod)%mod);
return 0;
}
BZOJ4596: [Shoi2016]黑暗前的幻想乡的更多相关文章
- bzoj4596[Shoi2016]黑暗前的幻想乡 Matrix定理+容斥原理
4596: [Shoi2016]黑暗前的幻想乡 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 464 Solved: 264[Submit][Sta ...
- 【BZOJ4596】[Shoi2016]黑暗前的幻想乡 容斥+矩阵树定理
[BZOJ4596][Shoi2016]黑暗前的幻想乡 Description 幽香上台以后,第一项措施就是要修建幻想乡的公路.幻想乡有 N 个城市,之间原来没有任何路.幽香向选民承诺要减税,所以她打 ...
- bzoj4596/luoguP4336 [SHOI2016]黑暗前的幻想乡(矩阵树定理,容斥)
bzoj4596/luoguP4336 [SHOI2016]黑暗前的幻想乡(矩阵树定理,容斥) bzoj Luogu 题解时间 看一看数据范围,求生成树个数毫无疑问直接上矩阵树定理. 但是要求每条边都 ...
- bzoj 4596 [Shoi2016]黑暗前的幻想乡 矩阵树定理+容斥
4596: [Shoi2016]黑暗前的幻想乡 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 559 Solved: 325[Submit][Sta ...
- [ZJOI2016]小星星&[SHOI2016]黑暗前的幻想乡(容斥)
这两道题思路比较像,所以把他们放到一块. [ZJOI2016]小星星 题目描述 小Y是一个心灵手巧的女孩子,她喜欢手工制作一些小饰品.她有n颗小星星,用m条彩色的细线串了起来,每条细线连着两颗小星星. ...
- P4336 [SHOI2016]黑暗前的幻想乡
P4336 [SHOI2016]黑暗前的幻想乡 矩阵树定理(高斯消元+乘法逆元)+容斥 ans=总方案数 -(公司1未参加方案数 ∪ 公司2未参加方案数 ∪ 公司3未参加方案数 ∪ ...... ∪ ...
- 【BZOJ4596】黑暗前的幻想乡(矩阵树定理,容斥)
[BZOJ4596]黑暗前的幻想乡(矩阵树定理,容斥) 题面 BZOJ 有\(n\)个点,要求连出一棵生成树, 指定了一些边可以染成某种颜色,一共\(n-1\)种颜色, 求所有颜色都出现过的生成树方案 ...
- 【BZOJ 4596】 4596: [Shoi2016]黑暗前的幻想乡 (容斥原理+矩阵树定理)
4596: [Shoi2016]黑暗前的幻想乡 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 324 Solved: 187 Description ...
- BZOJ4596:[SHOI2016]黑暗前的幻想乡——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4596 https://www.luogu.org/problemnew/show/P4336#su ...
随机推荐
- MongoDB安装
安装 1>设置MongoDB目录 cd /home/apps 附:centOS下创建目录命令 mkdir /home/apps 2>下载mongodb curl -O http ...
- LRU LFU FIFO 转载
-------------------------------------->href--------------------------> http://blog.chinaunix.n ...
- debian 或者kali 安装git
安装git 是时候动手尝试下 Git 了,不过得先安装好它.有许多种安装方式,主要分为两种,一种是通过编译源代码来安装:另一种是使用为特定平台预编译好的安装包. 源码安装 若是条件允许,从源代码安装有 ...
- 安卓手机 虚拟键盘输入用 position:fixed解决 !!!
下面 主要代码 方便看. 样式 <style> *{ margin:0; padding:0; } html{ height:100%;/*关键代码*/ } body{ height:10 ...
- Ionic 常用插件
ionic扩展插件 1.ionic-timepicker 时间选择 https://github.com/rajeshwarpatlolla/ionic-timepicker 2.ionic-da ...
- 值得推荐的C/C++框架和库
值得推荐的C/C++框架和库 [本文系外部转贴,原文地址:http://coolshell.info/c/c++/2014/12/13/c-open-project.htm]留作存档 下次造轮子前先看 ...
- Vim配置文件
转载 原文网址:http://www.cnblogs.com/ma6174/archive/2011/12/10/2283393.html 花了很长时间整理的,感觉用起来很方便,共享一下. 我的vim ...
- 【转】Caffe初试(九)solver及其设置
solver算是caffe的核心的核心,它协调着整个模型的运作.caffe程序运行必带的一个参数就是solver配置文件.运行代码一般为 #caffe train --solver=*_solver. ...
- Spring + Jedis集成Redis(集群redis数据库)
前段时间说过单例redis数据库的方法,但是生成环境一般不会使用,基本上都是集群redis数据库,所以这里说说集群redis的代码. 1.pom.xml引入jar <!--Redis--> ...
- php barcode 制作二条码,隐藏条码的内容,只保留条码
<?php global $_W, $_GPC; $operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display'; require_once ...