[HEOI2015]小Z的房间 && [CQOI2018]社交网络
今天看了一下矩阵树定理,然后学了一下\(O(n ^ 3)\)的方法求行列式。
哦对了,所有的证明我都没看……
这位大佬讲的好呀:
[学习笔记]高斯消元、行列式、Matrix-Tree 矩阵树定理
关于模数不是质数的情况,我看了半天才懂:其实就是加速了两行的辗转相减,把一次次减换成了取模。然后别忘了每一次交换两行的时候行列式要取相反数。
[HEOI2015]小Z的房间
这题就是板儿题了。把是'.'的格子记录下来,然后构造基尔霍夫矩阵就行了。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 105;
const ll mod = 1e9;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n, m, cnt = 0, id[maxn][maxn];
char s[maxn][maxn];
ll f[maxn][maxn];
In void add(int x, int y)
{
++f[x][x], ++f[y][y], --f[x][y], --f[y][x];
}
In ll Gauss(int n)
{
ll ans = 1;
for(int i = 1; i <= n; ++i)
{
for(int j = i + 1; j <= n; ++j)
while(f[j][i])
{
ll d = f[i][i] / f[j][i];
for(int k = i; k <= n; ++k) f[i][k] = (f[i][k] - d * f[j][k] % mod + mod) % mod;
swap(f[i], f[j]), ans = -ans;
}
ans = (ans * f[i][i] % mod + mod) % mod;
}
return ans;
}
int main()
{
n = read(), m = read();
for(int i = 1; i <= n; ++i) scanf("%s", s[i] + 1);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j) if(s[i][j] == '.') id[i][j] = ++cnt;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
if(id[i][j])
{
if(id[i][j + 1]) add(id[i][j], id[i][j + 1]);
if(id[i + 1][j]) add(id[i][j], id[i + 1][j]);
}
write(Gauss(cnt - 1)), enter;
return 0;
}
[[CQOI2018]社交网络](https://www.luogu.org/problemnew/show/P4455)
这题让求的是以节点1为根的生成树个数,于是我们把矩阵的第一行第一列消去,剩下的求一个行列式就是答案了。
注意连边是反的。
```c++
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 255;
const ll mod = 1e4 + 7;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans = 10) write(x / 10);
putchar(x % 10 + '0');
}
int n, m;
ll f[maxn][maxn];
In ll quickpow(ll a, ll b)
{
ll ret = 1;
for(; b; b >>= 1, a = a * a % mod)
if(b & 1) ret = ret * a % mod;
return ret;
}
In ll Gauss()
{
ll ans = 1;
for(int i = 2; i <= n; ++i)
{
int pos = i;
while(!f[pos][i]) ++pos;
if(pos ^ i) swap(f[i], f[pos]), ans = mod - ans;
if(!f[i][i]) return 0; //如果每一行第i个数都输0,才返回0
ans = ans * f[i][i] % mod;
ll inv = quickpow(f[i][i], mod - 2);
for(int j = i + 1; j <= n; ++j)
{
ll tp = f[j][i] * inv % mod;
for(int k = i; k <= n; ++k) f[j][k] = (f[j][k] - tp * f[i][k] % mod + mod) % mod;
}
}
return ans;
}
int main()
{
n = read(), m = read();
for(int i = 1; i <= m; ++i)
{
int y = read(), x = read(); //x -> y
++f[y][y], --f[y][x];
}
write(Gauss()), enter;
return 0;
}
[HEOI2015]小Z的房间 && [CQOI2018]社交网络的更多相关文章
- bzoj 4031: [HEOI2015]小Z的房间 轮廓线dp
4031: [HEOI2015]小Z的房间 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 98 Solved: 29[Submit][Status] ...
- 【bzoj4031】[HEOI2015]小Z的房间 解题报告
[bzoj4031][HEOI2015]小Z的房间 Description 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可以看做是一个包含\(n*m\)个格子的格状矩形,每个格子是一个房 ...
- 【BZOJ 4031】 4031: [HEOI2015]小Z的房间 (Matrix-Tree Theorem)
4031: [HEOI2015]小Z的房间 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1089 Solved: 533 Description ...
- BZOJ 4031: [HEOI2015]小Z的房间 高斯消元 MartixTree定理 辗转相除法
4031: [HEOI2015]小Z的房间 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4031 Description 你突然有了一个 ...
- 【bzoj4031】[HEOI2015]小Z的房间 Matrix-Tree定理+高斯消元
[bzoj4031][HEOI2015]小Z的房间 2015年4月30日3,0302 Description 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可以看做是一个包含n*m个格子的 ...
- 【bzoj4031】[HEOI2015]小Z的房间 && 【bzoj4894】天赋 (矩阵树定理)
来两道矩阵树模板: T1:[bzoj4031][HEOI2015]小Z的房间 Description 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可以看做是一个包含n*m个格子的格状矩形 ...
- 【刷题】BZOJ 4031 [HEOI2015]小Z的房间
Description 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可以看做是一个包含n*m个格子的格状矩形,每个格子是一个房间或者是一个柱子.在一开始的时候,相邻的格子之间都有墙隔着. ...
- P4111 [HEOI2015]小Z的房间 生成树计数
这个题是生成树计数的裸题,中间构造基尔霍夫矩阵,然后构成行列式,再用高斯消元就行了.这里高斯消元有一些区别,交换两行行列式的值变号,且消元只能将一行的数 * k 之后加到别的行上. 剩下就没啥了... ...
- bzoj4031 [HEOI2015]小Z的房间
Description 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可以看做是一个包含n*m个格子的格状矩形,每个格子是一个房间或者是一个柱子.在一开始的时候,相邻的格子之间都有墙隔着. ...
随机推荐
- CSS字体样式属性
font-size 字号大小 一般推荐使用相对长度(px ,em),不推荐使用绝对长度(in,cmm,mm,pt) font-family 字体 1.可以同时指定多个字体,中间用英文状态的逗号隔开,英 ...
- HDU 2586 How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- 实战 | Android中文图混排时文图的居中对齐 FontMetrics以及自定义ImageSpan实现
这个标题有点长,乍一看这么个标题你可能没明白啥意思,且听我慢慢道来. 公司的项目中新增了一个“心动” 的功能,用户初次使用时需要给一个引导页,就是下面图中的这个样子(这就是做完之后的效果了). 在上图 ...
- 图说Oracle基础知识(一)
本文主要对Oralce数据库操作的基础知识进行一下梳理,以便进行归纳总结.适用于未使用过Oracle数据库的读者,或需要学习Oracle数据库方面的基础知识.如有不足之处,还请指正. 关于SQL介绍的 ...
- leetcode-13罗马字符转整数
leetcode-13罗马字符转整数 算法:转换的规律是先逐字符按照对应的阿拉伯数字累加,然后对于特殊的(I.X.C出现在左侧)要处理.处理方法:出现特殊字符组合减去双倍的左侧字符(在开始的处理中已经 ...
- (其他)window10上好用的软件
1.everything:比我用电脑搜索快一些,(常用的搜索其实熟悉电脑的都会,不过想快一点) 如果你曾对效率工具有过研究,想必对 Everything 的名字不会陌生.这款仅有 1.3 MB 的小软 ...
- c++趣味之返回void
void a(){} void b(){return a();} int main() { b(); ; } 这个是能编译的(vs,gcc),void函数是能返回,一般不会这么写,但是这样确实可以.你 ...
- Docker EE 安装 on centos7
本文演示如何在CentOS7上安装Docker EE. 1 安装方式 有两种方法可以 在Centos上安装和升级Docker企业版(Docker EE): YUM存储库:设置Docker存储库并从中安 ...
- ThreadPoolExecutor源码解析(二)
1.ThreadPoolExcuter运行实例 首先我们先看如何新建一个ThreadPoolExecutor去运行线程.然后深入到源码中去看ThreadPoolExecutor里面使如何运作的. pu ...
- Mysql 查询昨天16:00至今天16:00之间的信息
SELECT * FROM t_violation_info WHERE ( DATE(UPDATED_TIME) = CURDATE() AND HOUR (UPDATED_TIME) < 1 ...