結果

問題 No.1116 Cycles of Dense Graph
ユーザー mkawa2mkawa2
提出日時 2022-07-28 23:26:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 235 ms / 2,000 ms
コード長 3,254 bytes
コンパイル時間 427 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 84,224 KB
最終ジャッジ日時 2024-07-18 13:47:26
合計ジャッジ時間 5,216 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
69,376 KB
testcase_01 AC 235 ms
83,456 KB
testcase_02 AC 84 ms
84,224 KB
testcase_03 AC 61 ms
72,320 KB
testcase_04 AC 55 ms
71,552 KB
testcase_05 AC 90 ms
84,080 KB
testcase_06 AC 105 ms
84,224 KB
testcase_07 AC 79 ms
84,012 KB
testcase_08 AC 52 ms
71,552 KB
testcase_09 AC 54 ms
72,576 KB
testcase_10 AC 54 ms
72,704 KB
testcase_11 AC 89 ms
83,712 KB
testcase_12 AC 93 ms
84,096 KB
testcase_13 AC 51 ms
71,424 KB
testcase_14 AC 53 ms
72,832 KB
testcase_15 AC 53 ms
72,448 KB
testcase_16 AC 89 ms
83,968 KB
testcase_17 AC 55 ms
71,552 KB
testcase_18 AC 79 ms
84,224 KB
testcase_19 AC 87 ms
83,968 KB
testcase_20 AC 85 ms
84,096 KB
testcase_21 AC 60 ms
75,392 KB
testcase_22 AC 102 ms
83,456 KB
testcase_23 AC 55 ms
72,832 KB
testcase_24 AC 183 ms
83,456 KB
testcase_25 AC 50 ms
70,016 KB
testcase_26 AC 168 ms
83,584 KB
testcase_27 AC 54 ms
72,960 KB
testcase_28 AC 62 ms
75,904 KB
testcase_29 AC 51 ms
69,760 KB
testcase_30 AC 49 ms
69,120 KB
testcase_31 AC 46 ms
68,992 KB
testcase_32 AC 48 ms
69,504 KB
testcase_33 AC 46 ms
69,120 KB
testcase_34 AC 48 ms
68,992 KB
testcase_35 AC 47 ms
69,248 KB
testcase_36 AC 170 ms
83,840 KB
testcase_37 AC 229 ms
83,456 KB
testcase_38 AC 227 ms
83,512 KB
testcase_39 AC 152 ms
83,456 KB
testcase_40 AC 157 ms
83,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
dij = [(-1, 1), (-1, 0), (-1, -1), (1, -1), (1, 1)]
inf = (1 << 63)-1
# inf = (1 << 31)-1
# md = 10**9+7
md = 998244353

class UnionFind:
    def __init__(self, uu=[]):
        self.state = {u: -1 for u in uu}
        self.cnt = len(self.state)

    def add(self, u):
        if u in self.state: return False
        self.state[u] = -1
        self.cnt += 1
        return True

    def root(self, u):
        stack = []
        while self.state[u] >= 0:
            stack.append(u)
            u = self.state[u]
        for v in stack: self.state[v] = u
        return u

    def same(self, u, v):
        return self.root(u) == self.root(v)

    def merge(self, u, v):
        self.add(u)
        self.add(v)
        u = self.root(u)
        v = self.root(v)
        if u == v: return False
        su = -self.state[u]
        sv = -self.state[v]
        if su < sv: u, v = v, u
        self.state[u] = -su-sv
        self.state[v] = u
        self.cnt -= 1
        return True

    def size(self, u):
        return -self.state[self.root(u)]

from collections import Counter

def nHr(hn, hr):
    return nCr(hn+hr-1, hr-1)

def nPr(com_n, com_r):
    if com_r < 0: return 0
    if com_n < com_r: return 0
    return fac[com_n]*ifac[com_n-com_r]%md

def nCr(com_n, com_r):
    if com_r < 0: return 0
    if com_n < com_r: return 0
    return fac[com_n]*ifac[com_r]%md*ifac[com_n-com_r]%md

n_max = 100005
fac = [1]
for i in range(1, n_max+1): fac.append(fac[-1]*i%md)
ifac = [1]*(n_max+1)
ifac[n_max] = pow(fac[n_max], md-2, md)
for i in range(n_max-1, 1, -1): ifac[i] = ifac[i+1]*(i+1)%md

def popcnt(a): return bin(a).count("1")

n, m = LI()
ab = LLI1(m)

pe = Counter()
ans = 0
for s in range(1, 1 << m):
    uf = UnionFind()
    cyc = 0
    deg = Counter()
    for i, (a, b) in enumerate(ab):
        if s >> i & 1 == 0: continue
        deg[a] += 1
        deg[b] += 1
        if deg[a] > 2 or deg[b] > 2:
            cyc = 2
            break
        if not uf.merge(a, b):
            cyc += 1
            if cyc == 2: break
    if cyc == 2: continue
    if cyc and uf.cnt > 1: continue
    e = popcnt(s)
    if cyc:
        if e & 1: ans -= 1
        else: ans += 1
    else:
        pe[uf.cnt, e] += 1
# pDB(ans, pe)

inv = (md+1)//2
for k in range(3, n+1):
    ans += nCr(n, k)*fac[k-1]%md*inv%md
    ans %= md
# pDB(ans)

for (p, e), c in pe.items():
    v = e+p
    cur = 0
    for k in range(max(v, 3), n+1):
        cur += nCr(n-v, k-v)*fac[k-v+p-1]%md*pow(2, p-1, md)%md
        cur %= md
    cur = cur*c%md
    if e & 1: ans -= cur
    else: ans += cur
    ans %= md

print(ans)
0