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 ...
随机推荐
- appium启动APP配置参数:
一.Android启动app python启动脚本如下: from appium import webdriver desired_caps = {} desired_caps['plat ...
- FPGA学习-PS2接口
选自http://m.elecfans.com/article/774143.html
- Java算法2
实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 分析:若从前向后遍历的话,那Happy后面 ...
- LeetCode 386——字典序排数
1. 题目 2. 解答 2.1 方法一 假设返回 118 以内数的字典顺序,则为 1,10,100,101,102,...,109,11,110,111,112,...,118,12,13,....根 ...
- 聊聊、dubbo 找不到 dubbo.xsd 报错
平常在用 Dubbo 的时候,创建 xml 会提示 http://code.alibabatech.com/schema/dubbo/dubbo.xsd 找不到. 大家可以去 https://gith ...
- 3D动态人脸识别技术分析——世纪晟人脸识别实现三维人脸建模
- 目录 - 国内3D动态人脸识别现状概况 - 新形势下人脸识别技术发展潜力 - 基于深度学习的3D动态人脸识别技术分析 1. 非线性数据建模方法 2. 基于3D变形模型的人脸建模 - 案例结合——世 ...
- 接口_GET请求_基于python
1.GET请求(不带参数) # coding:utf-8 import requests r=requests.get("https://www.baidu.com") #r即为r ...
- 为什么23种设计模式中没有MVC
GoF (Gang of Four,四人组, <Design Patterns: Elements of Reusable Object-Oriented Software>/<设计 ...
- 迭代器类型:iterator & const_iterator
vector<int> ivec{1, 3, 4, 1, 3, 4}; vector<int>::iterator iter; // iter能读写vector<int& ...
- Thunder团队第六周 - Scrum会议6
Scrum会议6 小组名称:Thunder 项目名称:i阅app Scrum Master:邹双黛 工作照片: 邹双黛同学在拍照,所以不在照片内. 参会成员: 王航:http://www.cnblog ...