• 【bzoj4031】[HEOI2015]小Z的房间 Matrix-Tree定理+高斯消元


    【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

    题解

     
    题解:周东的那篇IOI国家集训队论文,
       就是对于每个点求出度数
       然后列出一个行列式,
       a[i][i]=du[i]
       对于边u,v
       则a[u][v]=-1
       然后高斯消元,答案就是主对角线的积。
     1 #include<cstring>
     2 #include<cmath>
     3 #include<iostream>
     4 #include<algorithm>
     5 #include<cstdio>
     6 #include<cstdlib>
     7 
     8 #define N 107
     9 #define mod 1000000000
    10 #define ll long long
    11 using namespace std;
    12 const int xx[4]={-1,1,0,0};
    13 const int yy[4]={0,0,-1,1};
    14 inline int read()
    15 {
    16     int x=0,f=1;char ch=getchar();
    17     while(ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    18     while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    19     return x*f;
    20 }
    21 
    22 int n,m,id;
    23 char ch[N][N];
    24 ll a[N][N],p[N][N];
    25 
    26 int solve_sum_gaosi(int n)
    27 {
    28     for (int i=1;i<=n;i++)
    29         for (int j=1;j<=n;j++)
    30             (a[i][j]+=mod)%=mod;
    31     ll ans=1,f=1;
    32     for (int i=1;i<=n;i++)//楂樻柉娑堝厓
    33     {
    34         for (int j=i+1;j<=n;j++)
    35         {
    36             ll A=a[i][i],B=a[j][i];
    37             while(B!=0)
    38             {
    39                 ll t=A/B;A%=B;swap(A,B);
    40                 for (int k=i;k<=n;k++)
    41                     a[i][k]=(a[i][k]-t*a[j][k]%mod+mod)%mod;
    42                 for (int k=i;k<=n;k++)
    43                     swap(a[i][k],a[j][k]);
    44                 f=-f;
    45             }
    46         }
    47         if (!a[i][i])return 0;
    48         (ans*=a[i][i])%=mod;
    49     }
    50     if (f==-1) return (mod-ans)%mod;
    51     else return ans;
    52 }
    53 int main()
    54 {
    55     int n=read(),m=read();
    56     for (int i=1;i<=n;i++)
    57         scanf("%s",ch[i]+1);
    58     for (int i=1;i<=n;i++)
    59         for (int j=1;j<=m;j++)
    60             if (ch[i][j]=='.')p[i][j]=++id;
    61     for (int i=1;i<=n;i++)
    62         for (int j=1;j<=m;j++)
    63             if (ch[i][j]=='.')
    64                 for (int k=0;k<4;k++)
    65                 {
    66                     int x=i+xx[k],y=j+yy[k];
    67                     if (x<1||y<1||x>n||y>m||ch[x][y]!='.')continue;
    68                     int u=p[i][j],v=p[x][y];
    69                     a[u][u]++,a[u][v]--;
    70                 }
    71     /*for (int i=1;i<=n;i++)
    72     {
    73         for (int j=1;j<=n;j++)
    74             cout<<a[i][j]<<" ";
    75         cout<<endl;    
    76     }*/
    77     printf("%d
    ",(solve_sum_gaosi(id-1)+mod)%mod);//鐩存帴鑷�潃鏈€鍚庝竴琛屽嵆鍙?
    78 }
  • 相关阅读:
    evernote100个做笔记的好方法
    平衡二叉树的调整模版
    晨间日记的奇迹
    hdu 2952 Counting Sheep
    hdu 1535 Invitation Cards
    poj 3259 Wormholes(spfa)
    poj 2263 Heavy Cargo(floyd)
    poj 3268 Silver Cow Party(SPFA)
    hdu 1690 Bus System
    hdu 3631 Shortest Path(Floyd)
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/8204151.html
Copyright © 2020-2023  润新知