結果

問題 No.2435 Order All Company
ユーザー pockynypockyny
提出日時 2023-08-18 22:34:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 99 ms / 2,000 ms
コード長 2,243 bytes
コンパイル時間 955 ms
コンパイル使用メモリ 83,132 KB
実行使用メモリ 4,444 KB
最終ジャッジ日時 2023-08-18 22:34:10
合計ジャッジ時間 3,766 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,384 KB
testcase_05 AC 97 ms
4,384 KB
testcase_06 AC 97 ms
4,384 KB
testcase_07 AC 78 ms
4,384 KB
testcase_08 AC 97 ms
4,384 KB
testcase_09 AC 96 ms
4,380 KB
testcase_10 AC 8 ms
4,380 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 4 ms
4,384 KB
testcase_13 AC 10 ms
4,380 KB
testcase_14 AC 42 ms
4,380 KB
testcase_15 AC 56 ms
4,444 KB
testcase_16 AC 32 ms
4,380 KB
testcase_17 AC 99 ms
4,380 KB
testcase_18 AC 33 ms
4,384 KB
testcase_19 AC 30 ms
4,384 KB
testcase_20 AC 29 ms
4,384 KB
testcase_21 AC 35 ms
4,380 KB
testcase_22 AC 41 ms
4,380 KB
testcase_23 AC 23 ms
4,384 KB
testcase_24 AC 41 ms
4,380 KB
testcase_25 AC 39 ms
4,384 KB
testcase_26 AC 38 ms
4,380 KB
testcase_27 AC 39 ms
4,380 KB
testcase_28 AC 29 ms
4,380 KB
testcase_29 AC 64 ms
4,380 KB
testcase_30 AC 46 ms
4,384 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 5 ms
4,380 KB
testcase_35 AC 17 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;
ll mod = 998244353;
ll pw(ll a,ll x){
    ll ret = 1;
    while(x){
        if(x&1) (ret *= a) %= mod;
        (a *= a) %= mod; x /= 2;
    }
    return ret;
}

ll inv(ll x){return pw(x,mod - 2);}
ll det(vector<vector<ll>> a){
    int i,j,k,n = a.size();
    ll ret = 1;
    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            if(a[i][j]<0) a[i][j] += mod;
        }
    }
    for(i=0;i<n;i++){
        int piv = -1;
        for(j=i;j<n;j++){
            if(a[j][i]!=0){
                piv = j;
                break;
            }
        }
        if(piv==-1) break; 
        swap(a[piv],a[i]);
        if(piv!=i) ret *= -1;
        for(j=i + 1;j<n;j++){
            ll x = a[j][i]*inv(a[i][i])%mod;
            for(k=0;k<n;k++){
                (a[j][k] += mod - a[i][k]*x%mod) %= mod;
            }
        }
    }
    if(ret<0) ret += mod;
    for(i=0;i<n;i++) (ret *= a[i][i]) %= mod;
    return ret;
}

ll matrix_tree(vector<vector<ll>> mat){
    int i,j,k,n = mat.size();
    if(n==1) return 1;
    vector<vector<ll>> mat2(n - 1,vector<ll>(n - 1));
    //(n,n) no yoinshi
    for(i=0;i<n - 1;i++){
        for(j=0;j<n - 1;j++){
            if(i==j){
                for(k=0;k<n;k++){
                    if(k!=i) mat2[i][i] += mat[i][k];
                }
            }else{
                mat2[i][j] = mod - mat[i][j];
            }
        }
    }
    return det(mat2);
}

vector<pair<int,int>> v[10];
int main(){
    int i,j,n,k; cin >> n >> k;
    for(i=0;i<k;i++){
        int t; cin >> t;
        for(j=0;j<t;j++){
            int a,b; cin >> a >> b; a--; b--;
            v[i].push_back({a,b});
        }
    }
    ll ans = 0;
    for(i=0;i<(1<<k);i++){
        int c = 0;
        vector<vector<ll>> mat(n,vector<ll>(n,0));
        for(j=0;j<k;j++){
            if(i>>j&1) c++;
            else{
                for(auto p:v[j]){
                    mat[p.first][p.second]++;
                    mat[p.second][p.first]++;
                }
            }
        }
        if(c&1){
            ans += mod - matrix_tree(mat);
        }else{
            ans += matrix_tree(mat);
        }
        (ans %= mod);
    }
    cout << ans << "\n";
}
0