結果

問題 No.1900 Don't be Powers of 2
ユーザー tamatotamato
提出日時 2022-04-08 22:45:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,577 ms / 2,000 ms
コード長 3,176 bytes
コンパイル時間 273 ms
コンパイル使用メモリ 82,168 KB
実行使用メモリ 323,216 KB
最終ジャッジ日時 2024-11-28 13:35:59
合計ジャッジ時間 16,470 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 998244353


def main():
    import sys
    input = sys.stdin.readline
    from collections import deque

    class Dinic:
        def __init__(self, N):
            self.N = N
            self.adj = [[] for _ in range(N + 1)]
            self.lv = None
            self.progress = None
            self.inf = 1 << 60

        def add_edge(self, fr, to, cap):
            # [to, cap, rev]
            forward = [to, cap, None]
            backward = forward[2] = [fr, 0, forward]
            self.adj[fr].append(forward)
            self.adj[to].append(backward)

        def bfs(self, s, t):
            que = deque([s])
            lv = [-1] * (self.N + 1)
            lv[s] = 0
            while que:
                v = que.popleft()
                lvv = lv[v]
                for u, cap, _ in self.adj[v]:
                    if cap and lv[u] == -1:
                        que.append(u)
                        lv[u] = lvv + 1
                        if u == t:
                            break
            self.lv = lv

        def dfs(self, s, t, flow):
            st = deque([s])
            st_f = deque([flow])
            while st:
                v = st[-1]
                f = st_f[-1]
                if v == t:
                    for i in range(len(st) - 1, 0, -1):
                        p = st[i - 1]
                        edge = self.adj[p][self.progress[p] - 1]
                        edge[1] -= f
                        edge[2][1] += f
                    return f
                if self.progress[v] == len(self.adj[v]):
                    st.pop()
                    st_f.pop()
                    continue
                else:
                    adj_v = self.adj[v]
                    for i in range(self.progress[v], len(adj_v)):
                        i = self.progress[v]
                        self.progress[v] = i + 1
                        u, cap, rev = adj_v[i]
                        if not cap or self.lv[u] != self.lv[v] + 1:
                            continue
                        else:
                            st.append(u)
                            st_f.append(min(st_f[-1], cap))
                            break

        def max_flow(self, s, t):
            flow = 0
            while True:
                self.bfs(s, t)
                if self.lv[t] < 0:
                    return flow
                self.progress = [0] * (self.N + 1)
                flow_new = self.dfs(s, t, self.inf)
                while flow_new:
                    flow += flow_new
                    flow_new = self.dfs(s, t, self.inf)

    def popcount(n):
        res = 0
        for i in range(30):
            res += n >> i & 1
        return res

    N = int(input())
    A = list(map(int, input().split()))

    D = Dinic(N + 2)
    s = N + 1
    t = s + 1
    for i, a in enumerate(A):
        k = popcount(a)
        if k & 1:
            D.add_edge(i + 1, t, 1)
        else:
            D.add_edge(s, i + 1, 1)
            for j, b in enumerate(A):
                if popcount(a ^ b) == 1:
                    D.add_edge(i + 1, j + 1, 1)

    print(N - D.max_flow(s, t))


if __name__ == '__main__':
    main()
0