結果

問題 No.2435 Order All Company
ユーザー yuyu_5510yuyu_5510
提出日時 2023-08-14 22:49:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 3,432 bytes
コンパイル時間 1,944 ms
コンパイル使用メモリ 209,320 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-15 15:10:34
合計ジャッジ時間 4,015 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 44 ms
4,380 KB
testcase_06 AC 42 ms
4,380 KB
testcase_07 AC 26 ms
4,376 KB
testcase_08 AC 42 ms
4,376 KB
testcase_09 AC 42 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 31 ms
4,380 KB
testcase_15 AC 34 ms
4,380 KB
testcase_16 AC 28 ms
4,376 KB
testcase_17 AC 42 ms
4,376 KB
testcase_18 AC 25 ms
4,376 KB
testcase_19 AC 25 ms
4,380 KB
testcase_20 AC 24 ms
4,384 KB
testcase_21 AC 30 ms
4,376 KB
testcase_22 AC 30 ms
4,376 KB
testcase_23 AC 19 ms
4,376 KB
testcase_24 AC 30 ms
4,380 KB
testcase_25 AC 29 ms
4,376 KB
testcase_26 AC 29 ms
4,376 KB
testcase_27 AC 28 ms
4,376 KB
testcase_28 AC 26 ms
4,380 KB
testcase_29 AC 30 ms
4,380 KB
testcase_30 AC 34 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 4 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;


const long long mod = 998244353LL;
class mint {
    long long x;
public:
    mint(long long x=0) : x((x%mod+mod)%mod) {}

    long long val(){
        return x;
    }

    mint operator-() const { 
      return mint(-x);
    }
    mint& operator+=(const mint& a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint& a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const  mint& a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint& a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint& a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint& a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(long long t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint& a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint& a) const {
        mint res(*this);
        return res/=a;
    }

    friend ostream& operator<<(ostream& os, const mint& m){
        os << m.x;
        return os;
    }
};


mint matrix_tree_theory(vector<vector<mint>>&mat, int N){
    mint ret = 1;
    for(int i = 0; i < N-1;i++){
        if(mat[i][i].val() == 0){
            for(int j = i+1;j < N-1;j++){
                if(mat[j][i].val()){
                    for(int k = 0;k < N-1;k++){
                        swap(mat[i][k], mat[j][k]);
                    }
                    ret *= -1;
                    break;
                }
            }
        }
        if(mat[i][i].val() == 0) return 0;

        for(int j = i+1;j < N-1;j++){
            if(mat[j][i].val()){
                mint mul = mat[j][i] / mat[i][i];
                for(int k = i;k < N-1;k++){
                    mat[j][k] -= mul * mat[i][k];
                }
            }
        }
    }

    for(int i = 0;i < N-1;i++){
        ret *= mat[i][i];
    }

    return ret;
}




int main(){
    int N, K;
    cin >> N >> K;

    vector<vector<vector<mint>>> G(K, vector<vector<mint>>(N, vector<mint>(N, mint(0))));
    for(int i = 0;i < K;i++){
        int t;
        cin >> t;
        for(int j = 0;j < t;j++){
            int a, b;
            cin >> a >> b;
            --a;--b;
            G[i][a][b] -= 1;
            G[i][b][a] -= 1;
        }
    }

    for(int i = 0;i < K;i++){
        for(int j = 0;j < N;j++){
            mint sum = 0;
            for(int k = 0;k < N;k++){
                sum += G[i][j][k];
            }
            sum *= -1;
            G[i][j][j] = sum;
        }
    }
    
    mint ans = 0;
    for(int i = 0;i < 1<<K;i++){
        vector<vector<mint>> mat(N, vector<mint>(N, mint(0)));
        int cnt = 0;
        for(int j = 0;j < K;j++){
            if(i & 1<<j){
                cnt++;
                for(int a = 0;a < N;a++){
                    for(int b = 0;b < N;b++){
                        mat[a][b] += G[j][a][b];
                    }
                }
            }
        }
        if((K-cnt) % 2 == 0)ans += matrix_tree_theory(mat, N);
        else ans -= matrix_tree_theory(mat, N);
    }

    cout << ans.val() << endl;
}
0