URAL - 1627:Join (生成树计数)
Join
题目链接:https://vjudge.net/problem/URAL-1627
Description:
Businessman Petya recently bought a new house. This house has one floor with n × m square rooms, placed in rectangular lattice. Some rooms are pantries and the other ones are bedrooms. Now he wants to join all bedrooms with doors in such a way that there will be exactly one way between any pair of them. He can make doors only between neighbouring bedrooms (i.e. bedrooms having a common wall). Now he wants to count the number of different ways he can do it.
Input:
First line contains two integers n and m (1 ≤ n, m ≤ 9) — the number of lines and columns in the lattice. Next n lines contain exactly m characters representing house map, where "." means bedroom and "*" means pantry. It is guaranteed that there is at least one bedroom in the house.
Output:
Output the number of ways to join bedrooms modulo 10 9.
Sample Input:
2 2
.*
*.
Sample Output:
0
题意:
给出一个n*m的矩阵,然后"*"表示障碍物。现在可以在两个"."之间搭桥,问有多少种方式将所有的"."连通。
题解:
直接给所有能够搭桥的点建边,然后就是个生成树计数问题了。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = ,MOD = 1e9;
char mp[N][N];
int g[N][N],num[N][N];
ll b[N][N];
int tot=;
ll Det(int n){
int i,j,k;
ll ret = ;
if(n==) return ;
for(i=;i<=n;i++){
for(j = i+;j <= n;j++){
while(b[j][i]){
ll tmp=b[i][i]/b[j][i];//不存在除不尽的情况
for(k = i;k <= n;k++)
b[i][k] = ((b[i][k] - tmp*b[j][k])%MOD+MOD)%MOD;
for(k=i;k<=n;k++)
swap(b[i][k],b[j][k]);
ret = -ret;
}
}
if(!b[i][i]) return ;
ret = ret * b[i][i]%MOD;
if(ret<) ret+=MOD;
}
if(ret < ) ret += MOD;
return ret;
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%s",mp[i]+);
for(int j=;j<=m;j++){
if(mp[i][j]=='.') num[i][j]=++tot;
}
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mp[i][j]=='*') continue ;
if(j>&&mp[i][j-]=='.') g[num[i][j-]][num[i][j]]=;
if(i<n&&mp[i+][j]=='.') g[num[i+][j]][num[i][j]]=;
}
}
for(int i=;i<=tot;i++){
for(int j=;j<=tot;j++){
if(g[i][j]){
b[i][i]++;b[j][j]++;
b[i][j]=b[j][i]=-;
}
}
}
cout<<Det(tot);
return ;
}
URAL - 1627:Join (生成树计数)的更多相关文章
- kuangbin带你飞 生成树专题 : 次小生成树; 最小树形图;生成树计数
第一个部分 前4题 次小生成树 算法:首先如果生成了最小生成树,那么这些树上的所有的边都进行标记.标记为树边. 接下来进行枚举,枚举任意一条不在MST上的边,如果加入这条边,那么肯定会在这棵树上形成一 ...
- URAL-1627-Join 生成树计数
传送门:https://vjudge.net/problem/URAL-1627 题意: 给定一个n*m的图,问图中“.”的点生成的最小生成树有多少个. 思路: 生成树的计数,需要用Kirchhoff ...
- 【BZOJ1002】【FJOI2007】轮状病毒(生成树计数)
1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1766 Solved: 946[Submit][Status ...
- SPOJ 104 HIGH - Highways 生成树计数
题目链接:https://vjudge.net/problem/SPOJ-HIGH 解法: 生成树计数 1.构造 基尔霍夫矩阵(又叫拉普拉斯矩阵) n阶矩阵 若u.v之间有边相连 C[u][v]=C[ ...
- Luogu P5296 [北京省选集训2019]生成树计数
Luogu P5296 [北京省选集训2019]生成树计数 题目链接 题目大意:给定每条边的边权.一颗生成树的权值为边权和的\(k\)次方.求出所有生成树的权值和. 我们列出答案的式子: 设\(E\) ...
- Loj 2320.「清华集训 2017」生成树计数
Loj 2320.「清华集训 2017」生成树计数 题目描述 在一个 \(s\) 个点的图中,存在 \(s-n\) 条边,使图中形成了 \(n\) 个连通块,第 \(i\) 个连通块中有 \(a_i\ ...
- 「UVA10766」Organising the Organisation(生成树计数)
BUPT 2017 Summer Training (for 16) #6C 题意 n个点,完全图减去m条边,求生成树个数. 题解 注意可能会给重边. 然后就是生成树计数了. 代码 #include ...
- SPOJ.104.Highways([模板]Matrix Tree定理 生成树计数)
题目链接 \(Description\) 一个国家有1~n座城市,其中一些城市之间可以修建高速公路(无自环和重边). 求有多少种方案,选择修建一些高速公路,组成一个交通网络,使得任意两座城市之间恰好只 ...
- BZOJ1494 [NOI2007]生成树计数
题意 F.A.Qs Home Discuss ProblemSet Status Ranklist Contest 入门OJ ModifyUser autoint Logout 捐赠本站 Probl ...
随机推荐
- Linux 文件的常识
文件 文件的分类 文件 目录 链接 区分办法,ls -la 查看 十个标志符中的第一个 如:drwxrwxr-x. 2 normal normal 4096 8月 31 23:43 dir 目录是d ...
- 如何处理 jQuery $(window).resize() 中的方法被多次执行的小问题
引言: 估计很多同志们在编写浏览器resize()的方法时,都会遇到这样的情况: 当拖动浏览器的边角时,页面中的一些效果随浏览器大小的改变而触发,这一过程开始到结束,resize() 中的方法被执行了 ...
- NMAP-主机扫描
1.全面扫描 2.扫描指定段 3.ping扫描 只进行ping操作,十分隐蔽 4.无ping扫描 适用于防火墙禁止ping 5.TCP SYN扫描 6.TCP ACK扫描 7.UDP扫描 8.ICMP ...
- 自测之Lesson16:并发通信
知识点:三个多路并发模型(select .poll .epoll) 题目:以epoll模型,编写一个可供多个客户端访问的服务器程序. 实现代码: #include <netinet/in.h&g ...
- 自测之Lesson6:文件I/O
题目:区分文件I/O和标准I/O. 区别: ①首先两者一个显著的不同点在于,标准I/O默认采用了缓冲机制,比如调用fopen函数,不仅打开一个文件,而且建立了一个缓冲区(读写模式下将建立两个缓冲区), ...
- Switches and Lamps(思维)
You are given n switches and m lamps. The i-th switch turns on some subset of the lamps. This inform ...
- 20172305 2018-2019-1 《Java软件结构与数据结构》第一周学习总结
20172305 2018-2019-1 <Java软件结构与数据结构>第一周学习总结 教材学习内容总结 本周内容主要为书第一章和第二章的内容: 第一章 软件质量: 正确性(软件达到特定需 ...
- mini2440 Nor Flash工作原理分析
我的mini2440上是只接了一块Nor Flash,型号是S29AL016M90TAI02,这是一块2M Byte,16位宽度的Nor Flash,用于引导扇区的闪存.原理图里面关键的引脚是: 地址 ...
- 【week6】psp
本周psp
- BAT批处理(三)
1.set set命令:显示.设置或删除变量.显示变量:set 或 set s 前者显示批处理当前已定义的所有变量及其值,后者显示所有以s开头的变量及值.设置变量:set aa=abcd 此句命令便可 ...