結果

問題 No.2435 Order All Company
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2023-08-18 22:24:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 428 ms / 2,000 ms
コード長 2,183 bytes
コンパイル時間 282 ms
コンパイル使用メモリ 86,608 KB
実行使用メモリ 128,432 KB
最終ジャッジ日時 2023-08-18 22:24:48
合計ジャッジ時間 8,537 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
71,080 KB
testcase_01 AC 77 ms
70,792 KB
testcase_02 AC 62 ms
70,952 KB
testcase_03 AC 63 ms
71,152 KB
testcase_04 AC 74 ms
76,172 KB
testcase_05 AC 416 ms
128,100 KB
testcase_06 AC 409 ms
128,432 KB
testcase_07 AC 354 ms
95,828 KB
testcase_08 AC 418 ms
126,868 KB
testcase_09 AC 428 ms
123,192 KB
testcase_10 AC 127 ms
77,360 KB
testcase_11 AC 95 ms
76,684 KB
testcase_12 AC 82 ms
76,192 KB
testcase_13 AC 90 ms
76,692 KB
testcase_14 AC 248 ms
111,668 KB
testcase_15 AC 278 ms
105,052 KB
testcase_16 AC 183 ms
105,596 KB
testcase_17 AC 425 ms
125,584 KB
testcase_18 AC 178 ms
95,944 KB
testcase_19 AC 122 ms
86,056 KB
testcase_20 AC 141 ms
93,140 KB
testcase_21 AC 155 ms
102,572 KB
testcase_22 AC 271 ms
109,232 KB
testcase_23 AC 110 ms
86,068 KB
testcase_24 AC 179 ms
93,532 KB
testcase_25 AC 243 ms
109,644 KB
testcase_26 AC 195 ms
101,920 KB
testcase_27 AC 255 ms
115,300 KB
testcase_28 AC 162 ms
96,704 KB
testcase_29 AC 199 ms
100,580 KB
testcase_30 AC 185 ms
94,188 KB
testcase_31 AC 63 ms
71,120 KB
testcase_32 AC 62 ms
70,916 KB
testcase_33 AC 64 ms
70,956 KB
testcase_34 AC 92 ms
76,944 KB
testcase_35 AC 129 ms
77,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""

包除原理+行列木定理

"""

import sys
from sys import stdin

def Detmod(a,mod = 10**18+3):

    assert len(a) == len(a[0])
    n = len(a)
    
    b = [[a[i][j] for j in range(n)] for i in range(n)]

    ret = 1
    for x in range(n):

        ret *= b[x][x]
        ret %= mod
        if b[x][x] == 0:
            return 0
        inv = pow(b[x][x],mod-2,mod)
        
        for i in range(x+1,n):

            mul = b[i][x] * inv

            for j in range(x,n):
                b[i][j] -= b[x][j] * mul
                b[i][j] %= mod

    return ret


# need Detmod
class MatrixTreeTheorem():

    def __init__(self):

        self.vdic = {}
        self.edges = []

    def add_vertex(self,v):
        if v not in self.vdic:
            self.vdic[v] = len(self.vdic)
        return

    def add_edge(self,u,v):

        assert u in self.vdic
        assert v in self.vdic
        
        if u == v:
            return
        self.edges.append( (self.vdic[u],self.vdic[v]) )

    def calc(self, mod = 10**18+3):

        if len(self.vdic) == 0:
            return 1
        elif len(self.vdic) == 1:
            return 1

        H = [[0] * len(self.vdic) for i in range(len(self.vdic))]

        for u2,v2 in self.edges:
            H[u2][u2] += 1
            H[v2][v2] += 1
            H[v2][u2] -= 1
            H[u2][v2] -= 1

        for i in range(len(self.vdic)):
            del H[i][-1]
        del H[-1]

        return Detmod(H,mod)

mod = 998244353

N,K = map(int,stdin.readline().split())

ab = [ [] for i in range(K) ]

for lp in range(K):

    t = int(stdin.readline())

    for i in range(t):

        a,b = map(int,stdin.readline().split())
        a -= 1
        b -= 1

        ab[lp].append( (a,b) )

ans = 0
for bit in range(2**K):

    zero = 0
    for i in range(K):
        if 2**i & bit == 0:
            zero += 1

    mt = MatrixTreeTheorem()
    for i in range(N):
        mt.add_vertex(i)
    
    for i in range(K):
        if 2**i & bit > 0:
            for a,b in ab[i]:
                mt.add_edge(a,b)

    now = mt.calc(mod = 998244353)
    if zero % 2 == 0:
        ans += now
    else:
        ans -= now

print (ans % mod)
0