zoukankan      html  css  js  c++  java
  • bzoj4031 [HEOI2015]小Z的房间

    Description

    你突然有了一个大房子,房子里面有一些房间。事实上,你的房子可以看做是一个包含n*m个格子的格状矩形,每个格子是一个房间或者是一个柱子。在一开始的时候,相邻的格子之间都有墙隔着。

    你想要打通一些相邻房间的墙,使得所有房间能够互相到达。在此过程中,你不能把房子给打穿,或者打通柱子(以及柱子旁边的墙)。同时,你不希望在房子中有小偷的时候会很难抓,所以你希望任意两个房间之间都只有一条通路。现在,你希望统计一共有多少种可行的方案。

    Input

    第一行两个数分别表示n和m。

    接下来n行,每行m个字符,每个字符都会是’.’或者’*’,其中’.’代表房间,’*’代表柱子。

    Output

     一行一个整数,表示合法的方案数 Mod 10^9

    Sample Input

    3 3
    ...
    ...
    .*.

    Sample Output

    15

    HINT

    对于前100%的数据,n,m<=9

    正解:矩阵树定理+高斯消元。

    $Matrix-Tree$定理

    1、$G$的度数矩阵$({D_G})$是一个$n*n$的矩阵,并且满足:当$i≠j$时,$({D_{i,j}})=0$;当$i=j$时,$({D_{i,j}})$等于$({V_{i}})$的度数。
    2、$G$的邻接矩阵$({A_{G}})$也是一个$n*n$的矩阵,并且满足:如果$({V_{i}})$、$({V_{j}})$之间有边直接相连,则$({A_{i,j}})=1$,否则为$0$。
    定义$G$的$Kirchhoff$矩阵$(C_G)$为$(C_G=D_G-A_G)$
    $Matrix-Tree$定理:$G$的所有不同的生成树的个数等于其$Kirchhoff$矩阵$(C_G)$任何一个$n-1$阶主子式(去掉第$r$行第$r$列的新矩阵)的行列式的绝对值。

    这题有一个麻烦的地方在于:模数不是质数。所以我们不能直接求逆元。但是我们可以用欧几里得定理,直接辗转相除就行了。

     1 //It is made by wfj_2048~
     2 #include <algorithm>
     3 #include <iostream>
     4 #include <complex>
     5 #include <cstring>
     6 #include <cstdlib>
     7 #include <cstdio>
     8 #include <vector>
     9 #include <cmath>
    10 #include <queue>
    11 #include <stack>
    12 #include <map>
    13 #include <set>
    14 #define rhl (1000000000)
    15 #define inf (1<<30)
    16 #define il inline
    17 #define RG register
    18 #define ll long long
    19 #define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
    20 
    21 using namespace std;
    22 
    23 ll a[110][110],d[110][110],g[110][110],c[110][110],n,m,cnt,ans;
    24 char s[110][110];
    25 
    26 il void insert(RG ll x,RG ll y){ g[x][y]=1,d[x][x]++; return; }
    27 
    28 il void gauss(){
    29     RG ll f=1;
    30     for (RG ll i=1;i<cnt;++i){
    31     for (RG ll j=i+1;j<cnt;++j){
    32         RG ll x=a[i][i],y=a[j][i];
    33         while (y){
    34         RG ll t=x/y; x%=y; swap(x,y);
    35         for (RG ll k=i;k<cnt;++k){
    36             a[i][k]=(a[i][k]-t*a[j][k]%rhl+rhl)%rhl;
    37             swap(a[i][k],a[j][k]);
    38         }
    39         f=-f;
    40         }
    41     }
    42     if (!a[i][i]){ ans=0; return; }
    43     ans=ans*a[i][i]%rhl;
    44     }
    45     if (f==-1) ans=(rhl-ans)%rhl; return;
    46 }
    47 
    48 il void work(){
    49     cin>>n>>m,ans=1;
    50     for (RG ll i=1;i<=n;++i){
    51     scanf("%s",s[i]+1);
    52     for (RG ll j=1;j<=m;++j)
    53         if (s[i][j]=='.') c[i][j]=++cnt;
    54     }
    55     for (RG ll i=1;i<=n;++i)
    56     for (RG ll j=1;j<=m;++j){
    57         if (s[i][j]=='*') continue;
    58         if (i-1>0 && s[i-1][j]=='.') insert(c[i][j],c[i-1][j]);
    59         if (i+1<=n && s[i+1][j]=='.') insert(c[i][j],c[i+1][j]);
    60         if (j-1>0 && s[i][j-1]=='.') insert(c[i][j],c[i][j-1]);
    61         if (j+1<=m && s[i][j+1]=='.') insert(c[i][j],c[i][j+1]);
    62     }
    63     for (RG ll i=1;i<=cnt;++i)
    64     for (RG ll j=1;j<=cnt;++j) a[i][j]=(d[i][j]-g[i][j]+rhl)%rhl;
    65     gauss(); printf("%lld",ans); return;
    66 }
    67 
    68 int main(){
    69     File("room");
    70     work();
    71     return 0;
    72 }
  • 相关阅读:
    leetcode701. Insert into a Binary Search Tree
    leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes
    leetcode 110. Balanced Binary Tree
    leetcode 104. Maximum Depth of Binary Tree 111. Minimum Depth of Binary Tree
    二叉树
    leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)
    5. Longest Palindromic Substring
    128. Longest Consecutive Sequence
    Mac OS下Android Studio的Java not found问题,androidfound
    安卓 AsyncHttpClient
  • 原文地址:https://www.cnblogs.com/wfj2048/p/6633844.html
Copyright © 2011-2022 走看看