結果

問題 No.2236 Lights Out On Simple Graph
ユーザー rlangevinrlangevin
提出日時 2023-03-03 22:28:19
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,281 bytes
コンパイル時間 1,786 ms
コンパイル使用メモリ 81,560 KB
実行使用メモリ 76,040 KB
最終ジャッジ日時 2023-10-18 02:37:40
合計ジャッジ時間 7,449 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,664 KB
testcase_01 AC 39 ms
53,520 KB
testcase_02 AC 39 ms
53,520 KB
testcase_03 AC 40 ms
53,520 KB
testcase_04 AC 40 ms
53,520 KB
testcase_05 AC 38 ms
53,520 KB
testcase_06 AC 40 ms
53,520 KB
testcase_07 AC 127 ms
76,008 KB
testcase_08 AC 39 ms
53,524 KB
testcase_09 AC 41 ms
53,524 KB
testcase_10 AC 90 ms
76,040 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]
    
def non_rec_dfs(s, ans):
    stack = []
    stack.append(~s)
    stack.append(s)
    par = [-1] * N
    while stack:
        u = stack.pop()
        seen[u] = 1
        if u >= 0:
            stack.append(~u)
            for v in G[u]:
                if v == par[u]:
                    continue
                par[v] = u
                stack.append(v)
        else:
            u = ~u
            if D[u] == 1 and par[u] != -1:
                D[u] = 0
                D[par[u]] = 1 - D[par[u]]
                ans += 1
    return ans

def popcount(n):
    cnt = 0
    while n:
        cnt += n & 1
        n //= 2
    return cnt

N, M = map(int, input().split())
G = [[] for i in range(N)]
A, B = [], []
U = UnionFind(N)
for i in range(M):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    if U.is_same(a, b):
        A.append(a)
        B.append(b)
    else:
        G[a].append(b)
        G[b].append(a)
        U.union(a, b)
C = list(map(int, input().split()))
N2 = len(A)
inf = 10 ** 18
ans = inf
for s in range(1 << N2):
    D = [0] * N
    val = popcount(s)
    for i in range(N):
        D[i] = C[i]
    for i in range(N2):
        if (s >> i) & 1:
            D[A[i]] = 1 - D[A[i]]
            D[B[i]] = 1 - D[B[i]]
    seen = [0] * N
    for i in range(N):
        if seen[i]:
            continue
        val = non_rec_dfs(i, val)
    if sum(D) == 0:
        ans = min(ans, val)
        
print(ans) if ans != inf else print(-1)
0