結果

問題 No.2236 Lights Out On Simple Graph
ユーザー titia
提出日時 2023-03-05 01:59:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,200 ms / 4,000 ms
コード長 824 bytes
コンパイル時間 208 ms
コンパイル使用メモリ 82,212 KB
実行使用メモリ 169,636 KB
最終ジャッジ日時 2024-09-18 01:28:10
合計ジャッジ時間 47,857 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M=map(int,input().split())
E=[]
for i in range(M):
    a,b=map(int,input().split())
    a-=1
    b-=1
    E.append((1<<a)^(1<<b))
C=list(map(int,input().split()))
now=0
for i in range(N):
    if C[i]==1:
        now^=(1<<i)

C=now

D=dict()
for i in range(1<<(min(20,M))):
    now=0
    c=0
    for j in range(min(20,M)):
        if i & (1<<j) != 0:
            now^=E[j]
            c+=1

    if now in D:
        D[now]=min(D[now],c)
    else:
        D[now]=c
    
if M<=20:
    if C in D:
        print(D[C])
    else:
        print(-1)
    exit()

ANS=1<<60

for i in range(1<<min(M-20,20)):
    now=0
    c=0
    for j in range(min(20,M-20)):
        if i & (1<<j) != 0:
            now^=E[j+20]
            c+=1

    if now^C in D:
        ANS=min(ANS,D[now^C]+c)

if ANS==1<<60:
    print(-1)
else:
    print(ANS)
0