• 无向图生成树计数 基尔霍夫矩阵 SPOJ Highways


    基尔霍夫矩阵

    https://blog.csdn.net/w4149/article/details/77387045

    https://blog.csdn.net/qq_29963431/article/details/51236064

    题目链接  https://vjudge.net/problem/SPOJ-HIGH

    AC代码

     1 #include <bits/stdc++.h>
     2 #define pb push_back
     3 #define mp make_pair
     4 #define fi first
     5 #define se second
     6 #define all(a) (a).begin(), (a).end()
     7 #define fillchar(a, x) memset(a, x, sizeof(a))
     8 #define huan printf("
    ");
     9 #define debug(a,b) cout<<a<<" "<<b<<" ";
    10 using namespace std;
    11 typedef long long ll;
    12 const int maxn=1e3+100,maxm=100,inf=0x3f3f3f3f;
    13 const ll mod=998244353;
    14 ll a[maxn][maxn],du[maxn];
    15 ll det(int n)
    16 {
    17     ll ans = 1;
    18     for (int i = 2; i <= n; i++)
    19     {
    20         for (int j = i + 1; j <= n; j++)
    21         {
    22             while (a[j][i] != 0)
    23             {
    24                 ll u = a[i][i] / a[j][i];
    25                 for (int k = i; k <= n; k++)
    26                 {
    27                     ll t = (a[i][k] - (ll)a[j][k] * u);// % mod + mod) % mod;
    28                     a[i][k] = a[j][k];
    29                     a[j][k] = t;
    30                 }
    31                 ans = -ans;
    32             }
    33         }
    34         ans = ans * a[i][i] ;//% mod;
    35     }
    36     if (ans < 0)
    37     {
    38         ans=-ans;
    39         //ans += mod;
    40     }
    41     return ans;
    42 }
    43 int main()
    44 {
    45     int t,n,m;
    46     scanf("%d",&t);
    47     while(t--)
    48     {
    49         fillchar(a,0);
    50         fillchar(du,0);
    51         scanf("%d%d",&n,&m);
    52         for(int i=0;i<m;i++)
    53         {
    54             int u,v;
    55             scanf("%d%d",&u,&v);
    56             du[u]++,du[v]++;
    57             a[v][u]=-1,a[u][v]=-1;
    58         }
    59         for(int i=1;i<=n;i++)
    60             a[i][i]=du[i];
    61         printf("%lld
    ",det(n));
    62     }
    63 }

     行列式求值模板

    int a[maxn][maxn];
    ll det(int n)
    {
        ll ans = 1;
        for (int i = 1; i <= n; i++)
        {
            for (int j = i + 1; j <= n; j++)
            {
                while (a[j][i] != 0)
                {
                    ll u = a[i][i] / a[j][i];
                    for (int k = i; k <= n; k++)
                    {
                        int t = (a[i][k] - (ll)a[j][k] * u % mod + mod) % mod;
                        a[i][k] = a[j][k];
                        a[j][k] = t;
                    }
                    ans = -ans;
                }
            }
            ans = ans * a[i][i] % mod;
        }
        if (ans < 0)
        {
            ans += mod;
        }
        return ans;
    }
  • 相关阅读:
    额外的 string 操作
    vector 对象是如何增长的
    顺序容器操作
    容器库概览
    顺序容器概述
    特定容器算法
    泛型算法结构
    再探迭代器
    定制操作
    使用关联容器
  • 原文地址:https://www.cnblogs.com/stranger-/p/9508249.html
Copyright © 2020-2023  润新知