• 最小生成树计数(matrix tree矩阵树定理)


    Matrix-Tree 定理是解决生成树计数问题最有力的武器之一。它首先于 1847 年被Kirchhoff 证明。 在介绍定理之前, 我们首先明确几个概念: 

    1G 的度数矩阵 D[G]是一个 n*n 的矩阵, 并且满足: 当 ij ,dij=0;当 i=j时, dij 等于 vi 的度数。

    2G 的邻接矩阵 A[G]也是一个 n*n 的矩阵, 并且满足: 如果 vivj 之间有边直接相连,则 aij=1,否则为 0。 

    我们定义 G Kirchhoff 矩阵(也称为拉普拉斯算子)C[G]C[G]=D[G]-A[G]Matrix-Tree 定理可以描述为:

      G 的所有不同的生成树的个数等于其 Kirchhoff矩阵 C[G]任何一个 n-1 阶主子式的行列式的绝对值。

        所谓 n-1 阶主子式,就是对r(1≤rn),将 C[G]的第 r 行、第 r 列同时去掉后得到的新矩阵,用 Cr[G]表示。 

    证明略

    [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

    建模后跑基尔霍夫行列式即可,注意减法取模问题。

     1 const mo=1000000000;
     2 var
     3     n,m,i,j,tot,cnt                            :longint;
     4     map                                        :array[0..10,0..10] of char;
     5     num                                        :array[0..10,0..10] of longint;
     6     con                                        :array[0..105,0..105] of longint;
     7     g                                        :array[0..105,0..105] of int64;
     8     
     9 procedure swap(var x,y:int64);
    10 var
    11     z                                        :int64;
    12 begin
    13     z:=x;
    14     x:=y;
    15     y:=z;
    16 end;
    17     
    18 function det():int64;
    19 var
    20     i,j,k                                    :longint;
    21     ret,tt                                    :int64;
    22 begin
    23     for i:=1 to tot do
    24     for j:=1 to tot do g[i,j]:=(g[i,j]+mo) mod mo;
    25     ret:=1;
    26     for i:=2 to tot do
    27     begin
    28         for j:=i+1 to tot do
    29         begin
    30             while g[j,i]<>0 do
    31             begin
    32                 tt:=g[i,i] div g[j,i];
    33                 for k:=i to tot do g[i,k]:=((g[i,k]-(tt*g[j,k]) mod mo)+mo) mod mo;
    34                 for k:=i to tot do swap(g[i,k],g[j,k]);
    35                 ret:=-ret;
    36             end;
    37         end;
    38         if g[i,i]=0 then exit(0);
    39         ret:=(ret*g[i,i]) mod mo;
    40     end;
    41     exit((ret+mo)mod mo)
    42 end;
    43     
    44 begin
    45     readln(n,m);
    46     for i:=1 to n do
    47     begin
    48         for j:=1 to m do read(map[i,j]);
    49         readln;
    50     end;
    51     for i:=1 to n do
    52     for j:=1 to m do 
    53     begin
    54         if map[i,j]='*' then continue;
    55         inc(tot);
    56         num[i,j]:=tot;
    57     end;
    58     for i:=1 to n do
    59     for j:=1 to m do
    60     begin
    61         if map[i,j]='*' then continue;
    62         if (i>1) and (map[i-1,j]<>'*') then con[num[i,j],num[i-1,j]]:=1;
    63         if (i<n) and (map[i+1,j]<>'*') then con[num[i,j],num[i+1,j]]:=1;
    64         if (j>1) and (map[i,j-1]<>'*') then con[num[i,j],num[i,j-1]]:=1;
    65         if (j<m) and (map[i,j+1]<>'*') then con[num[i,j],num[i,j+1]]:=1;
    66     end;
    67     for i:=1 to tot do
    68     begin
    69         cnt:=0;
    70         for j:=1 to tot do
    71         begin
    72             if i=j then continue;
    73             if (con[i,j]=1) then
    74             begin
    75                 inc(cnt);
    76                 g[i,j]:=-1;
    77             end;
    78         end;
    79         g[i,i]:=cnt;
    80     end;
    81     writeln(det());
    82 end.
  • 相关阅读:
    多线程22:线程池
    多线程21:信号灯法
    多线程20:管程法
    多线程19:生产者消费者模式
    多线程18:Lock锁
    多线程17:死锁
    多线程16:CopyOnWriteArrayList
    多线程15:线程同步
    多线程14:三大不安全案例
    业余草 maven异常:Updating Maven Project 的统一解决方案
  • 原文地址:https://www.cnblogs.com/zoewilly/p/6665677.html
Copyright © 2020-2023  润新知