結果

問題 No.2435 Order All Company
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2023-08-18 22:24:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 404 ms / 2,000 ms
コード長 2,183 bytes
コンパイル時間 359 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 118,336 KB
最終ジャッジ日時 2024-05-06 04:39:49
合計ジャッジ時間 7,524 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,224 KB
testcase_01 AC 36 ms
52,352 KB
testcase_02 AC 37 ms
52,864 KB
testcase_03 AC 39 ms
52,736 KB
testcase_04 AC 51 ms
61,824 KB
testcase_05 AC 404 ms
117,444 KB
testcase_06 AC 390 ms
117,700 KB
testcase_07 AC 312 ms
93,100 KB
testcase_08 AC 398 ms
114,488 KB
testcase_09 AC 404 ms
117,664 KB
testcase_10 AC 94 ms
76,504 KB
testcase_11 AC 76 ms
72,960 KB
testcase_12 AC 62 ms
66,944 KB
testcase_13 AC 80 ms
75,776 KB
testcase_14 AC 247 ms
110,208 KB
testcase_15 AC 271 ms
111,996 KB
testcase_16 AC 170 ms
103,668 KB
testcase_17 AC 394 ms
111,264 KB
testcase_18 AC 165 ms
94,716 KB
testcase_19 AC 105 ms
84,324 KB
testcase_20 AC 124 ms
96,380 KB
testcase_21 AC 146 ms
100,732 KB
testcase_22 AC 246 ms
112,864 KB
testcase_23 AC 100 ms
85,248 KB
testcase_24 AC 162 ms
93,232 KB
testcase_25 AC 246 ms
118,336 KB
testcase_26 AC 194 ms
99,052 KB
testcase_27 AC 241 ms
115,920 KB
testcase_28 AC 131 ms
91,688 KB
testcase_29 AC 193 ms
100,316 KB
testcase_30 AC 179 ms
93,244 KB
testcase_31 AC 39 ms
52,096 KB
testcase_32 AC 39 ms
52,736 KB
testcase_33 AC 39 ms
52,352 KB
testcase_34 AC 86 ms
76,032 KB
testcase_35 AC 113 ms
76,928 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