結果

問題 No.2236 Lights Out On Simple Graph
ユーザー rlangevinrlangevin
提出日時 2023-03-03 22:34:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,779 ms / 4,000 ms
コード長 2,912 bytes
コンパイル時間 2,164 ms
コンパイル使用メモリ 81,416 KB
実行使用メモリ 76,200 KB
最終ジャッジ日時 2023-10-18 02:43:38
合計ジャッジ時間 11,949 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,472 KB
testcase_01 AC 39 ms
53,472 KB
testcase_02 AC 39 ms
53,472 KB
testcase_03 AC 40 ms
53,472 KB
testcase_04 AC 40 ms
53,472 KB
testcase_05 AC 39 ms
53,472 KB
testcase_06 AC 40 ms
53,472 KB
testcase_07 AC 87 ms
75,920 KB
testcase_08 AC 39 ms
53,472 KB
testcase_09 AC 39 ms
53,472 KB
testcase_10 AC 89 ms
75,936 KB
testcase_11 AC 466 ms
76,024 KB
testcase_12 AC 76 ms
73,248 KB
testcase_13 AC 65 ms
68,260 KB
testcase_14 AC 87 ms
75,936 KB
testcase_15 AC 48 ms
61,692 KB
testcase_16 AC 41 ms
53,472 KB
testcase_17 AC 119 ms
75,932 KB
testcase_18 AC 40 ms
53,472 KB
testcase_19 AC 57 ms
64,100 KB
testcase_20 AC 40 ms
53,472 KB
testcase_21 AC 40 ms
53,472 KB
testcase_22 AC 40 ms
53,472 KB
testcase_23 AC 85 ms
75,932 KB
testcase_24 AC 74 ms
72,676 KB
testcase_25 AC 41 ms
53,472 KB
testcase_26 AC 41 ms
53,472 KB
testcase_27 AC 75 ms
73,064 KB
testcase_28 AC 40 ms
53,472 KB
testcase_29 AC 40 ms
53,472 KB
testcase_30 AC 40 ms
53,472 KB
testcase_31 AC 41 ms
53,472 KB
testcase_32 AC 40 ms
53,472 KB
testcase_33 AC 40 ms
53,472 KB
testcase_34 AC 40 ms
53,472 KB
testcase_35 AC 111 ms
75,932 KB
testcase_36 AC 199 ms
75,936 KB
testcase_37 AC 84 ms
75,936 KB
testcase_38 AC 292 ms
75,936 KB
testcase_39 AC 2,779 ms
76,000 KB
testcase_40 AC 93 ms
76,200 KB
testcase_41 AC 93 ms
75,936 KB
testcase_42 AC 72 ms
72,692 KB
testcase_43 AC 81 ms
75,936 KB
testcase_44 AC 231 ms
75,932 KB
testcase_45 AC 72 ms
72,684 KB
testcase_46 AC 72 ms
72,696 KB
testcase_47 AC 64 ms
68,260 KB
testcase_48 AC 74 ms
73,228 KB
testcase_49 AC 87 ms
75,936 KB
testcase_50 AC 71 ms
75,112 KB
testcase_51 AC 57 ms
66,176 KB
testcase_52 AC 88 ms
75,936 KB
testcase_53 AC 40 ms
53,472 KB
testcase_54 AC 103 ms
75,936 KB
testcase_55 AC 333 ms
75,932 KB
testcase_56 AC 181 ms
75,932 KB
testcase_57 AC 246 ms
76,156 KB
testcase_58 AC 149 ms
75,412 KB
testcase_59 AC 281 ms
75,936 KB
権限があれば一括ダウンロードができます

ソースコード

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

# https://qiita.com/zawawahoge/items/8bbd4c2319e7f7746266
def popcount(x):
    '''xの立っているビット数をカウントする関数
    (xは64bit整数)'''

    # 2bitごとの組に分け、立っているビット数を2bitで表現する
    x = x - ((x >> 1) & 0x5555555555555555)

    # 4bit整数に 上位2bit + 下位2bit を計算した値を入れる
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)

    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f # 8bitごと
    x = x + (x >> 8) # 16bitごと
    x = x + (x >> 16) # 32bitごと
    x = x + (x >> 32) # 64bitごと = 全部の合計
    return x & 0x0000007f

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)
    if val > ans:
        continue
    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 val > ans:
            continue
    if sum(D) == 0:
        ans = min(ans, val)
        
print(ans) if ans != inf else print(-1)
0